diff --git a/.cirrus.yml b/.cirrus.yml index e59b4b9f9..5a6fb8c59 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,15 +1,18 @@ freebsd_instance: - image_family: freebsd-13-0-snap + image_family: freebsd-16-0-snap task: - name: FreeBSD 13.0 + name: FreeBSD env: IGNORE_OSVERSION: yes + skip_notifications: true prerequisites_script: - pkg update -f - pkg upgrade -y - - pkg install -y pkgconf vips node npm + - pkg install -y devel/git devel/pkgconf graphics/vips www/node22 www/npm + - pkg-config --modversion vips-cpp install_script: - - npm install --build-from-source --unsafe-perm + - npm install + - npm run build test_script: - - npm test + - node --test test/unit/io.js diff --git a/.editorconfig b/.editorconfig index 5760be583..62fb51fed 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,4 @@ indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true - -[*.md] -trim_trailing_whitespace = false +max_line_length = 120 diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 57bc2fef0..000000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -src/libvips/* linguist-vendored diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 0853a4adf..b65d48b98 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -6,8 +6,6 @@ Hello, thank you for your interest in helping! Please create a [new issue](https://github.com/lovell/sharp/issues/new) containing the steps to reproduce the problem. -If you're having installation problems, please include the output of running `npm install --verbose sharp`. - New bugs are assigned a `triage` label whilst under investigation. ## Submit a new feature request @@ -16,29 +14,29 @@ If a [similar request](https://github.com/lovell/sharp/labels/enhancement) exist it's probably fastest to add a comment to it about your requirement. Implementation is usually straightforward if libvips -[already supports](https://libvips.github.io/libvips/API/current/func-list.html) +[already supports](https://www.libvips.org/API/current/function-list.html) the feature you need. ## Submit a Pull Request to fix a bug Thank you! To prevent the problem occurring again, please add unit tests that would have failed. -Please select the `master` branch as the destination for your Pull Request so your fix can be included in the next minor release. +Please select the `main` branch as the destination for your Pull Request so your fix can be included in the next minor release. -Please squash your changes into a single commit using a command like `git rebase -i upstream/master`. +Please squash your changes into a single commit using a command like `git rebase -i upstream/main`. -To test C++ changes, you can compile the module using `npm install --build-from-source` and then run the tests using `npm test`. +To test C++ changes, you can compile the module using `npm run build` and then run the tests using `npm test`. ## Submit a Pull Request with a new feature -Please add JavaScript [unit tests](https://github.com/lovell/sharp/tree/master/test/unit) to cover your new feature. -A test coverage report for the JavaScript code is generated in the `coverage/lcov-report` directory. +Please add JavaScript [unit tests](https://github.com/lovell/sharp/tree/main/test/unit) to cover your new feature. +Please also update the [TypeScript definitions](https://github.com/lovell/sharp/tree/main/lib/index.d.ts), along with the [type definition tests](https://github.com/lovell/sharp/tree/main/test/types/sharp.test-d.ts). Where possible, the functional tests use gradient-based perceptual hashes based on [dHash](http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html) to compare expected vs actual images. -You deserve to add your details to the [list of contributors](https://github.com/lovell/sharp/blob/master/package.json#L5). +You deserve to add your details to the [list of contributors](https://github.com/lovell/sharp/blob/main/package.json#L5). 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. @@ -62,7 +60,7 @@ By way of example, the `background()` method present in v0.20.0 was deprecated i ## Documentation -The public API is documented with [JSDoc](http://usejsdoc.org/) annotated comments. +The public API is documented with [JSDoc](https://jsdoc.app/) annotated comments. These can be converted to Markdown by running: ```sh @@ -93,5 +91,5 @@ Please feel free to ask any questions via a [new issue](https://github.com/lovell/sharp/issues/new). If you're unable to post details publicly, please -[e-mail](https://github.com/lovell/sharp/blob/master/package.json#L5) +[e-mail](https://github.com/lovell/sharp/blob/main/package.json#L5) for private, paid consulting. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 69fc9b99c..d87e090af 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -5,12 +5,24 @@ labels: enhancement --- -What are you trying to achieve? +## Feature request -Have you searched for similar feature requests? +### What are you trying to achieve? -What would you expect the API to look like? + -What alternatives have you considered? +### When you searched for similar feature requests, what did you find that might be related? -Is there a sample image that helps explain? + + +### What would you expect the API to look like? + + + +### What alternatives have you considered? + + + +### Please provide sample image(s) that help explain this feature + + diff --git a/.github/ISSUE_TEMPLATE/installation.md b/.github/ISSUE_TEMPLATE/installation.md index 7bd2101fb..b47d10e9a 100644 --- a/.github/ISSUE_TEMPLATE/installation.md +++ b/.github/ISSUE_TEMPLATE/installation.md @@ -5,18 +5,65 @@ labels: installation --- -Did you see the [documentation relating to installation](https://sharp.pixelplumbing.com/install)? + -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? +## Possible install-time or require-time problem -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 using npm v6 or earlier and installing as a `root` or `sudo` user, have you tried with the `npm install --unsafe-perm` flag? +- [ ] I have read and understood all of the [documentation relating to installation](https://sharp.pixelplumbing.com/install). +- [ ] I have searched for known bugs relating to this problem in my choice of package manager. -If you are using npm v7, does the user running `npm install` own the directory it is run in? +You must confirm both of these before continuing. -If you are using the `ignore-scripts` feature of `npm`, have you tried with the `npm install --ignore-scripts=false` flag? +### Are you using the latest version of sharp? -What is the complete output of running `npm install --verbose sharp`? Have you checked this output for useful error messages? + -What is the output of running `npx envinfo --binaries --system`? +- [ ] I am using the latest version of `sharp` as reported by `npm view sharp dist-tags.latest`. + +If you cannot confirm this, please upgrade to the latest version and try again before opening an issue. + +If you are using another package which depends on a version of `sharp` that is not the latest, +please open an issue against that package instead. + +### Are you using a supported runtime? + + + +- [ ] I am using Node.js with a version that satisfies `^18.17.0 || ^20.3.0 || >=21.0.0` +- [ ] I am using Deno +- [ ] I am using Bun + +If you cannot confirm any of these, +please upgrade to the latest version +and try again before opening an issue. + +### Are you using a supported package manager and installing optional dependencies? + + + +- [ ] I am using npm >= 10.1.0 with `--include=optional` +- [ ] I am using yarn >= 3.2.0 +- [ ] I am using pnpm >= 7.1.0 with `--no-optional=false` +- [ ] I am using Deno +- [ ] I am using Bun + +If you cannot confirm any of these, please upgrade to the latest version of your chosen package manager +and ensure you are allowing the installation of optional or multi-platform dependencies before opening an issue. + +### What is the complete error message, including the full stack trace? + + + +### What is the complete output of running `npm install --verbose --foreground-scripts sharp` in an empty directory? + +
+ + + +
+ +### What is the output of running `npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp`? + + diff --git a/.github/ISSUE_TEMPLATE/possible-bug.md b/.github/ISSUE_TEMPLATE/possible-bug.md index 14b21b91d..c7e130e17 100644 --- a/.github/ISSUE_TEMPLATE/possible-bug.md +++ b/.github/ISSUE_TEMPLATE/possible-bug.md @@ -7,14 +7,59 @@ labels: triage -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`? +## Possible bug -What are the steps to reproduce? +### Is this a possible bug in a feature of sharp, unrelated to installation? -What is the expected behaviour? + -Are you able to provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem? +- [ ] Running `npm install sharp` completes without error. +- [ ] Running `node -e "require('sharp')"` completes without error. -Are you able to provide a sample image that helps explain the problem? +If you cannot confirm both of these, please open an [installation issue](https://github.com/lovell/sharp/issues/new?labels=installation&template=installation.md) instead. -What is the output of running `npx envinfo --binaries --system`? +### Are you using the latest version of sharp? + + + +- [ ] I am using the latest version of `sharp` as reported by `npm view sharp dist-tags.latest`. + +If you cannot confirm this, please upgrade to the latest version and try again before opening an issue. + +If you are using another package which depends on a version of `sharp` that is not the latest, please open an issue against that package instead. + +### What is the output of running `npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp`? + + + +### Does this problem relate to file caching? + +The default behaviour of libvips is to cache input files, which can lead to `EBUSY` or `EPERM` errors on Windows. +Use [`sharp.cache(false)`](https://sharp.pixelplumbing.com/api-utility#cache) to switch this feature off. + +- [ ] Adding `sharp.cache(false)` does not fix this problem. + +### Does this problem relate to images appearing to have been rotated by 90 degrees? + +Images that contain EXIF Orientation metadata are not auto-oriented. By default, EXIF metadata is removed. + +- To auto-orient pixel values use the parameter-less [`rotate()`](https://sharp.pixelplumbing.com/api-operation#rotate) operation. +- To retain EXIF Orientation use [`keepExif()`](https://sharp.pixelplumbing.com/api-output#keepexif). + +- [ ] Using `rotate()` or `keepExif()` does not fix this problem. + +### What are the steps to reproduce? + + + +### What is the expected behaviour? + + + +### Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem + + + +### Please provide sample image(s) that help explain this problem + + diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index 92e31e4fa..8fbdb5f4d 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -7,10 +7,20 @@ labels: question -What are you trying to achieve? +## Question about an existing feature -Have you searched for similar questions? +### What are you trying to achieve? -Are you able to provide a minimal, standalone code sample that demonstrates this question? + -Are you able to provide a sample image that helps explain the question? +### When you searched for similar issues, what did you find that might be related? + + + +### Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this question + + + +### Please provide sample image(s) that help explain this question + + diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 000000000..e17f100b8 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,18 @@ +# Security Policy + +## Supported Versions + +The latest version of `sharp` as published to npm +and reported by `npm view sharp dist-tags.latest` +is supported with security updates. + +## Reporting a Vulnerability + +Please use +[e-mail](https://github.com/lovell/sharp/blob/main/package.json#L5) +to report a vulnerability. + +You can expect a response within 48 hours +if you are a human reporting a genuine issue. + +Thank you in advance. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eefcee3c5..cd6838590 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,87 +1,356 @@ +name: CI on: - push - pull_request +permissions: {} jobs: - CI: + lint: + permissions: + contents: read + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v5 + with: + node-version: "24" + - run: npm install --ignore-scripts + - run: npm run lint-cpp + - run: npm run lint-js + - run: npm run lint-types + build-native: + permissions: + contents: read + needs: lint + name: "build-${{ matrix.platform }} [Node.js ${{ matrix.nodejs_version_major }}] ${{ matrix.package && '[package]' }}" 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:14-alpine3.13 - - 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 + - os: ubuntu-24.04 + container: rockylinux:8 + nodejs_arch: x64 + nodejs_version: "^18.17.0" + nodejs_version_major: 18 + platform: linux-x64 + package: true + - os: ubuntu-24.04 + container: rockylinux:8 + nodejs_arch: x64 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: linux-x64 + - os: ubuntu-24.04 + container: rockylinux:8 + nodejs_arch: x64 + nodejs_version: "^22.9.0" + nodejs_version_major: 22 + platform: linux-x64 + - os: ubuntu-24.04 + container: node:18-alpine3.17 + nodejs_version_major: 18 + platform: linuxmusl-x64 + package: true + - os: ubuntu-24.04 + container: node:20-alpine3.18 + nodejs_version_major: 20 + platform: linuxmusl-x64 + - os: ubuntu-24.04 + container: node:22-alpine3.20 + nodejs_version_major: 22 + platform: linuxmusl-x64 + - os: ubuntu-24.04-arm + container: arm64v8/rockylinux:8 + nodejs_arch: arm64 + nodejs_version: "^18.17.0" + nodejs_version_major: 18 + platform: linux-arm64 + package: true + - os: ubuntu-24.04-arm + container: arm64v8/rockylinux:8 + nodejs_arch: arm64 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: linux-arm64 + - os: macos-15-intel + nodejs_arch: x64 + nodejs_version: "^18.17.0" + nodejs_version_major: 18 + platform: darwin-x64 + package: true + - os: macos-15-intel + nodejs_arch: x64 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: darwin-x64 + - os: macos-15-intel + nodejs_arch: x64 + nodejs_version: "^22.9.0" + nodejs_version_major: 22 + platform: darwin-x64 + - os: macos-15 + nodejs_arch: arm64 + nodejs_version: "^18.17.0" + nodejs_version_major: 18 + platform: darwin-arm64 + package: true + - os: macos-15 + nodejs_arch: arm64 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: darwin-arm64 + - os: macos-15 + nodejs_arch: arm64 + nodejs_version: "^22.9.0" + nodejs_version_major: 22 + platform: darwin-arm64 + - os: windows-2022 + nodejs_arch: x86 + nodejs_version: "18.18.2" # pinned to avoid 18.19.0 and npm 10 + nodejs_version_major: 18 + platform: win32-ia32 + package: true + - os: windows-2022 + nodejs_arch: x86 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: win32-ia32 + - os: windows-2022 + nodejs_arch: x86 + nodejs_version: "^22.9.0" + nodejs_version_major: 22 + platform: win32-ia32 + - os: windows-2022 + nodejs_arch: x64 + nodejs_version: "^18.17.0" + nodejs_version_major: 18 + platform: win32-x64 + package: true + - os: windows-2022 + nodejs_arch: x64 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: win32-x64 + - os: windows-2022 + nodejs_arch: x64 + nodejs_version: "^22.9.0" + nodejs_version_major: 22 + platform: win32-x64 + - os: windows-11-arm + nodejs_arch: arm64 + nodejs_version: "^20.3.0" + nodejs_version_major: 20 + platform: win32-arm64 + package: true + - os: windows-11-arm + nodejs_arch: arm64 + nodejs_version: "^22.9.0" + nodejs_version_major: 22 + platform: win32-arm64 steps: - - name: Dependencies (Linux glibc) - if: contains(matrix.container, 'centos') + - name: Dependencies (Rocky Linux glibc) + if: contains(matrix.container, 'rockylinux') run: | - curl -sL https://rpm.nodesource.com/setup_${{ matrix.nodejs_version }}.x | bash - - yum install -y gcc-c++ make git python3 nodejs + dnf install -y gcc-toolset-14-gcc-c++ make git python3.12 fontconfig google-noto-sans-fonts + echo "/opt/rh/gcc-toolset-14/root/usr/bin" >> $GITHUB_PATH - name: Dependencies (Linux musl) if: contains(matrix.container, 'alpine') - run: apk add build-base git python3 --update-cache - - name: Dependencies (macOS, Windows) + run: apk add build-base git python3 font-noto --update-cache + - name: Dependencies (Python 3.11 - macOS, Windows) if: contains(matrix.os, 'macos') || contains(matrix.os, 'windows') - uses: actions/setup-node@v1 + uses: actions/setup-python@v5 + with: + python-version: "3.12" + - name: Dependencies (Node.js) + if: "!contains(matrix.platform, 'linuxmusl')" + uses: actions/setup-node@v5 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 + architecture: ${{ matrix.nodejs_arch }} + - uses: actions/checkout@v4 + - run: npm install + - run: npm run build + - run: npm run test-unit + - if: matrix.package + run: npm run package-from-local-build + - uses: actions/upload-artifact@v4 + if: matrix.package + with: + name: ${{ matrix.platform }} + path: npm/${{ matrix.platform }} + retention-days: 1 + if-no-files-found: error + build-linuxmusl-arm64: + permissions: + contents: read + needs: lint + name: "build-linuxmusl-arm64 [Node.js ${{ matrix.nodejs_version_major }}] ${{ matrix.package && '[package]' }}" + runs-on: ubuntu-24.04-arm + container: + image: ${{ matrix.container }} + volumes: + - /opt:/opt:rw,rshared + - /opt:/__e/node20:ro,rshared + strategy: + fail-fast: false + matrix: + include: + - container: node:18-alpine3.17 + nodejs_version_major: 18 + package: true + - container: node:20-alpine3.18 + nodejs_version_major: 20 + steps: + - name: Allow Linux musl containers on ARM64 runners # https://github.com/actions/runner/issues/801#issuecomment-2394425757 + shell: sh + run: | + sed -i "/^ID=/s/alpine/NotpineForGHA/" /etc/os-release + apk add nodejs --update-cache + mkdir /opt/bin + ln -s /usr/bin/node /opt/bin/node + - name: Dependencies + run: apk add build-base git python3 font-noto --update-cache + - uses: actions/checkout@v4 + - run: npm install + - run: npm run build + - run: npm run test-unit + - if: matrix.package + run: npm run package-from-local-build + - uses: actions/upload-artifact@v4 + if: matrix.package + with: + name: linuxmusl-arm64 + path: npm/linuxmusl-arm64 + retention-days: 1 + if-no-files-found: error + build-qemu: + permissions: + contents: read + needs: lint + name: "build-${{ matrix.platform }} [Node.js ${{ matrix.nodejs_version_major }}] [package]" + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + include: + - platform: linux-arm + base_image: "balenalib/rpi-raspbian:bullseye" + nodejs_arch: armv6l + nodejs_hostname: unofficial-builds.nodejs.org + nodejs_version: "18.17.0" + nodejs_version_major: 18 + - platform: linux-s390x + base_image: "--platform=linux/s390x s390x/debian:bookworm" + nodejs_arch: s390x + nodejs_hostname: nodejs.org + nodejs_version: "18.17.0" + nodejs_version_major: 18 + - platform: linux-ppc64 + base_image: "--platform=linux/ppc64le ppc64le/debian:bookworm" + nodejs_arch: ppc64le + nodejs_hostname: nodejs.org + nodejs_version: "18.17.0" + nodejs_version_major: 18 + - platform: linux-riscv64 + base_image: "--platform=linux/riscv64 riscv64/debian:trixie" + compiler_flags: "-march=rv64gc" + nodejs_arch: riscv64 + nodejs_hostname: unofficial-builds.nodejs.org + nodejs_version: "20.19.5" + nodejs_version_major: 20 + steps: + - uses: actions/checkout@v4 + - uses: uraimo/run-on-arch-action@v3 + with: + arch: none + distro: none + base_image: ${{ matrix.base_image }} + env: | + CFLAGS: "${{ matrix.compiler_flags }}" + CXXFLAGS: "${{ matrix.compiler_flags }}" + run: | + apt-get update + apt-get install -y curl g++ git libatomic1 make python3 xz-utils + mkdir /opt/nodejs + curl --silent https://${{ matrix.nodejs_hostname }}/download/release/v${{ matrix.nodejs_version}}/node-v${{ matrix.nodejs_version}}-linux-${{ matrix.nodejs_arch }}.tar.xz | tar xJC /opt/nodejs --strip-components=1 + export PATH=$PATH:/opt/nodejs/bin + npm install + npm run build + node --test test/unit/io.js + npm run package-from-local-build + - uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.platform }} + path: npm/${{ matrix.platform }} + retention-days: 1 + if-no-files-found: error + build-emscripten: + permissions: + contents: read + needs: lint + name: "build-wasm32 [package]" + runs-on: ubuntu-24.04 + container: "emscripten/emsdk:4.0.18" + steps: + - uses: actions/checkout@v4 + - name: Dependencies + run: apt-get update && apt-get install -y pkg-config + - name: Dependencies (Node.js) + uses: actions/setup-node@v5 + with: + node-version: "20" + - run: npm install + - run: emmake npm run build + - name: Verify emscripten versions match + run: | + EMSCRIPTEN_VERSION_LIBVIPS=$(node -p "require('@img/sharp-libvips-dev-wasm32/versions').emscripten") + EMSCRIPTEN_VERSION_SHARP=$(emcc -dumpversion) + echo "libvips built with emscripten $EMSCRIPTEN_VERSION_LIBVIPS" + echo "sharp built with emscripten $EMSCRIPTEN_VERSION_SHARP" + test "$EMSCRIPTEN_VERSION_LIBVIPS" = "$EMSCRIPTEN_VERSION_SHARP" + - run: emmake npm run test-unit + - run: emmake npm run package-from-local-build + - uses: actions/upload-artifact@v4 + with: + name: wasm32 + path: npm/wasm32 + retention-days: 1 + if-no-files-found: error + release: + permissions: + contents: write + id-token: write + runs-on: ubuntu-24.04 + needs: + - build-native + - build-linuxmusl-arm64 + - build-qemu + - build-emscripten + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + path: npm + - name: Create npm workspace tarball + run: tar -vcaf npm-workspace.tar.xz --directory npm --exclude=from-local-build.js . + - uses: actions/setup-node@v5 + with: + node-version: '24' + - name: Create release notes + run: npm run package-release-notes + - name: Create GitHub release for tag + if: startsWith(github.ref, 'refs/tags/v') + uses: ncipollo/release-action@v1 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 + artifacts: npm-workspace.tar.xz + artifactContentType: application/x-xz + prerelease: ${{ contains(github.ref, '-rc') }} + makeLatest: ${{ !contains(github.ref, '-rc') }} + bodyFile: release-notes.md + - name: Publish platform-specific npm packages + if: startsWith(github.ref, 'refs/tags/v') + run: cd npm && npm publish --workspaces --tag=${{ contains(github.ref, '-rc') && 'next' || 'latest' }} + - name: Publish sharp npm package + if: startsWith(github.ref, 'refs/tags/v') + run: npm publish --tag=${{ contains(github.ref, '-rc') && 'next' || 'latest' }} diff --git a/.github/workflows/npm.yml b/.github/workflows/npm.yml new file mode 100644 index 000000000..4db44a5b4 --- /dev/null +++ b/.github/workflows/npm.yml @@ -0,0 +1,196 @@ +name: "CI: npm smoke test" + +on: + push: + tags: + - "v**" + +permissions: {} + +jobs: + release-smoke-test: + name: "${{ github.ref_name }} ${{ matrix.name }}" + runs-on: ${{ matrix.runs-on }} + strategy: + fail-fast: false + matrix: + include: + - name: linux-x64-node-npm + runs-on: ubuntu-24.04 + runtime: node + package-manager: npm + - name: linux-x64-node-pnpm + runs-on: ubuntu-24.04 + runtime: node + package-manager: pnpm + - name: linux-x64-node-yarn + runs-on: ubuntu-24.04 + runtime: node + package-manager: yarn + - name: linux-x64-node-yarn-pnp + runs-on: ubuntu-24.04 + runtime: node + package-manager: yarn-pnp + - name: linux-x64-node-yarn-v1 + runs-on: ubuntu-24.04 + runtime: node + package-manager: yarn-v1 + - name: linux-x64-deno + runs-on: ubuntu-24.04 + runtime: deno + - name: linux-x64-bun + runs-on: ubuntu-24.04 + runtime: bun + + - name: darwin-x64-node-npm + runs-on: macos-15-intel + runtime: node + package-manager: npm + - name: darwin-x64-node-pnpm + runs-on: macos-15-intel + runtime: node + package-manager: pnpm + - name: darwin-x64-node-yarn + runs-on: macos-15-intel + runtime: node + package-manager: yarn + - name: darwin-x64-node-yarn-pnp + runs-on: macos-15-intel + runtime: node + package-manager: yarn-pnp + - name: darwin-x64-node-yarn-v1 + runs-on: macos-15-intel + runtime: node + package-manager: yarn-v1 + - name: darwin-x64-deno + runs-on: macos-15-intel + runtime: deno + - name: darwin-x64-bun + runs-on: macos-15-intel + runtime: bun + + - name: win32-x64-node-npm + runs-on: windows-2022 + runtime: node + package-manager: npm + - name: win32-x64-node-pnpm + runs-on: windows-2022 + runtime: node + package-manager: pnpm + - name: win32-x64-node-yarn + runs-on: windows-2022 + runtime: node + package-manager: yarn + - name: win32-x64-node-yarn-pnp + runs-on: windows-2022 + runtime: node + package-manager: yarn-pnp + - name: win32-x64-node-yarn-v1 + runs-on: windows-2022 + runtime: node + package-manager: yarn-v1 + - name: win32-x64-deno + runs-on: windows-2022 + runtime: deno + + steps: + - name: Install Node.js + if: ${{ matrix.runtime == 'node' }} + uses: actions/setup-node@v5 + with: + node-version: 20 + - name: Install pnpm + if: ${{ matrix.package-manager == 'pnpm' }} + uses: pnpm/action-setup@v4 + with: + version: 8 + - name: Install Deno + if: ${{ matrix.runtime == 'deno' }} + uses: denoland/setup-deno@v2 + with: + deno-version: v2.x + - name: Install Bun + if: ${{ matrix.runtime == 'bun' }} + uses: oven-sh/setup-bun@v2 + with: + bun-version: latest + + - name: Version + id: version + uses: actions/github-script@v8 + with: + script: | + core.setOutput('semver', context.ref.replace('refs/tags/v','')) + - name: Create package.json + uses: DamianReeves/write-file-action@v1.3 + with: + path: package.json + contents: | + { + "dependencies": { + "sharp": "${{ steps.version.outputs.semver }}" + }, + "type": "module" + } + - name: Create release.mjs + uses: DamianReeves/write-file-action@v1.3 + with: + path: release.mjs + contents: | + import { deepStrictEqual } from 'node:assert'; + import sharp from 'sharp'; + deepStrictEqual(['.jpg', '.jpeg', '.jpe', '.jfif'], sharp.format.jpeg.input.fileSuffix); + + - name: Run with Node.js + npm + if: ${{ matrix.package-manager == 'npm' }} + run: | + npm install --ignore-scripts + node release.mjs + + - name: Run with Node.js + pnpm + if: ${{ matrix.package-manager == 'pnpm' }} + run: | + pnpm install --ignore-scripts + node release.mjs + + - name: Run with Node.js + yarn + if: ${{ matrix.package-manager == 'yarn' }} + run: | + corepack enable + yarn set version stable + yarn config set enableImmutableInstalls false + yarn config set enableScripts false + yarn config set nodeLinker node-modules + yarn install + node release.mjs + + - name: Run with Node.js + yarn pnp + if: ${{ matrix.package-manager == 'yarn-pnp' }} + run: | + corepack enable + yarn set version stable + yarn config set enableImmutableInstalls false + yarn config set enableScripts false + yarn config set nodeLinker pnp + yarn install + yarn node release.mjs + + - name: Run with Node.js + yarn v1 + if: ${{ matrix.package-manager == 'yarn-v1' }} + run: | + corepack enable + yarn set version classic + yarn install + node release.mjs + + - name: Run with Deno + if: ${{ matrix.runtime == 'deno' }} + run: | + deno install + deno run --allow-env --allow-ffi --allow-read --allow-sys release.mjs + + - name: Run with Bun + if: ${{ matrix.runtime == 'bun' }} + run: | + bun install --ignore-scripts + bun release.mjs diff --git a/.gitignore b/.gitignore index f5dd9a65d..82f73b3f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,18 +1,19 @@ -build +src/build +src/node_modules node_modules /coverage +npm/*/* +!npm/*/package.json test/bench/node_modules test/fixtures/output* +test/fixtures/vips-properties.xml test/leak/libvips.supp -test/saliency/report.json -test/saliency/Image* -test/saliency/[Uu]serData* -!test/saliency/userData.js -vendor -.gitattributes .DS_Store .nyc_output .vscode/ package-lock.json .idea .firebase +.astro +docs/dist +release-notes.md diff --git a/.prebuildrc b/.prebuildrc deleted file mode 100644 index e9978aa33..000000000 --- a/.prebuildrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "include-regex": "(sharp\\.node|libvips-cpp\\.dll)", - "strip": true -} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 307e91452..000000000 --- a/.travis.yml +++ /dev/null @@ -1,108 +0,0 @@ -jobs: - include: - - 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 --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 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 --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 - os: linux - dist: bionic - language: shell - before_install: - - 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 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" - - - name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 14" - arch: arm64 - os: linux - dist: bionic - language: shell - before_install: - - 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 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" - - - name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 15" - arch: arm64 - os: linux - dist: bionic - 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" - - - name: "Linux ARM64v8 (Alpine 3.11, musl 1.1.24) - Node.js 10" - arch: arm64 - os: linux - dist: focal - language: shell - before_install: - - sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:10-alpine3.11 - - sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache" - 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 (Alpine 3.11, musl 1.1.24) - Node.js 12" - arch: arm64 - os: linux - dist: focal - language: shell - before_install: - - sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:12-alpine3.11 - - sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache" - 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 (Alpine 3.11, musl 1.1.24) - Node.js 14" - arch: arm64 - os: linux - dist: focal - language: shell - before_install: - - sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:14-alpine3.11 - - sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache" - 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 (Alpine 3.11, musl 1.1.24) - Node.js 15" - arch: arm64 - os: linux - dist: focal - language: shell - before_install: - - sudo chown 0.0 ${PWD} - - sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:15-alpine3.11 - - sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache" - 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 diff --git a/README.md b/README.md index 22d735865..47da52e8b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,14 @@ # sharp -sharp logo +sharp logo -The typical use case for this high speed Node.js module +The typical use case for this high speed Node-API module is to convert large images in common formats to -smaller, web-friendly JPEG, PNG, WebP and AVIF images of varying dimensions. +smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions. + +It can be used with all JavaScript runtimes +that provide support for Node-API v9, including +Node.js (^18.17.0 or >= 20.3.0), Deno and Bun. Resizing an image is typically 4x-5x faster than using the quickest ImageMagick and GraphicsMagick settings @@ -16,7 +20,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+ +Most modern macOS, Windows and Linux systems do not require any additional install or runtime dependencies. ## Documentation @@ -95,15 +99,12 @@ readableStream ## Contributing -A [guide for contributors](https://github.com/lovell/sharp/blob/master/.github/CONTRIBUTING.md) +A [guide for contributors](https://github.com/lovell/sharp/blob/main/.github/CONTRIBUTING.md) covers reporting bugs, requesting features and submitting code changes. -[![Test Coverage](https://coveralls.io/repos/lovell/sharp/badge.svg?branch=master)](https://coveralls.io/r/lovell/sharp?branch=master) -[![N-API v3](https://img.shields.io/badge/N--API-v3-green.svg)](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix) - ## Licensing -Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors. +Copyright 2013 Lovell Fuller and others. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 8ddfa0e71..000000000 --- a/appveyor.yml +++ /dev/null @@ -1,18 +0,0 @@ -os: Visual Studio 2019 -version: "{build}" -build: off -platform: x86 -environment: - matrix: - - nodejs_version: "10" - prebuild: true - - nodejs_version: "12" - - nodejs_version: "14" - - nodejs_version: "15" -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 diff --git a/binding.gyp b/binding.gyp deleted file mode 100644 index b3443dc69..000000000 --- a/binding.gyp +++ /dev/null @@ -1,230 +0,0 @@ -{ - 'variables': { - 'vips_version': ' - -## removeAlpha - -Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel. - -### Examples - -```javascript -sharp('rgba.png') - .removeAlpha() - .toFile('rgb.png', function(err, info) { - // rgb.png is a 3 channel image without an alpha channel - }); -``` - -Returns **Sharp** - -## ensureAlpha - -Ensure alpha channel, if missing. The added alpha channel will be fully opaque. This is a no-op if the image already has an alpha channel. - -### Examples - -```javascript -sharp('rgb.jpg') - .ensureAlpha() - .toFile('rgba.png', function(err, info) { - // rgba.png is a 4 channel image with a fully opaque alpha channel - }); -``` - -Returns **Sharp** - -**Meta** - -- **since**: 0.21.2 - -## extractChannel - -Extract a single channel from a multi-channel image. - -### Parameters - -- `channel` **([number][1] \| [string][2])** zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`. - -### Examples - -```javascript -sharp(input) - .extractChannel('green') - .toColourspace('b-w') - .toFile('green.jpg', function(err, info) { - // info.channels === 1 - // green.jpg is a greyscale image containing the green channel of the input - }); -``` - -- Throws **[Error][3]** Invalid channel - -Returns **Sharp** - -## joinChannel - -Join one or more channels to the image. -The meaning of the added channels depends on the output colourspace, set with `toColourspace()`. -By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. -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. -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 - -- `images` **([Array][4]<([string][2] \| [Buffer][5])> | [string][2] \| [Buffer][5])** one or more images (file paths, Buffers). -- `options` **[Object][6]** image options, see `sharp()` constructor. - - -- Throws **[Error][3]** Invalid parameters - -Returns **Sharp** - -## bandbool - -Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image. - -### Parameters - -- `boolOp` **[string][2]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively. - -### Examples - -```javascript -sharp('3-channel-rgb-input.png') - .bandbool(sharp.bool.and) - .toFile('1-channel-output.png', function (err, info) { - // The output will be a single channel image where each pixel `P = R & G & B`. - // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]` - // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`. - }); -``` - -- Throws **[Error][3]** Invalid parameters - -Returns **Sharp** - -[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number - -[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error - -[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array - -[5]: https://nodejs.org/api/buffer.html - -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object diff --git a/docs/api-colour.md b/docs/api-colour.md deleted file mode 100644 index 8dca338c1..000000000 --- a/docs/api-colour.md +++ /dev/null @@ -1,87 +0,0 @@ - - -## tint - -Tint the image using the provided chroma while preserving the image luminance. -An alpha channel may be present and will be unchanged by the operation. - -### Parameters - -- `rgb` **([string][1] \| [Object][2])** parsed by the [color][3] module to extract chroma values. - - -- Throws **[Error][4]** Invalid parameter - -Returns **Sharp** - -## greyscale - -Convert to 8-bit greyscale; 256 shades of grey. -This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use `gamma()` with `greyscale()` for the best results. -By default the output image will be web-friendly sRGB and contain three (identical) color channels. -This may be overridden by other sharp operations such as `toColourspace('b-w')`, -which will produce an output image containing one color channel. -An alpha channel may be present, and will be unchanged by the operation. - -### Parameters - -- `greyscale` **[Boolean][5]** (optional, default `true`) - -Returns **Sharp** - -## grayscale - -Alternative spelling of `greyscale`. - -### Parameters - -- `grayscale` **[Boolean][5]** (optional, default `true`) - -Returns **Sharp** - -## toColourspace - -Set the output colourspace. -By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. - -### Parameters - -- `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 - -Returns **Sharp** - -## toColorspace - -Alternative spelling of `toColourspace`. - -### Parameters - -- `colorspace` **[string][1]?** output colorspace. - - -- Throws **[Error][4]** Invalid parameters - -Returns **Sharp** - -[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[3]: https://www.npmjs.org/package/color - -[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error - -[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[6]: https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568 diff --git a/docs/api-composite.md b/docs/api-composite.md deleted file mode 100644 index 32a343bcf..000000000 --- a/docs/api-composite.md +++ /dev/null @@ -1,86 +0,0 @@ - - -## composite - -Composite image(s) over the processed (resized, extracted etc.) image. - -The images to composite must be the same size or smaller than the processed image. -If both `top` and `left` options are provided, they take precedence over `gravity`. - -The `blend` option can be one of `clear`, `source`, `over`, `in`, `out`, `atop`, -`dest`, `dest-over`, `dest-in`, `dest-out`, `dest-atop`, -`xor`, `add`, `saturate`, `multiply`, `screen`, `overlay`, `darken`, `lighten`, -`colour-dodge`, `color-dodge`, `colour-burn`,`color-burn`, -`hard-light`, `soft-light`, `difference`, `exclusion`. - -More information about blend modes can be found at -[https://libvips.github.io/libvips/API/current/libvips-conversion.html#VipsBlendMode][1] -and [https://www.cairographics.org/operators/][2] - -### Parameters - -- `images` **[Array][3]<[Object][4]>** Ordered list of images to composite - - `images[].input` **([Buffer][5] \| [String][6])?** Buffer containing image data, String containing the path to an image file, or Create object (see below) - - `images[].input.create` **[Object][4]?** describes a blank overlay to be created. - - `images[].input.create.width` **[Number][7]?** - - `images[].input.create.height` **[Number][7]?** - - `images[].input.create.channels` **[Number][7]?** 3-4 - - `images[].input.create.background` **([String][6] \| [Object][4])?** parsed by the [color][8] module to extract values for red, green, blue and alpha. - - `images[].blend` **[String][6]** how to blend this image with the image below. (optional, default `'over'`) - - `images[].gravity` **[String][6]** gravity at which to place the overlay. (optional, default `'centre'`) - - `images[].top` **[Number][7]?** the pixel offset from the top edge. - - `images[].left` **[Number][7]?** the pixel offset from the left edge. - - `images[].tile` **[Boolean][9]** set to true to repeat the overlay image across the entire image with the given `gravity`. (optional, default `false`) - - `images[].premultiplied` **[Boolean][9]** set to true to avoid premultipling the image below. Equivalent to the `--premultiplied` vips option. (optional, default `false`) - - `images[].density` **[Number][7]** number representing the DPI for vector overlay image. (optional, default `72`) - - `images[].raw` **[Object][4]?** describes overlay when using raw pixel data. - - `images[].raw.width` **[Number][7]?** - - `images[].raw.height` **[Number][7]?** - - `images[].raw.channels` **[Number][7]?** - -### Examples - -```javascript -sharp('input.png') - .rotate(180) - .resize(300) - .flatten( { background: '#ff6600' } ) - .composite([{ input: 'overlay.png', gravity: 'southeast' }]) - .sharpen() - .withMetadata() - .webp( { quality: 90 } ) - .toBuffer() - .then(function(outputBuffer) { - // outputBuffer contains upside down, 300px wide, alpha channel flattened - // onto orange background, composited with overlay.png with SE gravity, - // sharpened, with metadata, 90% quality WebP image data. Phew! - }); -``` - -- Throws **[Error][10]** Invalid parameters - -Returns **Sharp** - -**Meta** - -- **since**: 0.22.0 - -[1]: https://libvips.github.io/libvips/API/current/libvips-conversion.html#VipsBlendMode - -[2]: https://www.cairographics.org/operators/ - -[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array - -[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[5]: https://nodejs.org/api/buffer.html - -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number - -[8]: https://www.npmjs.org/package/color - -[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error diff --git a/docs/api-constructor.md b/docs/api-constructor.md deleted file mode 100644 index 0fb93a541..000000000 --- a/docs/api-constructor.md +++ /dev/null @@ -1,214 +0,0 @@ - - -## Sharp - -Constructor factory to create an instance of `sharp`, to which further methods are chained. - -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. - -Implements the [stream.Duplex][1] class. - -### Parameters - -- `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][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][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][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 - -```javascript -sharp('input.jpg') - .resize(300, 200) - .toFile('output.jpg', function(err) { - // output.jpg is a 300 pixels wide and 200 pixels high image - // containing a scaled and cropped version of input.jpg - }); -``` - -```javascript -// Read image data from readableStream, -// resize to 300 pixels wide, -// emit an 'info' event with calculated dimensions -// and finally write image data to writableStream -var transformer = sharp() - .resize(300) - .on('info', function(info) { - console.log('Image height is ' + info.height); - }); -readableStream.pipe(transformer).pipe(writableStream); -``` - -```javascript -// Create a blank 300x200 PNG image of semi-transluent red pixels -sharp({ - create: { - width: 300, - height: 200, - channels: 4, - background: { r: 255, g: 0, b: 0, alpha: 0.5 } - } -}) -.png() -.toBuffer() -.then( ... ); -``` - -```javascript -// Convert an animated GIF to an animated WebP -await sharp('in.gif', { animated: true }).toFile('out.webp'); -``` - -```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'); -``` - -```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 - -Take a "snapshot" of the Sharp instance, returning a new instance. -Cloned instances inherit the input of their parent instance. -This allows multiple output Streams and therefore multiple processing pipelines to share a single input Stream. - -### Examples - -```javascript -const pipeline = sharp().rotate(); -pipeline.clone().resize(800, 600).pipe(firstWritableStream); -pipeline.clone().extract({ left: 20, top: 20, width: 100, height: 100 }).pipe(secondWritableStream); -readableStream.pipe(pipeline); -// firstWritableStream receives auto-rotated, resized readableStream -// secondWritableStream receives auto-rotated, extracted region of readableStream -``` - -```javascript -// Create a pipeline that will download an image, resize it and format it to different files -// Using Promises to know when the pipeline is complete -const fs = require("fs"); -const got = require("got"); -const sharpStream = sharp({ - failOnError: false -}); - -const promises = []; - -promises.push( - sharpStream - .clone() - .jpeg({ quality: 100 }) - .toFile("originalFile.jpg") -); - -promises.push( - sharpStream - .clone() - .resize({ width: 500 }) - .jpeg({ quality: 80 }) - .toFile("optimized-500.jpg") -); - -promises.push( - sharpStream - .clone() - .resize({ width: 500 }) - .webp({ quality: 80 }) - .toFile("optimized-500.webp") -); - -// https://github.com/sindresorhus/got#gotstreamurl-options -got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream); - -Promise.all(promises) - .then(res => { console.log("Done!", res); }) - .catch(err => { - console.error("Error processing files, let's clean it up", err); - try { - fs.unlinkSync("originalFile.jpg"); - fs.unlinkSync("optimized-500.jpg"); - fs.unlinkSync("optimized-500.webp"); - } catch (e) {} - }); -``` - -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/Uint8Array - -[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray - -[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number - -[9]: https://www.npmjs.org/package/color - -[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error - -[11]: #sharp diff --git a/docs/api-input.md b/docs/api-input.md deleted file mode 100644 index 0b2509d1b..000000000 --- a/docs/api-input.md +++ /dev/null @@ -1,109 +0,0 @@ - - -## metadata - -Fast access to (uncached) image metadata without decoding any compressed image data. -A `Promise` is returned when `callback` is not provided. - -- `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg` -- `size`: Total size of image in bytes, for Stream and Buffer input only -- `width`: Number of pixels wide (EXIF orientation is not taken into consideration) -- `height`: Number of pixels high (EXIF orientation is not taken into consideration) -- `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...][1] -- `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK -- `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...][2] -- `density`: Number of pixels per inch (DPI), if present -- `chromaSubsampling`: String containing JPEG chroma subsampling, `4:2:0` or `4:4:4` for RGB, `4:2:0:4` or `4:4:4:4` for CMYK -- `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan -- `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP -- `pageHeight`: Number of pixels high each page in a multi-page image will be. -- `loop`: Number of times to loop an animated image, zero refers to a continuous loop. -- `delay`: Delay in ms between each page in an animated image, provided as an array of integers. -- `pagePrimary`: Number of the primary page in a HEIF image -- `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide -- `hasProfile`: Boolean indicating the presence of an embedded ICC profile -- `hasAlpha`: Boolean indicating the presence of an alpha transparency channel -- `orientation`: Number value of the EXIF Orientation header, if present -- `exif`: Buffer containing raw EXIF data, if present -- `icc`: Buffer containing raw [ICC][3] profile data, if present -- `iptc`: Buffer containing raw IPTC data, if present -- `xmp`: Buffer containing raw XMP data, if present -- `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present - -### Parameters - -- `callback` **[Function][4]?** called with the arguments `(err, metadata)` - -### Examples - -```javascript -const image = sharp(inputJpg); -image - .metadata() - .then(function(metadata) { - return image - .resize(Math.round(metadata.width / 2)) - .webp() - .toBuffer(); - }) - .then(function(data) { - // data contains a WebP image half the width and height of the original JPEG - }); -``` - -Returns **([Promise][5]<[Object][6]> | Sharp)** - -## stats - -Access to pixel-derived image statistics for every channel in the image. -A `Promise` is returned when `callback` is not provided. - -- `channels`: Array of channel statistics for each channel in the image. Each channel statistic contains - - `min` (minimum value in the channel) - - `max` (maximum value in the channel) - - `sum` (sum of all values in a channel) - - `squaresSum` (sum of squared values in a channel) - - `mean` (mean of the values in a channel) - - `stdev` (standard deviation for the values in a channel) - - `minX` (x-coordinate of one of the pixel where the minimum lies) - - `minY` (y-coordinate of one of the pixel where the minimum lies) - - `maxX` (x-coordinate of one of the pixel where the maximum lies) - - `maxY` (y-coordinate of one of the pixel where the maximum lies) -- `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque. -- `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental) -- `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any (experimental) -- `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram (experimental) - -### Parameters - -- `callback` **[Function][4]?** called with the arguments `(err, stats)` - -### Examples - -```javascript -const image = sharp(inputJpg); -image - .stats() - .then(function(stats) { - // stats contains the channel-wise statistics array and the isOpaque value - }); -``` - -```javascript -const { entropy, sharpness, dominant } = await sharp(input).stats(); -const { r, g, b } = dominant; -``` - -Returns **[Promise][5]<[Object][6]>** - -[1]: https://libvips.github.io/libvips/API/current/VipsImage.html#VipsInterpretation - -[2]: https://libvips.github.io/libvips/API/current/VipsImage.html#VipsBandFormat - -[3]: https://www.npmjs.com/package/icc - -[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function - -[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise - -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object diff --git a/docs/api-operation.md b/docs/api-operation.md deleted file mode 100644 index cfcea48eb..000000000 --- a/docs/api-operation.md +++ /dev/null @@ -1,409 +0,0 @@ - - -## rotate - -Rotate the output image by either an explicit angle -or auto-orient based on the EXIF `Orientation` tag. - -If an angle is provided, it is converted to a valid positive degree rotation. -For example, `-450` will produce a 270deg rotation. - -When rotating by an angle other than a multiple of 90, -the background colour can be provided with the `background` option. - -If no angle is provided, it is determined from the EXIF data. -Mirroring is supported and may infer the use of a flip operation. - -The use of `rotate` implies the removal of the EXIF `Orientation` tag, if any. - -Method order is important when both rotating and extracting regions, -for example `rotate(x).extract(y)` will produce a different result to `extract(y).rotate(x)`. - -### Parameters - -- `angle` **[number][1]** angle of rotation. (optional, default `auto`) -- `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"`) - -### Examples - -```javascript -const pipeline = sharp() - .rotate() - .resize(null, 200) - .toBuffer(function (err, outputBuffer, info) { - // outputBuffer contains 200px high JPEG image data, - // auto-rotated using EXIF Orientation tag - // info.width and info.height contain the dimensions of the resized image - }); -readableStream.pipe(pipeline); -``` - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## flip - -Flip the image about the vertical Y axis. This always occurs after rotation, if any. -The use of `flip` implies the removal of the EXIF `Orientation` tag, if any. - -### Parameters - -- `flip` **[Boolean][6]** (optional, default `true`) - -Returns **Sharp** - -## flop - -Flop the image about the horizontal X axis. This always occurs after rotation, if any. -The use of `flop` implies the removal of the EXIF `Orientation` tag, if any. - -### Parameters - -- `flop` **[Boolean][6]** (optional, default `true`) - -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. -When used without parameters, performs a fast, mild sharpen of the output image. -When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space. -Separate control over the level of sharpening in "flat" and "jagged" areas is available. - -### Parameters - -- `sigma` **[number][1]?** the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. -- `flat` **[number][1]** the level of sharpening to apply to "flat" areas. (optional, default `1.0`) -- `jagged` **[number][1]** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`) - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## median - -Apply median filter. -When used without parameters the default window is 3x3. - -### Parameters - -- `size` **[number][1]** square mask size: size x size (optional, default `3`) - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## blur - -Blur the image. -When used without parameters, performs a fast, mild blur of the output image. -When a `sigma` is provided, performs a slower, more accurate Gaussian blur. - -### Parameters - -- `sigma` **[number][1]?** a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## flatten - -Merge alpha transparency channel, if any, with a background, then remove the alpha channel. - -### Parameters - -- `options` **[Object][2]?** - - `options.background` **([string][3] \| [Object][2])** background colour, parsed by the [color][4] module, defaults to black. (optional, default `{r:0,g:0,b:0}`) - -### Examples - -```javascript -await sharp(rgbaInput) - .flatten('#F0A703') - .toBuffer(); -``` - -Returns **Sharp** - -## gamma - -Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of `1/gamma` -then increasing the encoding (brighten) post-resize at a factor of `gamma`. -This can improve the perceived brightness of a resized image in non-linear colour spaces. -JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation -when applying a gamma correction. - -Supply a second argument to use a different output gamma value, otherwise the first value is used in both cases. - -### Parameters - -- `gamma` **[number][1]** value between 1.0 and 3.0. (optional, default `2.2`) -- `gammaOut` **[number][1]?** value between 1.0 and 3.0. (optional, defaults to same as `gamma`) - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## negate - -Produce the "negative" of the image. - -### Parameters - -- `negate` **[Boolean][6]** (optional, default `true`) - -Returns **Sharp** - -## normalise - -Enhance output image contrast by stretching its luminance to cover the full dynamic range. - -### Parameters - -- `normalise` **[Boolean][6]** (optional, default `true`) - -Returns **Sharp** - -## normalize - -Alternative spelling of normalise. - -### Parameters - -- `normalize` **[Boolean][6]** (optional, default `true`) - -Returns **Sharp** - -## convolve - -Convolve the image with the specified kernel. - -### Parameters - -- `kernel` **[Object][2]** - - `kernel.width` **[number][1]** width of the kernel in pixels. - - `kernel.height` **[number][1]** width of the kernel in pixels. - - `kernel.kernel` **[Array][7]<[number][1]>** Array of length `width*height` containing the kernel values. - - `kernel.scale` **[number][1]** the scale of the kernel in pixels. (optional, default `sum`) - - `kernel.offset` **[number][1]** the offset of the kernel in pixels. (optional, default `0`) - -### Examples - -```javascript -sharp(input) - .convolve({ - width: 3, - height: 3, - kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1] - }) - .raw() - .toBuffer(function(err, data, info) { - // data contains the raw pixel data representing the convolution - // of the input image with the horizontal Sobel operator - }); -``` - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## threshold - -Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0. - -### Parameters - -- `threshold` **[number][1]** a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default `128`) -- `options` **[Object][2]?** - - `options.greyscale` **[Boolean][6]** convert to single channel greyscale. (optional, default `true`) - - `options.grayscale` **[Boolean][6]** alternative spelling for greyscale. (optional, default `true`) - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## boolean - -Perform a bitwise boolean operation with operand image. - -This operation creates an output image where each pixel is the result of -the selected bitwise boolean `operation` between the corresponding pixels of the input images. - -### Parameters - -- `operand` **([Buffer][8] \| [string][3])** Buffer containing image data or string containing the path to an image file. -- `operator` **[string][3]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively. -- `options` **[Object][2]?** - - `options.raw` **[Object][2]?** describes operand when using raw pixel data. - - `options.raw.width` **[number][1]?** - - `options.raw.height` **[number][1]?** - - `options.raw.channels` **[number][1]?** - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## linear - -Apply the linear formula a \* input + b to the image (levels adjustment) - -### Parameters - -- `a` **[number][1]** multiplier (optional, default `1.0`) -- `b` **[number][1]** offset (optional, default `0.0`) - - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -## recomb - -Recomb the image with the specified matrix. - -### Parameters - -- `inputMatrix` **[Array][7]<[Array][7]<[number][1]>>** 3x3 Recombination matrix - -### Examples - -```javascript -sharp(input) - .recomb([ - [0.3588, 0.7044, 0.1368], - [0.2990, 0.5870, 0.1140], - [0.2392, 0.4696, 0.0912], - ]) - .raw() - .toBuffer(function(err, data, info) { - // data contains the raw pixel data after applying the recomb - // With this example input, a sepia filter has been applied - }); -``` - -- Throws **[Error][5]** Invalid parameters - -Returns **Sharp** - -**Meta** - -- **since**: 0.21.1 - -## modulate - -Transforms the image using brightness, saturation and hue rotation. - -### Parameters - -- `options` **[Object][2]?** - - `options.brightness` **[number][1]?** Brightness multiplier - - `options.saturation` **[number][1]?** Saturation multiplier - - `options.hue` **[number][1]?** Degrees for hue rotation - -### Examples - -```javascript -sharp(input) - .modulate({ - brightness: 2 // increase lightness by a factor of 2 - }); - -sharp(input) - .modulate({ - hue: 180 // hue-rotate by 180 degrees - }); - -// decreate brightness and saturation while also hue-rotating by 90 degrees -sharp(input) - .modulate({ - brightness: 0.5, - saturation: 0.5, - hue: 90 - }); -``` - -Returns **Sharp** - -**Meta** - -- **since**: 0.22.1 - -[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number - -[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[4]: https://www.npmjs.org/package/color - -[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error - -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array - -[8]: https://nodejs.org/api/buffer.html diff --git a/docs/api-output.md b/docs/api-output.md deleted file mode 100644 index eb2cd8b86..000000000 --- a/docs/api-output.md +++ /dev/null @@ -1,470 +0,0 @@ - - -## toFile - -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, 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. -See [withMetadata][1] for control over this. - -A `Promise` is returned when `callback` is not provided. - -### Parameters - -- `fileOut` **[string][2]** the path to write the image data to. -- `callback` **[Function][3]?** called on completion with two arguments `(err, info)`. - `info` contains the output image `format`, `size` (bytes), `width`, `height`, - `channels` and `premultiplied` (indicating if premultiplication was used). - When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. - -### Examples - -```javascript -sharp(input) - .toFile('output.png', (err, info) => { ... }); -``` - -```javascript -sharp(input) - .toFile('output.png') - .then(info => { ... }) - .catch(err => { ... }); -``` - -- Throws **[Error][4]** Invalid parameters - -Returns **[Promise][5]<[Object][6]>** when no callback is provided - -## toBuffer - -Write output to a Buffer. -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. - -By default all metadata will be removed, which includes EXIF-based orientation. -See [withMetadata][1] for control over this. - -`callback`, if present, gets three arguments `(err, data, info)` where: - -- `err` is an error, if any. -- `data` is the output image data. -- `info` contains the output image `format`, `size` (bytes), `width`, `height`, - `channels` and `premultiplied` (indicating if premultiplication was used). - When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. - -A `Promise` is returned when `callback` is not provided. - -### Parameters - -- `options` **[Object][6]?** - - `options.resolveWithObject` **[boolean][7]?** Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`. -- `callback` **[Function][3]?** - -### Examples - -```javascript -sharp(input) - .toBuffer((err, data, info) => { ... }); -``` - -```javascript -sharp(input) - .toBuffer() - .then(data => { ... }) - .catch(err => { ... }); -``` - -```javascript -sharp(input) - .toBuffer({ resolveWithObject: true }) - .then(({ data, info }) => { ... }) - .catch(err => { ... }); -``` - -```javascript -const { data, info } = await sharp('my-image.jpg') - // output the raw pixels - .raw() - .toBuffer({ resolveWithObject: true }); - -// 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 -const { width, height, channels } = info; -await sharp(pixelArray, { raw: { width, height, channels } }) - .toFile('my-changed-image.jpg'); -``` - -Returns **[Promise][5]<[Buffer][8]>** when no callback is provided - -## withMetadata - -Include all metadata (EXIF, XMP, IPTC) from the input image in the output image. -This will also convert to and add a web-friendly sRGB ICC profile unless a custom -output profile is provided. - -The default behaviour, when `withMetadata` is not used, is to convert to the device-independent -sRGB colour space and strip all metadata, including the removal of any ICC profile. - -### Parameters - -- `options` **[Object][6]?** - - `options.orientation` **[number][9]?** value between 1 and 8, used to update the EXIF `Orientation` tag. - - `options.icc` **[string][2]?** filesystem path to output ICC profile, defaults to sRGB. - -### Examples - -```javascript -sharp('input.jpg') - .withMetadata() - .toFile('output-with-metadata.jpg') - .then(info => { ... }); -``` - -- Throws **[Error][4]** Invalid parameters - -Returns **Sharp** - -## toFormat - -Force output to a given format. - -### Parameters - -- `format` **([string][2] \| [Object][6])** as a string or an Object with an 'id' attribute -- `options` **[Object][6]** output options - -### Examples - -```javascript -// Convert any input to PNG output -const data = await sharp(input) - .toFormat('png') - .toBuffer(); -``` - -- Throws **[Error][4]** unsupported format or options - -Returns **Sharp** - -## jpeg - -Use these JPEG options for output image. - -### Parameters - -- `options` **[Object][6]?** output options - - `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`) - - `options.progressive` **[boolean][7]** use progressive (interlace) scan (optional, default `false`) - - `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling (optional, default `'4:2:0'`) - - `options.optimiseCoding` **[boolean][7]** optimise Huffman coding tables (optional, default `true`) - - `options.optimizeCoding` **[boolean][7]** alternative spelling of optimiseCoding (optional, default `true`) - - `options.mozjpeg` **[boolean][7]** use mozjpeg defaults, equivalent to `{ trellisQuantisation: true, overshootDeringing: true, optimiseScans: true, quantisationTable: 3 }` (optional, default `false`) - - `options.trellisQuantisation` **[boolean][7]** apply trellis quantisation (optional, default `false`) - - `options.overshootDeringing` **[boolean][7]** apply overshoot deringing (optional, default `false`) - - `options.optimiseScans` **[boolean][7]** optimise progressive scans, forces progressive (optional, default `false`) - - `options.optimizeScans` **[boolean][7]** alternative spelling of optimiseScans (optional, default `false`) - - `options.quantisationTable` **[number][9]** quantization table to use, integer 0-8 (optional, default `0`) - - `options.quantizationTable` **[number][9]** alternative spelling of quantisationTable (optional, default `0`) - - `options.force` **[boolean][7]** force JPEG output, otherwise attempt to use input format (optional, default `true`) - -### Examples - -```javascript -// Convert any input to very high quality JPEG output -const data = await sharp(input) - .jpeg({ - quality: 100, - chromaSubsampling: '4:4:4' - }) - .toBuffer(); -``` - -```javascript -// Use mozjpeg to reduce output JPEG file size (slower) -const data = await sharp(input) - .jpeg({ mozjpeg: true }) - .toBuffer(); -``` - -- Throws **[Error][4]** Invalid options - -Returns **Sharp** - -## png - -Use these PNG options for output image. - -By default, PNG output is full colour at 8 or 16 bits per pixel. -Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel. -Set `palette` to `true` for slower, indexed PNG output. - -### Parameters - -- `options` **[Object][6]?** - - `options.progressive` **[boolean][7]** use progressive (interlace) scan (optional, default `false`) - - `options.compressionLevel` **[number][9]** zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest) (optional, default `6`) - - `options.adaptiveFiltering` **[boolean][7]** use adaptive row filtering (optional, default `false`) - - `options.palette` **[boolean][7]** quantise to a palette-based image with alpha transparency support (optional, default `false`) - - `options.quality` **[number][9]** use the lowest number of colours needed to achieve given quality, sets `palette` to `true` (optional, default `100`) - - `options.colours` **[number][9]** maximum number of palette entries, sets `palette` to `true` (optional, default `256`) - - `options.colors` **[number][9]** alternative spelling of `options.colours`, sets `palette` to `true` (optional, default `256`) - - `options.dither` **[number][9]** level of Floyd-Steinberg error diffusion, sets `palette` to `true` (optional, default `1.0`) - - `options.force` **[boolean][7]** force PNG output, otherwise attempt to use input format (optional, default `true`) - -### Examples - -```javascript -// Convert any input to full colour PNG output -const data = await sharp(input) - .png() - .toBuffer(); -``` - -```javascript -// Convert any input to indexed PNG output (slower) -const data = await sharp(input) - .png({ palette: true }) - .toBuffer(); -``` - -- Throws **[Error][4]** Invalid options - -Returns **Sharp** - -## webp - -Use these WebP options for output image. - -### Parameters - -- `options` **[Object][6]?** output options - - `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`) - - `options.alphaQuality` **[number][9]** quality of alpha layer, integer 0-100 (optional, default `100`) - - `options.lossless` **[boolean][7]** use lossless compression mode (optional, default `false`) - - `options.nearLossless` **[boolean][7]** use near_lossless compression mode (optional, default `false`) - - `options.smartSubsample` **[boolean][7]** use high quality chroma subsampling (optional, default `false`) - - `options.reductionEffort` **[number][9]** level of CPU effort to reduce file size, integer 0-6 (optional, default `4`) - - `options.pageHeight` **[number][9]?** page height for animated output - - `options.loop` **[number][9]** number of animation iterations, use 0 for infinite animation (optional, default `0`) - - `options.delay` **[Array][10]<[number][9]>?** list of delays between animation frames (in milliseconds) - - `options.force` **[boolean][7]** force WebP output, otherwise attempt to use input format (optional, default `true`) - -### Examples - -```javascript -// Convert any input to lossless WebP output -const data = await sharp(input) - .webp({ lossless: true }) - .toBuffer(); -``` - -- Throws **[Error][4]** Invalid options - -Returns **Sharp** - -## gif - -Use these GIF options for output image. - -Requires libvips compiled with support for ImageMagick or GraphicsMagick. -The prebuilt binaries do not include this - see -[installing a custom libvips][11]. - -### Parameters - -- `options` **[Object][6]?** output options - - `options.pageHeight` **[number][9]?** page height for animated output - - `options.loop` **[number][9]** number of animation iterations, use 0 for infinite animation (optional, default `0`) - - `options.delay` **[Array][10]<[number][9]>?** list of delays between animation frames (in milliseconds) - - `options.force` **[boolean][7]** force GIF output, otherwise attempt to use input format (optional, default `true`) - - -- Throws **[Error][4]** Invalid options - -Returns **Sharp** - -## tiff - -Use these TIFF options for output image. - -### Parameters - -- `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` **[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` **[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` **[number][9]** reduce bitdepth to 1, 2 or 4 bit (optional, default `8`) - -### Examples - -```javascript -// Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output -sharp('input.svg') - .tiff({ - compression: 'lzw', - bitdepth: 1 - }) - .toFile('1-bpp-output.tiff') - .then(info => { ... }); -``` - -- Throws **[Error][4]** Invalid options - -Returns **Sharp** - -## avif - -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. - -### Parameters - -- `options` **[Object][6]?** output options - - `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 - -Returns **Sharp** - -**Meta** - -- **since**: 0.23.0 - -## raw - -Force output to be raw, uncompressed, 8-bit unsigned integer (unit8) pixel data. -Pixel ordering is left-to-right, top-to-bottom, without padding. -Channel ordering will be RGB or RGBA for non-greyscale colourspaces. - -### Examples - -```javascript -// Extract raw RGB pixel data from JPEG input -const { data, info } = await sharp('input.jpg') - .raw() - .toBuffer({ resolveWithObject: true }); -``` - -```javascript -// Extract alpha channel as raw pixel data from PNG input -const data = await sharp('input.png') - .ensureAlpha() - .extractChannel(3) - .toColourspace('b-w') - .raw() - .toBuffer(); -``` - -Returns **Sharp** - -## tile - -Use tile-based deep zoom (image pyramid) output. -Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions. -Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format. - -Warning: multiple sharp instances concurrently producing tile output can expose a possible race condition in some versions of libgsf. - -### Parameters - -- `options` **[Object][6]?** - - `options.size` **[number][9]** tile size in pixels, a value between 1 and 8192. (optional, default `256`) - - `options.overlap` **[number][9]** tile overlap in pixels, a value between 0 and 8192. (optional, default `0`) - - `options.angle` **[number][9]** tile angle of rotation, must be a multiple of 90. (optional, default `0`) - - `options.background` **([string][2] \| [Object][6])** background colour, parsed by the [color][12] module, defaults to white without transparency. (optional, default `{r:255,g:255,b:255,alpha:1}`) - - `options.depth` **[string][2]?** how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout. - - `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`) - - `options.id` **[string][2]** when `layout` is `iiif`, sets the `@id` attribute of `info.json` (optional, default `'https://example.com/iiif'`) - -### Examples - -```javascript -sharp('input.tiff') - .png() - .tile({ - size: 512 - }) - .toFile('output.dz', function(err, info) { - // output.dzi is the Deep Zoom XML definition - // output_files contains 512x512 tiles grouped by zoom level - }); -``` - -- Throws **[Error][4]** Invalid parameters - -Returns **Sharp** - -[1]: #withmetadata - -[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function - -[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error - -[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise - -[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[8]: https://nodejs.org/api/buffer.html - -[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number - -[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array - -[11]: https://sharp.pixelplumbing.com/install#custom-libvips - -[12]: https://www.npmjs.org/package/color diff --git a/docs/api-resize.md b/docs/api-resize.md deleted file mode 100644 index 5fac17f02..000000000 --- a/docs/api-resize.md +++ /dev/null @@ -1,258 +0,0 @@ - - -## resize - -Resize image to `width`, `height` or `width x height`. - -When both a `width` and `height` are provided, the possible methods by which the image should **fit** these are: - -- `cover`: (default) Preserving aspect ratio, ensure the image covers both provided dimensions by cropping/clipping to fit. -- `contain`: Preserving aspect ratio, contain within both provided dimensions using "letterboxing" where necessary. -- `fill`: Ignore the aspect ratio of the input and stretch to both provided dimensions. -- `inside`: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified. -- `outside`: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified. - -Some of these values are based on the [object-fit][1] CSS property. - -When using a `fit` of `cover` or `contain`, the default **position** is `centre`. Other options are: - -- `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`. -- `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`. -- `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy. - -Some of these values are based on the [object-position][2] CSS property. - -The experimental strategy-based approach resizes so one dimension is at its target length -then repeatedly ranks edge regions, discarding the edge with the lowest score based on the selected strategy. - -- `entropy`: focus on the region with the highest [Shannon entropy][3]. -- `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones. - -Possible interpolation kernels are: - -- `nearest`: Use [nearest neighbour interpolation][4]. -- `cubic`: Use a [Catmull-Rom spline][5]. -- `mitchell`: Use a [Mitchell-Netravali spline][6]. -- `lanczos2`: Use a [Lanczos kernel][7] with `a=2`. -- `lanczos3`: Use a Lanczos kernel with `a=3` (the default). - -### Parameters - -- `width` **[number][8]?** pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. -- `height` **[number][8]?** pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. -- `options` **[Object][9]?** - - `options.width` **[String][10]?** alternative means of specifying `width`. If both are present this take priority. - - `options.height` **[String][10]?** alternative means of specifying `height`. If both are present this take priority. - - `options.fit` **[String][10]** how the image should be resized to fit both provided dimensions, one of `cover`, `contain`, `fill`, `inside` or `outside`. (optional, default `'cover'`) - - `options.position` **[String][10]** position, gravity or strategy to use when `fit` is `cover` or `contain`. (optional, default `'centre'`) - - `options.background` **([String][10] \| [Object][9])** background colour when using a `fit` of `contain`, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`) - - `options.kernel` **[String][10]** the kernel to use for image reduction. (optional, default `'lanczos3'`) - - `options.withoutEnlargement` **[Boolean][12]** do not enlarge if the width _or_ height are already less than the specified dimensions, equivalent to GraphicsMagick's `>` geometry option. (optional, default `false`) - - `options.fastShrinkOnLoad` **[Boolean][12]** take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default `true`) - -### Examples - -```javascript -sharp(input) - .resize({ width: 100 }) - .toBuffer() - .then(data => { - // 100 pixels wide, auto-scaled height - }); -``` - -```javascript -sharp(input) - .resize({ height: 100 }) - .toBuffer() - .then(data => { - // 100 pixels high, auto-scaled width - }); -``` - -```javascript -sharp(input) - .resize(200, 300, { - kernel: sharp.kernel.nearest, - fit: 'contain', - position: 'right top', - background: { r: 255, g: 255, b: 255, alpha: 0.5 } - }) - .toFile('output.png') - .then(() => { - // output.png is a 200 pixels wide and 300 pixels high image - // containing a nearest-neighbour scaled version - // contained within the north-east corner of a semi-transparent white canvas - }); -``` - -```javascript -const transformer = sharp() - .resize({ - width: 200, - height: 200, - fit: sharp.fit.cover, - position: sharp.strategy.entropy - }); -// Read image data from readableStream -// Write 200px square auto-cropped image data to writableStream -readableStream - .pipe(transformer) - .pipe(writableStream); -``` - -```javascript -sharp(input) - .resize(200, 200, { - fit: sharp.fit.inside, - withoutEnlargement: true - }) - .toFormat('jpeg') - .toBuffer() - .then(function(outputBuffer) { - // outputBuffer contains JPEG image data - // no wider and no higher than 200 pixels - // and no larger than the input image - }); -``` - -```javascript -const scaleByHalf = await sharp(input) - .metadata() - .then(({ width }) => sharp(input) - .resize(Math.round(width * 0.5)) - .toBuffer() - ); -``` - -- Throws **[Error][13]** Invalid parameters - -Returns **Sharp** - -## extend - -Extends/pads the edges of the image with the provided background colour. -This operation will always occur after resizing and extraction, if any. - -### Parameters - -- `extend` **([number][8] \| [Object][9])** single pixel count to add to all edges or an Object with per-edge counts - - `extend.top` **[number][8]** (optional, default `0`) - - `extend.left` **[number][8]** (optional, default `0`) - - `extend.bottom` **[number][8]** (optional, default `0`) - - `extend.right` **[number][8]** (optional, default `0`) - - `extend.background` **([String][10] \| [Object][9])** background colour, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`) - -### Examples - -```javascript -// Resize to 140 pixels wide, then add 10 transparent pixels -// to the top, left and right edges and 20 to the bottom edge -sharp(input) - .resize(140) - .extend({ - top: 10, - bottom: 20, - left: 10, - right: 10, - background: { r: 0, g: 0, b: 0, alpha: 0 } - }) - ... -``` - -```javascript -// Add a row of 10 red pixels to the bottom -sharp(input) - .extend({ - bottom: 10, - background: 'red' - }) - ... -``` - -- Throws **[Error][13]** Invalid parameters - -Returns **Sharp** - -## extract - -Extract/crop a region of the image. - -- Use `extract` before `resize` for pre-resize extraction. -- Use `extract` after `resize` for post-resize extraction. -- Use `extract` before and after for both. - -### Parameters - -- `options` **[Object][9]** describes the region to extract using integral pixel values - - `options.left` **[number][8]** zero-indexed offset from left edge - - `options.top` **[number][8]** zero-indexed offset from top edge - - `options.width` **[number][8]** width of region to extract - - `options.height` **[number][8]** height of region to extract - -### Examples - -```javascript -sharp(input) - .extract({ left: left, top: top, width: width, height: height }) - .toFile(output, function(err) { - // Extract a region of the input image, saving in the same format. - }); -``` - -```javascript -sharp(input) - .extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre }) - .resize(width, height) - .extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost }) - .toFile(output, function(err) { - // Extract a region, resize, then extract from the resized image - }); -``` - -- Throws **[Error][13]** Invalid parameters - -Returns **Sharp** - -## trim - -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, obtained from callback of `.toFile()` or `.toBuffer()`, -will contain `trimOffsetLeft` and `trimOffsetTop` properties. - -### Parameters - -- `threshold` **[number][8]** the allowed difference from the top-left pixel, a number greater than zero. (optional, default `10`) - - -- Throws **[Error][13]** Invalid parameters - -Returns **Sharp** - -[1]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit - -[2]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-position - -[3]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29 - -[4]: http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation - -[5]: https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline - -[6]: https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf - -[7]: https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel - -[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number - -[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[11]: https://www.npmjs.org/package/color - -[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[13]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error diff --git a/docs/api-utility.md b/docs/api-utility.md deleted file mode 100644 index 01a0bd3c8..000000000 --- a/docs/api-utility.md +++ /dev/null @@ -1,189 +0,0 @@ - - -## format - -An Object containing nested boolean values representing the available input and output formats/methods. - -### Examples - -```javascript -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. - -### Examples - -```javascript -console.log(sharp.versions); -``` - -## cache - -Gets or, when options are provided, sets the limits of _libvips'_ operation cache. -Existing entries in the cache will be trimmed after any change in limits. -This method always returns cache statistics, -useful for determining how much working memory is required for a particular task. - -### Parameters - -- `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 - -```javascript -const stats = sharp.cache(); -``` - -```javascript -sharp.cache( { items: 200 } ); -sharp.cache( { files: 0 } ); -sharp.cache(false); -``` - -Returns **[Object][1]** - -## concurrency - -Gets or, when a concurrency is provided, sets -the number of threads _libvips'_ should create to process each image. - -The default value is the number of CPU cores, -except when using glibc-based Linux without jemalloc, -where the default is `1` to help reduce memory fragmentation. - -A value of `0` will reset this to the number of CPU cores. - -The maximum number of images that can be processed in parallel -is limited by libuv's `UV_THREADPOOL_SIZE` environment variable. - -This method always returns the current concurrency. - -### Parameters - -- `concurrency` **[number][11]?** - -### Examples - -```javascript -const threads = sharp.concurrency(); // 4 -sharp.concurrency(2); // 2 -sharp.concurrency(0); // 4 -``` - -Returns **[number][11]** concurrency - -## queue - -An EventEmitter that emits a `change` event when a task is either: - -- queued, waiting for _libuv_ to provide a worker thread -- complete - -### Examples - -```javascript -sharp.queue.on('change', function(queueLength) { - console.log('Queue contains ' + queueLength + ' task(s)'); -}); -``` - -## counters - -Provides access to internal task counters. - -- queue is the number of tasks this module has queued waiting for _libuv_ to provide a worker thread from its pool. -- process is the number of resize tasks currently being processed. - -### Examples - -```javascript -const counters = sharp.counters(); // { queue: 2, process: 4 } -``` - -Returns **[Object][1]** - -## simd - -Get and set use of SIMD vector unit instructions. -Requires libvips to have been compiled with liborc support. - -Improves the performance of `resize`, `blur` and `sharpen` operations -by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM NEON. - -### Parameters - -- `simd` **[boolean][10]** (optional, default `true`) - -### Examples - -```javascript -const simd = sharp.simd(); -// simd is `true` if the runtime use of liborc is currently enabled -``` - -```javascript -const simd = sharp.simd(false); -// prevent libvips from using liborc at runtime -``` - -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/String - -[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 diff --git a/docs/astro.config.mjs b/docs/astro.config.mjs new file mode 100644 index 000000000..2209c3381 --- /dev/null +++ b/docs/astro.config.mjs @@ -0,0 +1,90 @@ +// @ts-check +import starlight from '@astrojs/starlight'; +import { defineConfig } from 'astro/config'; +import starlightAutoSidebar from 'starlight-auto-sidebar'; + +import { version } from '../package.json'; + +export default defineConfig({ + site: 'https://sharp.pixelplumbing.com', + integrations: [ + starlight({ + title: 'sharp', + description: + 'High performance Node.js image processing. The fastest module to resize JPEG, PNG, WebP and TIFF images.', + logo: { + src: './src/assets/sharp-logo.svg', + alt: '#' + }, + customCss: ['./src/styles/custom.css'], + head: [{ + tag: 'meta', + attrs: { + 'http-equiv': 'Content-Security-Policy', + content: "default-src 'self'; connect-src 'self'; object-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://static.cloudflareinsights.com/beacon.min.js/;" + } + }, { + tag: 'link', + attrs: { + rel: 'author', + href: '/humans.txt', + type: 'text/plain' + } + }, { + tag: 'script', + attrs: { + type: 'application/ld+json' + }, + content: JSON.stringify({ + '@context': 'https://schema.org', + '@type': 'SoftwareSourceCode', + name: 'sharp', + description: 'High performance Node.js image processing', + url: 'https://sharp.pixelplumbing.com', + codeRepository: 'https://github.com/lovell/sharp', + programmingLanguage: ['JavaScript', 'C++'], + runtimePlatform: 'Node.js', + copyrightHolder: { + '@context': 'https://schema.org', + '@type': 'Person', + name: 'Lovell Fuller' + }, + copyrightYear: 2013, + license: 'https://www.apache.org/licenses/LICENSE-2.0' + }) + }], + sidebar: [ + { label: 'Home', link: '/' }, + { label: 'Installation', slug: 'install' }, + { + label: 'API', + items: [ + { label: 'Constructor', slug: 'api-constructor' }, + { label: 'Input metadata', slug: 'api-input' }, + { label: 'Output options', slug: 'api-output' }, + { label: 'Resizing images', slug: 'api-resize' }, + { label: 'Compositing images', slug: 'api-composite' }, + { label: 'Image operations', slug: 'api-operation' }, + { label: 'Colour manipulation', slug: 'api-colour' }, + { label: 'Channel manipulation', slug: 'api-channel' }, + { label: 'Global properties', slug: 'api-utility' } + ] + }, + { label: 'Performance', slug: 'performance' }, + { + label: 'Changelog', + collapsed: true, + autogenerate: { directory: 'changelog' } + } + ], + social: [ + { icon: 'openCollective', label: 'Open Collective', href: 'https://opencollective.com/libvips' }, + { icon: 'github', label: 'GitHub', href: 'https://github.com/lovell/sharp' } + ], + plugins: [starlightAutoSidebar()] + }) + ], + redirects: { + '/changelog': `/changelog/v${version}` + } +}); diff --git a/docs/build.js b/docs/build.js deleted file mode 100644 index 325138dfb..000000000 --- a/docs/build.js +++ /dev/null @@ -1,25 +0,0 @@ -'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); -}); diff --git a/docs/build.mjs b/docs/build.mjs new file mode 100644 index 000000000..da43a5bf5 --- /dev/null +++ b/docs/build.mjs @@ -0,0 +1,42 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +import fs from 'node:fs/promises'; +import path from 'node:path'; +import jsdoc2md from 'jsdoc-to-markdown'; + +const pages = { + constructor: 'Constructor', + input: 'Input metadata', + resize: 'Resizing images', + composite: 'Compositing images', + operation: 'Image operations', + colour: 'Colour manipulation', + channel: 'Channel manipulation', + output: 'Output options', + utility: 'Global properties' +}; + +Object.keys(pages).forEach(async (m) => { + const input = path.join('lib', `${m}.js`); + const output = path.join('docs', 'src', 'content', 'docs', `api-${m}.md`); + + const ast = await jsdoc2md.getTemplateData({ files: input }); + const markdown = await jsdoc2md.render({ + data: ast, + 'global-index-format': 'none', + 'module-index-format': 'none' + }); + + const cleanMarkdown = + `---\n# This file was auto-generated from JSDoc in lib/${m}.js\ntitle: ${pages[m]}\n---\n\n` + + markdown + .replace(/(## )([A-Za-z0-9]+)([^\n]*)/g, '$1$2\n> $2$3\n') // simplify headings + .replace(/<\/a>/g, '') // remove anchors + .replace(/\*\*Kind\*\*: global[^\n]+/g, '') // remove all "global" Kind labels (requires JSDoc refactoring) + .trim(); + + await fs.writeFile(output, cleanMarkdown); +}); diff --git a/docs/changelog.md b/docs/changelog.md deleted file mode 100644 index b87ea5e46..000000000 --- a/docs/changelog.md +++ /dev/null @@ -1,1310 +0,0 @@ -# Changelog - -## v0.28 - *bijou* - -Requires libvips v8.10.6 - -### v0.28.0 - 29th March 2021 - -* Prebuilt binaries now include mozjpeg and libimagequant (BSD 2-Clause). - -* Prebuilt binaries limit AVIF support to the most common 8-bit depth. - -* Add `mozjpeg` option to `jpeg` method, sets mozjpeg defaults. - -* Reduce the default PNG `compressionLevel` to the more commonly used 6. - -* Reduce concurrency on glibc-based Linux when using the default memory allocator to help prevent fragmentation. - -* Default missing edge properties of extend operation to zero. - [#2578](https://github.com/lovell/sharp/issues/2578) - -* Ensure composite does not clip top and left offsets. - [#2594](https://github.com/lovell/sharp/pull/2594) - [@SHG42](https://github.com/SHG42) - -* Improve error handling of network failure at install time. - [#2608](https://github.com/lovell/sharp/pull/2608) - [@abradley](https://github.com/abradley) - -* Ensure `@id` attribute can be set for IIIF tile-based output. - [#2612](https://github.com/lovell/sharp/issues/2612) - [@edsilv](https://github.com/edsilv) - -* Ensure composite replicates the correct number of tiles for centred gravities. - [#2626](https://github.com/lovell/sharp/issues/2626) - -## 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+. - -* TIFF output `squash` is replaced by `bitdepth` to reduce to 1, 2 or 4 bit. - -* JPEG output `quality` >= 90 no longer automatically sets `chromaSubsampling` to `4:4:4`. - -* Add most `dominant` colour to image `stats`. - [#640](https://github.com/lovell/sharp/issues/640) - -* Add support for animated GIF (requires \*magick) and WebP output. - [#2012](https://github.com/lovell/sharp/pull/2012) - [@deftomat](https://github.com/deftomat) - -* Add support for libvips ImageMagick v7 loaders. - [#2258](https://github.com/lovell/sharp/pull/2258) - [@vouillon](https://github.com/vouillon) - -* Allow multi-page input via \*magick. - [#2259](https://github.com/lovell/sharp/pull/2259) - [@vouillon](https://github.com/vouillon) - -* Add support to `withMetadata` for custom ICC profile. - [#2271](https://github.com/lovell/sharp/pull/2271) - [@roborourke](https://github.com/roborourke) - -* Ensure prebuilt binaries for ARM default to v7 when using Electron. - [#2292](https://github.com/lovell/sharp/pull/2292) - [@diegodev3](https://github.com/diegodev3) - -## v0.25 - *yield* - -Requires libvips v8.9.1 - -### v0.25.4 - 12th June 2020 - -* Allow libvips binary location override where version is appended. - [#2217](https://github.com/lovell/sharp/pull/2217) - [@malice00](https://github.com/malice00) - -* Enable PNG palette when setting quality, colours, colors or dither. - [#2226](https://github.com/lovell/sharp/pull/2226) - [@romaleev](https://github.com/romaleev) - -* Add `level` constructor option to use a specific level of a multi-level image. - Expose `levels` metadata for multi-level images. - [#2222](https://github.com/lovell/sharp/issues/2222) - -* Add support for named `alpha` channel to `extractChannel` operation. - [#2138](https://github.com/lovell/sharp/issues/2138) - -* Add experimental `sharpness` calculation to `stats()` response. - [#2251](https://github.com/lovell/sharp/issues/2251) - -* Emit `warning` event for non-critical processing problems. - [#2032](https://github.com/lovell/sharp/issues/2032) - -### v0.25.3 - 17th May 2020 - -* Ensure libvips is initialised only once, improves worker thread safety. - [#2143](https://github.com/lovell/sharp/issues/2143) - -* Ensure npm platform flag is respected when copying DLLs. - [#2188](https://github.com/lovell/sharp/pull/2188) - [@dimadeveatii](https://github.com/dimadeveatii) - -* Allow SVG input with large inline images to be parsed. - [#2195](https://github.com/lovell/sharp/issues/2195) - -### v0.25.2 - 20th March 2020 - -* Provide prebuilt binaries for Linux ARM64v8. - -* Add IIIF layout support to tile-based output. - [#2098](https://github.com/lovell/sharp/pull/2098) - [@edsilv](https://github.com/edsilv) - -* Ensure input options are consistently and correctly detected. - [#2118](https://github.com/lovell/sharp/issues/2118) - -* Ensure N-API prebuilt binaries work on RHEL7 and its derivatives. - [#2119](https://github.com/lovell/sharp/issues/2119) - -* Ensure AsyncWorker options are persisted. - [#2130](https://github.com/lovell/sharp/issues/2130) - -### v0.25.1 - 7th March 2020 - -* Ensure prebuilt binaries are fetched based on N-API version. - [#2117](https://github.com/lovell/sharp/issues/2117) - -### v0.25.0 - 7th March 2020 - -* Remove `limitInputPixels` and `sequentialRead` previously deprecated in v0.24.0. - -* Migrate internals to N-API. - [#1282](https://github.com/lovell/sharp/issues/1282) - -* Add support for 32-bit Windows. - [#2088](https://github.com/lovell/sharp/issues/2088) - -* Ensure correct ordering of rotate-then-trim operations. - [#2087](https://github.com/lovell/sharp/issues/2087) - -* Ensure composite accepts `limitInputPixels` and `sequentialRead` input options. - [#2099](https://github.com/lovell/sharp/issues/2099) - -## v0.24 - "*wit*" - -Requires libvips v8.9.0. - -### v0.24.1 - 15th February 2020 - -* Prevent use of sequentialRead for EXIF-based rotate operation. - [#2042](https://github.com/lovell/sharp/issues/2042) - -* Ensure RGBA LZW TIFF returns correct channel count. - [#2064](https://github.com/lovell/sharp/issues/2064) - -### v0.24.0 - 16th January 2020 - -* Drop support for Node.js 8. - [#1910](https://github.com/lovell/sharp/issues/1910) - -* Drop support for undefined input where options also provided. - [#1768](https://github.com/lovell/sharp/issues/1768) - -* Move `limitInputPixels` and `sequentialRead` to input options, deprecating functions of the same name. - -* Expose `delay` and `loop` metadata for animated images. - [#1905](https://github.com/lovell/sharp/issues/1905) - -* Ensure correct colour output for 16-bit, 2-channel PNG input with ICC profile. - [#2013](https://github.com/lovell/sharp/issues/2013) - -* Prevent use of sequentialRead for rotate operations. - [#2016](https://github.com/lovell/sharp/issues/2016) - -* Correctly bind max width and height values when using withoutEnlargement. - [#2024](https://github.com/lovell/sharp/pull/2024) - [@BrychanOdlum](https://github.com/BrychanOdlum) - -* Add support for input with 16-bit RGB profile. - [#2037](https://github.com/lovell/sharp/issues/2037) - -## v0.23 - "*vision*" - -Requires libvips v8.8.1. - -### v0.23.4 - 5th December 2019 - -* Handle zero-length Buffer objects when using Node.js v13.2.0+. - -* Expose raw TIFFTAG_PHOTOSHOP metadata. - [#1600](https://github.com/lovell/sharp/issues/1600) - -* Improve thread safety by using copy-on-write when updating metadata. - [#1986](https://github.com/lovell/sharp/issues/1986) - -### v0.23.3 - 17th November 2019 - -* Ensure `trim` operation supports images contained in the alpha channel. - [#1597](https://github.com/lovell/sharp/issues/1597) - -* Ensure tile `overlap` option works as expected. - [#1921](https://github.com/lovell/sharp/pull/1921) - [@rustyguts](https://github.com/rustyguts) - -* Allow compilation on FreeBSD and variants (broken since v0.23.0) - [#1952](https://github.com/lovell/sharp/pull/1952) - [@pouya-eghbali](https://github.com/pouya-eghbali) - -* Ensure `modulate` and other colour-based operations can co-exist. - [#1958](https://github.com/lovell/sharp/issues/1958) - -### v0.23.2 - 28th October 2019 - -* Add `background` option to tile output operation. - [#1924](https://github.com/lovell/sharp/pull/1924) - [@neave](https://github.com/neave) - -* Add support for Node.js 13. - [#1932](https://github.com/lovell/sharp/pull/1932) - [@MayhemYDG](https://github.com/MayhemYDG) - -### v0.23.1 - 26th September 2019 - -* Ensure `sharp.format.vips` is present and correct (filesystem only). - [#1813](https://github.com/lovell/sharp/issues/1813) - -* Ensure invalid `width` and `height` provided as options to `resize` throw. - [#1817](https://github.com/lovell/sharp/issues/1817) - -* Allow use of 'heic' and 'heif' identifiers with `toFormat`. - [#1834](https://github.com/lovell/sharp/pull/1834) - [@jaubourg](https://github.com/jaubourg) - -* Add `premultiplied` option to `composite` operation. - [#1835](https://github.com/lovell/sharp/pull/1835) - [@Andargor](https://github.com/Andargor) - -* Allow instance reuse with differing `toBuffer` options. - [#1860](https://github.com/lovell/sharp/pull/1860) - [@RaboliotTheGrey](https://github.com/RaboliotTheGrey) - -* Ensure image is at least 3x3 pixels before attempting trim operation. - -### v0.23.0 - 29th July 2019 - -* Remove `overlayWith` previously deprecated in v0.22.0. - -* Add experimental support for HEIF images. Requires libvips compiled with libheif. - [#1105](https://github.com/lovell/sharp/issues/1105) - -* Expose libwebp `smartSubsample` and `reductionEffort` options. - [#1545](https://github.com/lovell/sharp/issues/1545) - -* Add experimental support for Worker Threads. - [#1558](https://github.com/lovell/sharp/issues/1558) - -* Use libvips' built-in CMYK and sRGB profiles when required. - [#1619](https://github.com/lovell/sharp/issues/1619) - -* Drop support for Node.js versions 6 and 11. - [#1674](https://github.com/lovell/sharp/issues/1674) - -* Expose `skipBlanks` option for tile-based output. - [#1687](https://github.com/lovell/sharp/pull/1687) - [@RaboliotTheGrey](https://github.com/RaboliotTheGrey) - -* Allow use of `failOnError` option with Stream-based input. - [#1691](https://github.com/lovell/sharp/issues/1691) - -* Fix rotate/extract ordering for non-90 angles. - [#1755](https://github.com/lovell/sharp/pull/1755) - [@iovdin](https://github.com/iovdin) - -## v0.22 - "*uptake*" - -Requires libvips v8.7.4. - -### v0.22.1 - 25th April 2019 - -* Add `modulate` operation for brightness, saturation and hue. - [#1601](https://github.com/lovell/sharp/pull/1601) - [@Goues](https://github.com/Goues) - -* Improve help messaging should `require("sharp")` fail. - [#1638](https://github.com/lovell/sharp/pull/1638) - [@sidharthachatterjee](https://github.com/sidharthachatterjee) - -* Add support for Node 12. - [#1668](https://github.com/lovell/sharp/issues/1668) - -### v0.22.0 - 18th March 2019 - -* Remove functions previously deprecated in v0.21.0: - `background`, `crop`, `embed`, `ignoreAspectRatio`, `max`, `min` and `withoutEnlargement`. - -* Add `composite` operation supporting multiple images and blend modes; deprecate `overlayWith`. - [#728](https://github.com/lovell/sharp/issues/728) - -* Add support for `pages` input option for multi-page input. - [#1566](https://github.com/lovell/sharp/issues/1566) - -* Allow Stream-based input of raw pixel data. - [#1579](https://github.com/lovell/sharp/issues/1579) - -* Add support for `page` input option to GIF and PDF. - [#1595](https://github.com/lovell/sharp/pull/1595) - [@ramiel](https://github.com/ramiel) - -## v0.21 - "*teeth*" - -Requires libvips v8.7.0. - -### v0.21.3 - 19th January 2019 - -* Input image decoding now fails fast, set `failOnError` to change this behaviour. - -* Failed filesystem-based input now separates missing file and invalid format errors. - [#1542](https://github.com/lovell/sharp/issues/1542) - -### v0.21.2 - 13th January 2019 - -* Ensure all metadata is removed from PNG output unless `withMetadata` used. - -* Ensure shortest edge is at least one pixel after resizing. - [#1003](https://github.com/lovell/sharp/issues/1003) - -* Add `ensureAlpha` operation to add an alpha channel, if missing. - [#1153](https://github.com/lovell/sharp/issues/1153) - -* Expose `pages` and `pageHeight` metadata for multi-page input images. - [#1205](https://github.com/lovell/sharp/issues/1205) - -* Expose PNG output options requiring libimagequant. - [#1484](https://github.com/lovell/sharp/issues/1484) - -* Expose underlying error message for invalid input. - [#1505](https://github.com/lovell/sharp/issues/1505) - -* Prevent mutatation of options passed to `jpeg`. - [#1516](https://github.com/lovell/sharp/issues/1516) - -* Ensure forced output format applied correctly when output chaining. - [#1528](https://github.com/lovell/sharp/issues/1528) - -### v0.21.1 - 7th December 2018 - -* Install: support `sharp_dist_base_url` npm config, like existing `SHARP_DIST_BASE_URL`. - [#1422](https://github.com/lovell/sharp/pull/1422) - [@SethWen](https://github.com/SethWen) - -* Ensure `channel` metadata is correct for raw, greyscale output. - [#1425](https://github.com/lovell/sharp/issues/1425) - -* Add support for the "mitchell" kernel for image reductions. - [#1438](https://github.com/lovell/sharp/pull/1438) - [@Daiz](https://github.com/Daiz) - -* Allow separate parameters for gamma encoding and decoding. - [#1439](https://github.com/lovell/sharp/pull/1439) - [@Daiz](https://github.com/Daiz) - -* Build prototype with `Object.assign` to allow minification. - [#1475](https://github.com/lovell/sharp/pull/1475) - [@jaubourg](https://github.com/jaubourg) - -* Expose libvips' recombination matrix operation. - [#1477](https://github.com/lovell/sharp/pull/1477) - [@fromkeith](https://github.com/fromkeith) - -* Expose libvips' pyramid/tile options for TIFF output. - [#1483](https://github.com/lovell/sharp/pull/1483) - [@mbklein](https://github.com/mbklein) - -### v0.21.0 - 4th October 2018 - -* Deprecate the following resize-related functions: - `crop`, `embed`, `ignoreAspectRatio`, `max`, `min` and `withoutEnlargement`. - Access to these is now via options passed to the `resize` function. - For example: - `embed('north')` is now `resize(width, height, { fit: 'contain', position: 'north' })`, - `crop('attention')` is now `resize(width, height, { fit: 'cover', position: 'attention' })`, - `max().withoutEnlargement()` is now `resize(width, height, { fit: 'inside', withoutEnlargement: true })`. - [#1135](https://github.com/lovell/sharp/issues/1135) - -* Deprecate the `background` function. - Per-operation `background` options added to `resize`, `extend` and `flatten` operations. - [#1392](https://github.com/lovell/sharp/issues/1392) - -* Add `size` to `metadata` response (Stream and Buffer input only). - [#695](https://github.com/lovell/sharp/issues/695) - -* Switch from custom trim operation to `vips_find_trim`. - [#914](https://github.com/lovell/sharp/issues/914) - -* Add `chromaSubsampling` and `isProgressive` properties to `metadata` response. - [#1186](https://github.com/lovell/sharp/issues/1186) - -* Drop Node 4 support. - [#1212](https://github.com/lovell/sharp/issues/1212) - -* Enable SIMD convolution by default. - [#1213](https://github.com/lovell/sharp/issues/1213) - -* Add experimental prebuilt binaries for musl-based Linux. - [#1379](https://github.com/lovell/sharp/issues/1379) - -* Add support for arbitrary rotation angle via vips_rotate. - [#1385](https://github.com/lovell/sharp/pull/1385) - [@freezy](https://github.com/freezy) - -## v0.20 - "*prebuild*" - -Requires libvips v8.6.1. - -### v0.20.8 - 5th September 2018 - -* Avoid race conditions when creating directories during installation. - [#1358](https://github.com/lovell/sharp/pull/1358) - [@ajhool](https://github.com/ajhool) - -* Accept floating point values for input density parameter. - [#1362](https://github.com/lovell/sharp/pull/1362) - [@aeirola](https://github.com/aeirola) - -### v0.20.7 - 21st August 2018 - -* Use copy+unlink if rename operation fails during installation. - [#1345](https://github.com/lovell/sharp/issues/1345) - -### v0.20.6 - 20th August 2018 - -* Add removeAlpha operation to remove alpha channel, if any. - [#1248](https://github.com/lovell/sharp/issues/1248) - -* Expose mozjpeg quant_table flag. - [#1285](https://github.com/lovell/sharp/pull/1285) - [@rexxars](https://github.com/rexxars) - -* Allow full WebP alphaQuality range of 0-100. - [#1290](https://github.com/lovell/sharp/pull/1290) - [@sylvaindumont](https://github.com/sylvaindumont) - -* Cache libvips binaries to reduce re-install time. - [#1301](https://github.com/lovell/sharp/issues/1301) - -* Ensure vendor platform mismatch throws error at install time. - [#1303](https://github.com/lovell/sharp/issues/1303) - -* Improve install time error messages for FreeBSD users. - [#1310](https://github.com/lovell/sharp/issues/1310) - -* Ensure extractChannel works with 16-bit images. - [#1330](https://github.com/lovell/sharp/issues/1330) - -* Expose depth option for tile-based output. - [#1342](https://github.com/lovell/sharp/pull/1342) - [@alundavies](https://github.com/alundavies) - -* Add experimental entropy field to stats response. - -### v0.20.5 - 27th June 2018 - -* Expose libjpeg optimize_coding flag. - [#1265](https://github.com/lovell/sharp/pull/1265) - [@tomlokhorst](https://github.com/tomlokhorst) - -### v0.20.4 - 20th June 2018 - -* Prevent possible rounding error when using shrink-on-load and 90/270 degree rotation. - [#1241](https://github.com/lovell/sharp/issues/1241) - [@anahit42](https://github.com/anahit42) - -* Ensure extractChannel sets correct single-channel colour space interpretation. - [#1257](https://github.com/lovell/sharp/issues/1257) - [@jeremychone](https://github.com/jeremychone) - -### v0.20.3 - 29th May 2018 - -* Fix tint operation by ensuring LAB interpretation and allowing negative values. - [#1235](https://github.com/lovell/sharp/issues/1235) - [@wezside](https://github.com/wezside) - -### v0.20.2 - 28th April 2018 - -* Add tint operation to set image chroma. - [#825](https://github.com/lovell/sharp/pull/825) - [@rikh42](https://github.com/rikh42) - -* Add environment variable to ignore globally-installed libvips. - [#1165](https://github.com/lovell/sharp/pull/1165) - [@oncletom](https://github.com/oncletom) - -* Add support for page selection with multi-page input (GIF/TIFF). - [#1204](https://github.com/lovell/sharp/pull/1204) - [@woolite64](https://github.com/woolite64) - -* Add support for Group4 (CCITTFAX4) compression with TIFF output. - [#1208](https://github.com/lovell/sharp/pull/1208) - [@woolite64](https://github.com/woolite64) - -### v0.20.1 - 17th March 2018 - -* Improve installation experience when a globally-installed libvips below the minimum required version is found. - [#1148](https://github.com/lovell/sharp/issues/1148) - -* Prevent smartcrop error when cumulative rounding is below target size. - [#1154](https://github.com/lovell/sharp/issues/1154) - [@ralrom](https://github.com/ralrom) - -* Expose libvips' median filter operation. - [#1161](https://github.com/lovell/sharp/pull/1161) - [@BiancoA](https://github.com/BiancoA) - -### v0.20.0 - 5th March 2018 - -* Add support for prebuilt sharp binaries on common platforms. - [#186](https://github.com/lovell/sharp/issues/186) - -## v0.19 - "*suit*" - -Requires libvips v8.6.1. - -### v0.19.1 - 24th February 2018 - -* Expose libvips' linear transform feature. - [#1024](https://github.com/lovell/sharp/pull/1024) - [@3epnm](https://github.com/3epnm) - -* Expose angle option for tile-based output. - [#1121](https://github.com/lovell/sharp/pull/1121) - [@BiancoA](https://github.com/BiancoA) - -* Prevent crop operation when image already at or below target dimensions. - [#1134](https://github.com/lovell/sharp/issues/1134) - [@pieh](https://github.com/pieh) - -### v0.19.0 - 11th January 2018 - -* Expose offset coordinates of strategy-based crop. - [#868](https://github.com/lovell/sharp/issues/868) - [@mirohristov-com](https://github.com/mirohristov-com) - -* PNG output now defaults to adaptiveFiltering=false, compressionLevel=9 - [#872](https://github.com/lovell/sharp/issues/872) - [@wmertens](https://github.com/wmertens) - -* Add stats feature for pixel-derived image statistics. - [#915](https://github.com/lovell/sharp/pull/915) - [@rnanwani](https://github.com/rnanwani) - -* Add failOnError option to fail-fast on bad input image data. - [#976](https://github.com/lovell/sharp/pull/976) - [@mceachen](https://github.com/mceachen) - -* Resize: switch to libvips' implementation, make fastShrinkOnLoad optional, remove interpolator and centreSampling options. - [#977](https://github.com/lovell/sharp/pull/977) - [@jardakotesovec](https://github.com/jardakotesovec) - -* Attach finish event listener to a clone only for Stream-based input. - [#995](https://github.com/lovell/sharp/issues/995) - [@whmountains](https://github.com/whmountains) - -* Add tilecache before smartcrop to avoid over-computation of previous operations. - [#1028](https://github.com/lovell/sharp/issues/1028) - [@coffeebite](https://github.com/coffeebite) - -* Prevent toFile extension taking precedence over requested format. - [#1037](https://github.com/lovell/sharp/issues/1037) - [@tomgallagher](https://github.com/tomgallagher) - -* Add support for gravity option to existing embed feature. - [#1038](https://github.com/lovell/sharp/pull/1038) - [@AzureByte](https://github.com/AzureByte) - -* Expose IPTC and XMP metadata when available. - [#1079](https://github.com/lovell/sharp/pull/1079) - [@oaleynik](https://github.com/oaleynik) - -* TIFF output: switch default predictor from 'none' to 'horizontal' to match libvips' behaviour. - -## v0.18 - "*ridge*" - -Requires libvips v8.5.5. - -### v0.18.4 - 18th September 2017 - -* Ensure input Buffer really is marked as Persistent, prevents mark-sweep GC. - [#950](https://github.com/lovell/sharp/issues/950) - [@lfdoherty](https://github.com/lfdoherty) - -### v0.18.3 - 13th September 2017 - -* Skip shrink-on-load when trimming. - [#888](https://github.com/lovell/sharp/pull/888) - [@kleisauke](https://github.com/kleisauke) - -* Migrate from got to simple-get for basic auth support. - [#945](https://github.com/lovell/sharp/pull/945) - [@pbomb](https://github.com/pbomb) - -### v0.18.2 - 1st July 2017 - -* Expose libvips' xres and yres properties for TIFF output. - [#828](https://github.com/lovell/sharp/pull/828) - [@YvesBos](https://github.com/YvesBos) - -* Ensure flip and flop operations work with auto-rotate. - [#837](https://github.com/lovell/sharp/issues/837) - [@rexxars](https://github.com/rexxars) - -* Allow binary download URL override via SHARP_DIST_BASE_URL env variable. - [#841](https://github.com/lovell/sharp/issues/841) - -* Add support for Solus Linux. - [#857](https://github.com/lovell/sharp/pull/857) - [@ekremkaraca](https://github.com/ekremkaraca) - -### v0.18.1 - 30th May 2017 - -* Remove regression from #781 that could cause incorrect shrink calculation. - [#831](https://github.com/lovell/sharp/issues/831) - [@suprMax](https://github.com/suprMax) - -### v0.18.0 - 30th May 2017 - -* Remove the previously-deprecated output format "option" functions: - quality, progressive, compressionLevel, withoutAdaptiveFiltering, - withoutChromaSubsampling, trellisQuantisation, trellisQuantization, - overshootDeringing, optimiseScans and optimizeScans. - -* Ensure maximum output dimensions are based on the format to be used. - [#176](https://github.com/lovell/sharp/issues/176) - [@stephanebachelier](https://github.com/stephanebachelier) - -* Avoid costly (un)premultiply when using overlayWith without alpha channel. - [#573](https://github.com/lovell/sharp/issues/573) - [@strarsis](https://github.com/strarsis) - -* Include pixel depth (e.g. "uchar") when reading metadata. - [#577](https://github.com/lovell/sharp/issues/577) - [@moedusa](https://github.com/moedusa) - -* Add support for Buffer and Stream-based TIFF output. - [#587](https://github.com/lovell/sharp/issues/587) - [@strarsis](https://github.com/strarsis) - -* Expose warnings from libvips via NODE_DEBUG=sharp environment variable. - [#607](https://github.com/lovell/sharp/issues/607) - [@puzrin](https://github.com/puzrin) - -* Switch to the libvips implementation of "attention" and "entropy" crop strategies. - [#727](https://github.com/lovell/sharp/issues/727) - -* Improve performance and accuracy of nearest neighbour integral upsampling. - [#752](https://github.com/lovell/sharp/issues/752) - [@MrIbby](https://github.com/MrIbby) - -* Constructor single argument API: allow plain object, reject null/undefined. - [#768](https://github.com/lovell/sharp/issues/768) - [@kub1x](https://github.com/kub1x) - -* Ensure ARM64 pre-built binaries use correct C++11 ABI version. - [#772](https://github.com/lovell/sharp/issues/772) - [@ajiratech2](https://github.com/ajiratech2) - -* Prevent aliasing by using dynamic values for shrink(-on-load). - [#781](https://github.com/lovell/sharp/issues/781) - [@kleisauke](https://github.com/kleisauke) - -* Expose libvips' "squash" parameter to enable 1-bit TIFF output. - [#783](https://github.com/lovell/sharp/pull/783) - [@YvesBos](https://github.com/YvesBos) - -* Add support for rotation using any multiple of +/-90 degrees. - [#791](https://github.com/lovell/sharp/pull/791) - [@ncoden](https://github.com/ncoden) - -* Add "jpg" alias to toFormat as shortened form of "jpeg". - [#814](https://github.com/lovell/sharp/pull/814) - [@jingsam](https://github.com/jingsam) - -## v0.17 - "*quill*" - -Requires libvips v8.4.2. - -### v0.17.3 - 1st April 2017 - -* Allow toBuffer to optionally resolve a Promise with both info and data. - [#143](https://github.com/lovell/sharp/issues/143) - [@salzhrani](https://github.com/salzhrani) - -* Create blank image of given width, height, channels and background. - [#470](https://github.com/lovell/sharp/issues/470) - [@pjarts](https://github.com/pjarts) - -* Add support for the "nearest" kernel for image reductions. - [#732](https://github.com/lovell/sharp/pull/732) - [@alice0meta](https://github.com/alice0meta) - -* Add support for TIFF compression and predictor options. - [#738](https://github.com/lovell/sharp/pull/738) - [@kristojorg](https://github.com/kristojorg) - -### v0.17.2 - 11th February 2017 - -* Ensure Readable side of Stream can start flowing after Writable side has finished. - [#671](https://github.com/lovell/sharp/issues/671) - [@danhaller](https://github.com/danhaller) - -* Expose WebP alpha quality, lossless and near-lossless output options. - [#685](https://github.com/lovell/sharp/pull/685) - [@rnanwani](https://github.com/rnanwani) - -### v0.17.1 - 15th January 2017 - -* Improve error messages for invalid parameters. - [@spikeon](https://github.com/spikeon) - [#644](https://github.com/lovell/sharp/pull/644) - -* Simplify expression for finding vips-cpp libdir. - [#656](https://github.com/lovell/sharp/pull/656) - -* Allow HTTPS-over-HTTP proxy when downloading pre-compiled dependencies. - [@wangzhiwei1888](https://github.com/wangzhiwei1888) - [#679](https://github.com/lovell/sharp/issues/679) - -### v0.17.0 - 11th December 2016 - -* Drop support for versions of Node prior to v4. - -* Deprecate the following output format "option" functions: - quality, progressive, compressionLevel, withoutAdaptiveFiltering, - withoutChromaSubsampling, trellisQuantisation, trellisQuantization, - overshootDeringing, optimiseScans and optimizeScans. - Access to these is now via output format functions, for example `quality(n)` - is now `jpeg({quality: n})` and/or `webp({quality: n})`. - -* Autoconvert GIF and SVG input to PNG output if no other format is specified. - -* Expose libvips' "centre" resize option to mimic \*magick's +0.5px convention. - [#568](https://github.com/lovell/sharp/issues/568) - -* Ensure support for embedded base64 PNG and JPEG images within an SVG. - [#601](https://github.com/lovell/sharp/issues/601) - [@dynamite-ready](https://github.com/dynamite-ready) - -* Ensure premultiply operation occurs before box filter shrink. - [#605](https://github.com/lovell/sharp/issues/605) - [@CmdrShepardsPie](https://github.com/CmdrShepardsPie) - [@teroparvinen](https://github.com/teroparvinen) - -* Add support for PNG and WebP tile-based output formats (in addition to JPEG). - [#622](https://github.com/lovell/sharp/pull/622) - [@ppaskaris](https://github.com/ppaskaris) - -* Allow use of extend with greyscale input. - [#623](https://github.com/lovell/sharp/pull/623) - [@ppaskaris](https://github.com/ppaskaris) - -* Allow non-RGB input to embed/extend onto background with an alpha channel. - [#646](https://github.com/lovell/sharp/issues/646) - [@DaGaMs](https://github.com/DaGaMs) - -## v0.16 - "*pencil*" - -Requires libvips v8.3.3 - -### v0.16.2 - 22nd October 2016 - -* Restrict readelf usage to Linux only when detecting global libvips version. - [#602](https://github.com/lovell/sharp/issues/602) - [@caoko](https://github.com/caoko) - -### v0.16.1 - 13th October 2016 - -* C++11 ABI version is now auto-detected, remove sharp-cxx11 installation flag. - -* Add experimental 'attention' crop strategy. - [#295](https://github.com/lovell/sharp/issues/295) - -* Include .node extension for Meteor's require() implementation. - [#537](https://github.com/lovell/sharp/issues/537) - [@isjackwild](https://github.com/isjackwild) - -* Ensure convolution kernel scale is clamped to a minimum value of 1. - [#561](https://github.com/lovell/sharp/issues/561) - [@abagshaw](https://github.com/abagshaw) - -* Correct calculation of y-axis placement when overlaying image at a fixed point. - [#566](https://github.com/lovell/sharp/issues/566) - [@Nateowami](https://github.com/Nateowami) - -### v0.16.0 - 18th August 2016 - -* Add pre-compiled libvips for OS X, ARMv7 and ARMv8. - [#312](https://github.com/lovell/sharp/issues/312) - -* Ensure boolean, bandbool, extractChannel ops occur before sRGB conversion. - [#504](https://github.com/lovell/sharp/pull/504) - [@mhirsch](https://github.com/mhirsch) - -* Recalculate factors after WebP shrink-on-load to avoid round-to-zero errors. - [#508](https://github.com/lovell/sharp/issues/508) - [@asilvas](https://github.com/asilvas) - -* Prevent boolean errors during extract operation. - [#511](https://github.com/lovell/sharp/pull/511) - [@mhirsch](https://github.com/mhirsch) - -* Add joinChannel and toColourspace/toColorspace operations. - [#513](https://github.com/lovell/sharp/pull/513) - [@mhirsch](https://github.com/mhirsch) - -* Add support for raw pixel data with boolean and withOverlay operations. - [#516](https://github.com/lovell/sharp/pull/516) - [@mhirsch](https://github.com/mhirsch) - -* Prevent bandbool creating a single channel sRGB image. - [#519](https://github.com/lovell/sharp/pull/519) - [@mhirsch](https://github.com/mhirsch) - -* Ensure ICC profiles are removed from PNG output unless withMetadata used. - [#521](https://github.com/lovell/sharp/issues/521) - [@ChrisPinewood](https://github.com/ChrisPinewood) - -* Add alpha channels, if missing, to overlayWith images. - [#540](https://github.com/lovell/sharp/pull/540) - [@cmtt](https://github.com/cmtt) - -* Remove deprecated interpolateWith method - use resize(w, h, { interpolator: ... }) - [#310](https://github.com/lovell/sharp/issues/310) - -## v0.15 - "*outfit*" - -Requires libvips v8.3.1 - -### v0.15.1 - 12th July 2016 - -* Concat Stream-based input in single operation for ~+3% perf and less GC. - [#429](https://github.com/lovell/sharp/issues/429) - [@papandreou](https://github.com/papandreou) - -* Add alpha channel, if required, before extend operation. - [#439](https://github.com/lovell/sharp/pull/439) - [@frulo](https://github.com/frulo) - -* Allow overlay image to be repeated across entire image via tile option. - [#443](https://github.com/lovell/sharp/pull/443) - [@lemnisk8](https://github.com/lemnisk8) - -* Add cutout option to overlayWith feature, applies only the alpha channel of the overlay image. - [#448](https://github.com/lovell/sharp/pull/448) - [@kleisauke](https://github.com/kleisauke) - -* Ensure scaling factors are calculated independently to prevent rounding errors. - [#452](https://github.com/lovell/sharp/issues/452) - [@puzrin](https://github.com/puzrin) - -* Add --sharp-cxx11 flag to compile with gcc's new C++11 ABI. - [#456](https://github.com/lovell/sharp/pull/456) - [@kapouer](https://github.com/kapouer) - -* Add top/left offset support to overlayWith operation. - [#473](https://github.com/lovell/sharp/pull/473) - [@rnanwani](https://github.com/rnanwani) - -* Add convolve operation for kernel-based convolution. - [#479](https://github.com/lovell/sharp/pull/479) - [@mhirsch](https://github.com/mhirsch) - -* Add greyscale option to threshold operation for colourspace conversion control. - [#480](https://github.com/lovell/sharp/pull/480) - [@mhirsch](https://github.com/mhirsch) - -* Ensure ICC profiles are licenced for distribution. - [#486](https://github.com/lovell/sharp/issues/486) - [@kapouer](https://github.com/kapouer) - -* Allow images with an alpha channel to work with LAB-colourspace based sharpen. - [#490](https://github.com/lovell/sharp/issues/490) - [@jwagner](https://github.com/jwagner) - -* Add trim operation to remove "boring" edges. - [#492](https://github.com/lovell/sharp/pull/492) - [@kleisauke](https://github.com/kleisauke) - -* Add bandbool feature for channel-wise boolean operations. - [#496](https://github.com/lovell/sharp/pull/496) - [@mhirsch](https://github.com/mhirsch) - -* Add extractChannel operation to extract a channel from an image. - [#497](https://github.com/lovell/sharp/pull/497) - [@mhirsch](https://github.com/mhirsch) - -* Add ability to read and write native libvips .v files. - [#500](https://github.com/lovell/sharp/pull/500) - [@mhirsch](https://github.com/mhirsch) - -* Add boolean feature for bitwise image operations. - [#501](https://github.com/lovell/sharp/pull/501) - [@mhirsch](https://github.com/mhirsch) - -### v0.15.0 - 21st May 2016 - -* Use libvips' new Lanczos 3 kernel as default for image reduction. - Deprecate interpolateWith method, now provided as a resize option. - [#310](https://github.com/lovell/sharp/issues/310) - [@jcupitt](https://github.com/jcupitt) - -* Take advantage of libvips v8.3 features. - Add support for libvips' new GIF and SVG loaders. - Pre-built binaries now include giflib and librsvg, exclude *magick. - Use shrink-on-load for WebP input. - Break existing sharpen API to accept sigma and improve precision. - [#369](https://github.com/lovell/sharp/issues/369) - -* Remove unnecessary (un)premultiply operations when not resizing/compositing. - [#413](https://github.com/lovell/sharp/issues/413) - [@jardakotesovec](https://github.com/jardakotesovec) - -## v0.14 - "*needle*" - -Requires libvips v8.2.3 - -### v0.14.1 - 16th April 2016 - -* Allow removal of limitation on input pixel count via limitInputPixels. Use with care. - [#250](https://github.com/lovell/sharp/issues/250) - [#316](https://github.com/lovell/sharp/pull/316) - [@anandthakker](https://github.com/anandthakker) - [@kentongray](https://github.com/kentongray) - -* Use final output image for metadata passed to callback. - [#399](https://github.com/lovell/sharp/pull/399) - [@salzhrani](https://github.com/salzhrani) - -* Add support for writing tiled images to a zip container. - [#402](https://github.com/lovell/sharp/pull/402) - [@felixbuenemann](https://github.com/felixbuenemann) - -* Allow use of embed with 1 and 2 channel images. - [#411](https://github.com/lovell/sharp/issues/411) - [@janaz](https://github.com/janaz) - -* Improve Electron compatibility by allowing node-gyp rebuilds without npm. - [#412](https://github.com/lovell/sharp/issues/412) - [@nouh](https://github.com/nouh) - -### v0.14.0 - 2nd April 2016 - -* Add ability to extend (pad) the edges of an image. - [#128](https://github.com/lovell/sharp/issues/128) - [@blowsie](https://github.com/blowsie) - -* Add support for Zoomify and Google tile layouts. Breaks existing tile API. - [#223](https://github.com/lovell/sharp/issues/223) - [@bdunnette](https://github.com/bdunnette) - -* Improvements to overlayWith: differing sizes/formats, gravity, buffer input. - [#239](https://github.com/lovell/sharp/issues/239) - [@chrisriley](https://github.com/chrisriley) - -* Add entropy-based crop strategy to remove least interesting edges. - [#295](https://github.com/lovell/sharp/issues/295) - [@rightaway](https://github.com/rightaway) - -* Expose density metadata; set density of images from vector input. - [#338](https://github.com/lovell/sharp/issues/338) - [@lookfirst](https://github.com/lookfirst) - -* Emit post-processing 'info' event for Stream output. - [#367](https://github.com/lovell/sharp/issues/367) - [@salzhrani](https://github.com/salzhrani) - -* Ensure output image EXIF Orientation values are within 1-8 range. - [#385](https://github.com/lovell/sharp/pull/385) - [@jtobinisaniceguy](https://github.com/jtobinisaniceguy) - -* Ensure ratios are not swapped when rotating 90/270 and ignoring aspect. - [#387](https://github.com/lovell/sharp/issues/387) - [@kleisauke](https://github.com/kleisauke) - -* Remove deprecated style of calling extract API. Breaks calls using positional arguments. - [#276](https://github.com/lovell/sharp/issues/276) - -## v0.13 - "*mind*" - -Requires libvips v8.2.2 - -### v0.13.1 - 27th February 2016 - -* Fix embedding onto transparent backgrounds; regression introduced in v0.13.0. - [#366](https://github.com/lovell/sharp/issues/366) - [@diegocsandrim](https://github.com/diegocsandrim) - -### v0.13.0 - 15th February 2016 - -* Improve vector image support by allowing control of density/DPI. - Switch pre-built libs from Imagemagick to Graphicsmagick. - [#110](https://github.com/lovell/sharp/issues/110) - [@bradisbell](https://github.com/bradisbell) - -* Add support for raw, uncompressed pixel Buffer/Stream input. - [#220](https://github.com/lovell/sharp/issues/220) - [@mikemorris](https://github.com/mikemorris) - -* Switch from libvips' C to C++ bindings, requires upgrade to v8.2.2. - [#299](https://github.com/lovell/sharp/issues/299) - -* Control number of open files in libvips' cache; breaks existing `cache` behaviour. - [#315](https://github.com/lovell/sharp/issues/315) - [@impomezia](https://github.com/impomezia) - -* Ensure 16-bit input images can be normalised and embedded onto transparent backgrounds. - [#339](https://github.com/lovell/sharp/issues/339) - [#340](https://github.com/lovell/sharp/issues/340) - [@janaz](https://github.com/janaz) - -* Ensure selected format takes precedence over any unknown output filename extension. - [#344](https://github.com/lovell/sharp/issues/344) - [@ubaltaci](https://github.com/ubaltaci) - -* Add support for libvips' PBM, PGM, PPM and FITS image format loaders. - [#347](https://github.com/lovell/sharp/issues/347) - [@oaleynik](https://github.com/oaleynik) - -* Ensure default crop gravity is center/centre. - [#351](https://github.com/lovell/sharp/pull/351) - [@joelmukuthu](https://github.com/joelmukuthu) - -* Improve support for musl libc systems e.g. Alpine Linux. - [#354](https://github.com/lovell/sharp/issues/354) - [#359](https://github.com/lovell/sharp/pull/359) - [@download13](https://github.com/download13) - [@wjordan](https://github.com/wjordan) - -* Small optimisation when reducing by an integral factor to favour shrink over affine. - -* Add support for gamma correction of images with an alpha channel. - -## v0.12 - "*look*" - -Requires libvips v8.2.0 - -### v0.12.2 - 16th January 2016 - -* Upgrade libvips to v8.2.0 for improved vips_shrink. - -* Add pre-compiled libvips for ARMv6+ CPUs. - -* Ensure 16-bit input images work with embed option. - [#325](https://github.com/lovell/sharp/issues/325) - [@janaz](https://github.com/janaz) - -* Allow compilation with gmake to provide FreeBSD support. - [#326](https://github.com/lovell/sharp/issues/326) - [@c0decafe](https://github.com/c0decafe) - -* Attempt to remove temporary file after installation. - [#331](https://github.com/lovell/sharp/issues/331) - [@dtoubelis](https://github.com/dtoubelis) - -### v0.12.1 - 12th December 2015 - -* Allow use of SIMD vector instructions (via liborc) to be toggled on/off. - [#172](https://github.com/lovell/sharp/issues/172) - [@bkw](https://github.com/bkw) - [@puzrin](https://github.com/puzrin) - -* Ensure embedded ICC profiles output with perceptual intent. - [#321](https://github.com/lovell/sharp/issues/321) - [@vlapo](https://github.com/vlapo) - -* Use the NPM-configured HTTPS proxy, if any, for binary downloads. - -### v0.12.0 - 23rd November 2015 - -* Bundle pre-compiled libvips and its dependencies for 64-bit Linux and Windows. - [#42](https://github.com/lovell/sharp/issues/42) - -* Take advantage of libvips v8.1.0+ features. - [#152](https://github.com/lovell/sharp/issues/152) - -* Add support for 64-bit Windows. Drop support for 32-bit Windows. - [#224](https://github.com/lovell/sharp/issues/224) - [@sabrehagen](https://github.com/sabrehagen) - -* Switch default interpolator to bicubic. - [#289](https://github.com/lovell/sharp/issues/289) - [@mahnunchik](https://github.com/mahnunchik) - -* Pre-extract rotatation should not swap width/height. - [#296](https://github.com/lovell/sharp/issues/296) - [@asilvas](https://github.com/asilvas) - -* Ensure 16-bit+alpha input images are (un)premultiplied correctly. - [#301](https://github.com/lovell/sharp/issues/301) - [@izaakschroeder](https://github.com/izaakschroeder) - -* Add `threshold` operation. - [#303](https://github.com/lovell/sharp/pull/303) - [@dacarley](https://github.com/dacarley) - -* Add `negate` operation. - [#306](https://github.com/lovell/sharp/pull/306) - [@dacarley](https://github.com/dacarley) - -* Support `options` Object with existing `extract` operation. - [#309](https://github.com/lovell/sharp/pull/309) - [@papandreou](https://github.com/papandreou) - -## v0.11 - "*knife*" - -### v0.11.4 - 5th November 2015 - -* Add corners, e.g. `northeast`, to existing `gravity` option. - [#291](https://github.com/lovell/sharp/pull/291) - [@brandonaaron](https://github.com/brandonaaron) - -* Ensure correct auto-rotation for EXIF Orientation values 2 and 4. - [#288](https://github.com/lovell/sharp/pull/288) - [@brandonaaron](https://github.com/brandonaaron) - -* Make static linking possible via `--runtime_link` install option. - [#287](https://github.com/lovell/sharp/pull/287) - [@vlapo](https://github.com/vlapo) - -### v0.11.3 - 8th September 2015 - -* Intrepret blurSigma, sharpenFlat, and sharpenJagged as double precision. - [#263](https://github.com/lovell/sharp/pull/263) - [@chrisriley](https://github.com/chrisriley) - -### v0.11.2 - 28th August 2015 - -* Allow crop gravity to be provided as a String. - [#255](https://github.com/lovell/sharp/pull/255) - [@papandreou](https://github.com/papandreou) -* Add support for io.js v3 and Node v4. - [#246](https://github.com/lovell/sharp/issues/246) - -### v0.11.1 - 12th August 2015 - -* Silence MSVC warning: "C4530: C++ exception handler used, but unwind semantics are not enabled". - [#244](https://github.com/lovell/sharp/pull/244) - [@TheThing](https://github.com/TheThing) - -* Suppress gamma correction for input image with alpha transparency. - [#249](https://github.com/lovell/sharp/issues/249) - [@compeak](https://github.com/compeak) - -### v0.11.0 - 15th July 2015 - -* Allow alpha transparency compositing via new `overlayWith` method. - [#97](https://github.com/lovell/sharp/issues/97) - [@gasi](https://github.com/gasi) - -* Expose raw ICC profile data as a Buffer when using `metadata`. - [#129](https://github.com/lovell/sharp/issues/129) - [@homerjam](https://github.com/homerjam) - -* Allow image header updates via a parameter passed to existing `withMetadata` method. - Provide initial support for EXIF `Orientation` tag, - which if present is now removed when using `rotate`, `flip` or `flop`. - [#189](https://github.com/lovell/sharp/issues/189) - [@h2non](https://github.com/h2non) - -* Tighten constructor parameter checks. - [#221](https://github.com/lovell/sharp/issues/221) - [@mikemorris](https://github.com/mikemorris) - -* Allow one input Stream to be shared with two or more output Streams via new `clone` method. - [#235](https://github.com/lovell/sharp/issues/235) - [@jaubourg](https://github.com/jaubourg) - -* Use `round` instead of `floor` when auto-scaling dimensions to avoid floating-point rounding errors. - [#238](https://github.com/lovell/sharp/issues/238) - [@richardadjogah](https://github.com/richardadjogah) - -## v0.10 - "*judgment*" - -### v0.10.1 - 1st June 2015 - -* Allow embed of image with alpha transparency onto non-transparent background. - [#204](https://github.com/lovell/sharp/issues/204) - [@mikemliu](https://github.com/mikemliu) - -* Include C standard library for `atoi` as Xcode 6.3 appears to no longer do this. - [#228](https://github.com/lovell/sharp/issues/228) - [@doggan](https://github.com/doggan) - -### v0.10.0 - 23rd April 2015 - -* Add support for Windows (x86). - [#19](https://github.com/lovell/sharp/issues/19) - [@DullReferenceException](https://github.com/DullReferenceException) - [@itsananderson](https://github.com/itsananderson) - -* Add support for Openslide input and DeepZoom output. - [#146](https://github.com/lovell/sharp/issues/146) - [@mvictoras](https://github.com/mvictoras) - -* Allow arbitrary aspect ratios when resizing images via new `ignoreAspectRatio` method. - [#192](https://github.com/lovell/sharp/issues/192) - [@skedastik](https://github.com/skedastik) - -* Enhance output image contrast by stretching its luminance to cover the full dynamic range via new `normalize` method. - [#194](https://github.com/lovell/sharp/issues/194) - [@bkw](https://github.com/bkw) - [@codingforce](https://github.com/codingforce) diff --git a/docs/firebase.json b/docs/firebase.json index 5ba9f56ce..f00270acb 100644 --- a/docs/firebase.json +++ b/docs/firebase.json @@ -1,118 +1,16 @@ { "hosting": { "site": "pixelplumbing-sharp", - "public": ".", - "ignore": [ - ".*", - "build.js", - "firebase.json", - "*.md", - "image/**", - "search-index/**" - ], + "public": "dist", "headers": [ { "source": "**", "headers": [ - { - "key": "Cache-Control", - "value": "max-age=86400" - } + { "key": "Cache-Control", "value": "max-age=86400" }, + { "key": "X-Content-Type-Options", "value": "nosniff" }, + { "key": "X-Frame-Options", "value": "SAMEORIGIN" } ] } - ], - "redirects": [ - { - "source": "**/install/**", - "destination": "/install", - "type": 301 - }, - { - "source": "/page/install", - "destination": "/install", - "type": 301 - }, - { - "source": "**/api-constructor/**", - "destination": "/api-constructor", - "type": 301 - }, - { - "source": "**/api-input/**", - "destination": "/api-input", - "type": 301 - }, - { - "source": "**/api-output/**", - "destination": "/api-output", - "type": 301 - }, - { - "source": "**/api-resize/**", - "destination": "/api-resize", - "type": 301 - }, - { - "source": "**/api-compsite/**", - "destination": "/api-compsite", - "type": 301 - }, - { - "source": "**/api-operation/**", - "destination": "/api-operation", - "type": 301 - }, - { - "source": "**/api-colour/**", - "destination": "/api-colour", - "type": 301 - }, - { - "source": "**/api-channel/**", - "destination": "/api-channel", - "type": 301 - }, - { - "source": "**/api-utility/**", - "destination": "/api-utility", - "type": 301 - }, - { - "source": "/page/api", - "destination": "/api-constructor", - "type": 301 - }, - { - "source": "**/performance/**", - "destination": "/performance", - "type": 301 - }, - { - "source": "/page/performance", - "destination": "/performance", - "type": 301 - }, - { - "source": "**/changelog/**", - "destination": "/changelog", - "type": 301 - }, - { - "source": "/page/changelog", - "destination": "/changelog", - "type": 301 - }, - { - "source": "/en/**", - "destination": "/", - "type": 301 - } - ], - "rewrites": [ - { - "source": "**", - "destination": "/index.html" - } ] } } diff --git a/docs/index.html b/docs/index.html deleted file mode 100644 index 789f470e4..000000000 --- a/docs/index.html +++ /dev/null @@ -1,216 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - sharp - High performance Node.js image processing - - - -
- - - - - \ No newline at end of file diff --git a/docs/install.md b/docs/install.md deleted file mode 100644 index 051e924e3..000000000 --- a/docs/install.md +++ /dev/null @@ -1,240 +0,0 @@ -# Installation - -```sh -npm install sharp -``` - -```sh -yarn add sharp -``` - -## Prerequisites - -* Node.js v10+ - -## Prebuilt binaries - -Ready-compiled sharp and libvips binaries are provided for use with -Node.js v10+ on the most common platforms: - -* macOS x64 (>= 10.13) -* Linux x64 (glibc >= 2.17, musl >= 1.1.24) -* Linux ARM64 (glibc >= 2.29, musl >= 1.1.24) -* Windows x64 -* Windows x86 - -An ~7.5MB 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, AVIF, TIFF, GIF (input) and SVG (input) image formats. - -The following platforms have prebuilt libvips but not sharp: - -* macOS ARM64 -* Linux ARMv6 -* Linux ARMv7 (glibc >= 2.28) -* Windows ARM64 - -The following platforms require compilation of both libvips and sharp from source: - -* Linux x86 -* Linux x64 (glibc <= 2.16, includes RHEL/CentOS 6) -* Linux ARM64 (glibc <= 2.28) -* Linux PowerPC -* FreeBSD -* OpenBSD - -## Common problems - -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. - -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 - -Prebuilt libvips binaries are provided for macOS on ARM64 (since sharp v0.28.0). - -During `npm install` sharp will be built locally, -which requires Xcode and Python - see -[building from source](#building-from-source). - -When this new ARM64 CPU is made freely available -to open source projects via a CI service -then prebuilt sharp binaries can also be provided. - -## Custom libvips - -To use a custom, globally-installed version of libvips instead of the provided binaries, -make sure it is at least the version listed under `config.libvips` in the `package.json` file -and that it can be located using `pkg-config --modversion vips-cpp`. - -For help compiling libvips from source, please see -[https://libvips.github.io/libvips/install.html#building-libvips-from-a-source-tarball](https://libvips.github.io/libvips/install.html#building-libvips-from-a-source-tarball). - -The use of a globally-installed libvips is unsupported on Windows. - -## Building from source - -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 sharp binaries do not exist for the current platform, or -* when the `npm install --build-from-source` flag is used. - -Building from source requires: - -* C++11 compiler -* [node-gyp](https://github.com/nodejs/node-gyp#installation) and its dependencies - -## Custom prebuilt binaries - -This is an advanced approach that most people will not require. - -To install the prebuilt sharp binaries from a custom URL, -set the `sharp_binary_host` npm config option -or the `npm_config_sharp_binary_host` environment variable. - -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. 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` or -`https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.gz`. - -See the Chinese mirror below for a further example. - -## Chinese mirror - -Alibaba provide a mirror site based in China containing binaries for both sharp and libvips. - -To use this either set the following configuration: - -```sh -npm config set sharp_binary_host "https://npm.taobao.org/mirrors/sharp" -npm config set sharp_libvips_binary_host "https://npm.taobao.org/mirrors/sharp-libvips" -npm install sharp -``` - -or set the following environment variables: - -```sh -npm_config_sharp_binary_host="https://npm.taobao.org/mirrors/sharp" \ - npm_config_sharp_libvips_binary_host="https://npm.taobao.org/mirrors/sharp-libvips" \ - npm install sharp -``` - -## FreeBSD - -The `vips` package must be installed before `npm install` is run. - -```sh -pkg install -y pkgconf vips -``` - -```sh -cd /usr/ports/graphics/vips/ && make install clean -``` - -## Linux memory allocator - -The default memory allocator on most glibc-based Linux systems -(e.g. Debian, Red Hat) is unsuitable for long-running, multi-threaded -processes that involve lots of small memory allocations. - -For this reason, by default, sharp will limit the use of thread-based -[concurrency](api-utility#concurrency) when the glibc allocator is -detected at runtime. - -To help avoid fragmentation and improve performance on these systems, -the use of an alternative memory allocator such as -[jemalloc](https://github.com/jemalloc/jemalloc) is recommended. - -Those using musl-based Linux (e.g. Alpine) and non-Linux systems are -unaffected. - -## Heroku - -Add the -[jemalloc buildpack](https://github.com/gaffneyc/heroku-buildpack-jemalloc) -to reduce the effects of memory fragmentation. - -Set -[NODE_MODULES_CACHE](https://devcenter.heroku.com/articles/nodejs-support#cache-behavior) -to `false` when using the `yarn` package manager. - -## AWS Lambda - -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. - -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 -``` - -Alternatively a Docker container closely matching the Lambda runtime can be used: - -```sh -rm -rf node_modules/sharp -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. - -## Webpack - -Ensure sharp is added to the -[externals](https://webpack.js.org/configuration/externals/) -configuration. - -```js -externals: { - 'sharp': 'commonjs sharp' -} -``` - -## 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 - -### 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. -``` diff --git a/docs/package.json b/docs/package.json new file mode 100644 index 000000000..7eaa41943 --- /dev/null +++ b/docs/package.json @@ -0,0 +1,18 @@ +{ + "name": "sharp-docs", + "type": "module", + "version": "0.0.1", + "private": true, + "scripts": { + "dev": "astro dev", + "start": "astro dev", + "build": "astro build", + "preview": "astro preview", + "astro": "astro" + }, + "dependencies": { + "@astrojs/starlight": "^0.36.2", + "astro": "^5.15.3", + "starlight-auto-sidebar": "^0.1.3" + } +} diff --git a/docs/performance.md b/docs/performance.md deleted file mode 100644 index 2d8834cb9..000000000 --- a/docs/performance.md +++ /dev/null @@ -1,68 +0,0 @@ -# Performance - -A test to benchmark the performance of this module relative to alternatives. - -## The contenders - -* [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.6 - 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.28.0 / libvips v8.10.6 - Caching within libvips disabled to ensure a fair comparison. - -## The task - -Decompress a 2725x2225 JPEG image, -resize to 720x588 using Lanczos 3 resampling (where available), -then compress to JPEG at a "quality" setting of 80. - -## Test environment - -* AWS EC2 eu-west-1 [c5ad.xlarge](https://aws.amazon.com/ec2/instance-types/c5/) (4x AMD EPYC 7R32) -* Ubuntu 20.10 (ami-03f10415e8b0bfb86) -* Node.js v14.16.0 - -## Results - -| Module | Input | Output | Ops/sec | Speed-up | -| :----------------- | :----- | :----- | ------: | -------: | -| jimp | buffer | buffer | 0.78 | 1.0 | -| mapnik | buffer | buffer | 3.39 | 4.3 | -| gm | buffer | buffer | 7.84 | 10.1 | -| gm | file | file | 9.24 | 11.8 | -| imagemagick | file | file | 9.37 | 12.0 | -| sharp | stream | stream | 26.84 | 34.4 | -| sharp | file | file | 29.76 | 38.2 | -| sharp | buffer | buffer | 31.60 | 40.5 | - -Greater libvips performance can be expected with caching enabled (default) -and using 8+ core machines, especially those with larger L1/L2 CPU caches. - -The I/O limits of the relevant (de)compression library will generally determine maximum throughput. - -## Running the benchmark test - -Requires _ImageMagick_, _GraphicsMagick_ and _Mapnik_: - -```sh -brew install imagemagick -brew install graphicsmagick -brew install mapnik -``` - -```sh -sudo apt-get install build-essential imagemagick libmagick++-dev graphicsmagick libmapnik-dev -``` - -```sh -sudo yum install ImageMagick-devel ImageMagick-c++-devel GraphicsMagick mapnik-devel -``` - -```sh -git clone https://github.com/lovell/sharp.git -cd sharp -npm install --build-from-source -cd test/bench -npm install -npm test -``` diff --git a/docs/public/api-resize-fit.svg b/docs/public/api-resize-fit.svg new file mode 100644 index 000000000..9227a0d2f --- /dev/null +++ b/docs/public/api-resize-fit.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + cover + + + + contain + + + + fill + + + + + inside + + + + + outside + + diff --git a/docs/image/sharp-logo.svg b/docs/public/favicon.svg similarity index 100% rename from docs/image/sharp-logo.svg rename to docs/public/favicon.svg diff --git a/docs/humans.txt b/docs/public/humans.txt similarity index 64% rename from docs/humans.txt rename to docs/public/humans.txt index 3ec1bdb22..7119152ab 100644 --- a/docs/humans.txt +++ b/docs/public/humans.txt @@ -28,7 +28,7 @@ Name: Brandon Aaron GitHub: https://github.com/brandonaaron Name: Andreas Lind -GitHub: https://github.com/papandreouGitHub: +GitHub: https://github.com/papandreou Name: Maurus Cuelenaere GitHub: https://github.com/mcuelenaere @@ -209,3 +209,120 @@ GitHub: https://github.com/beig Name: Florian Busch GitHub: https://github.com/florian-busch + +Name: Matthieu Salettes +GitHub: https://github.com/msalettes + +Name: Taneli Vatanen +GitHub: https://github.com/Daiz + +Name: Mart Jansink +GitHub: https://github.com/mart-jansink + +Name: Tenpi +GitHub: https://github.com/Tenpi + +Name: Zaruike +GitHub: https://github.com/Zaruike + +Name: Erlend F +GitHub: https://github.com/erf + +Name: Drian Naude +GitHub: https://github.com/driannaude + +Name: Max Gordon +GitHub: https://github.com/gforge + +Name: Chris Banks +GitHub: https://github.com/christopherbradleybanks + +Name: codepage949 +GitHub: https://github.com/codepage949 + +Name: Chris Hranj +GitHub: https://github.com/Brodan + +Name: Ankur Parihar +GitHub: https://github.com/ankurparihar + +Name: Joona Heinikoski +GitHub: https://github.com/joonamo + +Name: AlexanderTheGrey +GitHub: https://github.com/AlexanderTheGrey + +Name: Blayne Chard +GitHub: https://github.com/blacha + +Name: Brahim +GitHub: https://github.com/brahima + +Name: Anton Marsden +GitHub: https://github.com/antonmarsden + +Name: Marcos Casagrande +GitHub: https://github.com/marcosc90 + +Name: Emanuel Jöbstl +GitHub: https://github.com/ejoebstl + +Name: Tomasz Janowski +GitHub: https://github.com/janaz + +Name: Lachlan Newman +GitHub: https://github.com/LachlanNewman + +Name: BJJ +GitHub: https://github.com/bianjunjie1981 + +Name: Dennis Beatty +GitHub: https://github.com/dnsbty + +Name: Ingvar Stepanyan +GitHub: https://github.com/RReverser + +Name: Tamás András Horváth +GitHub: https://github.com/icetee + +Name: Aaron Che +GitHub: https://github.com/yolopunk + +Name: Mert Alev +GitHub: https://github.com/mertalev + +Name: Adriaan Meuris +GitHub: https://github.com/adriaanmeuris + +Name: Richard Hillmann +GitHub: https://github.com/project0 + +Name: Pongsatorn Manusopit +GitHub: https://github.com/ton11797 + +Name: Nathan Keynes +GitHub: https://github.com/nkeynes + +Name: Sumit D +GitHub: https://github.com/sumitd2 + +Name: Caleb Meredith +GitHub: https://github.com/calebmer + +Name: Don Denton +GitHub: https://github.com/happycollision + +Name: Florent Zabera +GitHub: https://github.com/florentzabera + +Name: Quentin Pinçon +GitHub: https://github.com/qpincon + +Name: Hans Chen +GitHub: https://github.com/hans00 + +Name: Thibaut Patel +GitHub: https://github.com/tpatel + +Name: Maël Nison +GitHub: https://github.com/arcanis diff --git a/docs/public/robots.txt b/docs/public/robots.txt new file mode 100644 index 000000000..a829753d9 --- /dev/null +++ b/docs/public/robots.txt @@ -0,0 +1,4 @@ +User-agent: * +Disallow: + +Sitemap: https://sharp.pixelplumbing.com/sitemap-index.xml diff --git a/docs/image/sharp-logo-mono.svg b/docs/public/sharp-logo-mono.svg similarity index 100% rename from docs/image/sharp-logo-mono.svg rename to docs/public/sharp-logo-mono.svg diff --git a/docs/public/sharp-logo.svg b/docs/public/sharp-logo.svg new file mode 100644 index 000000000..fc185469f --- /dev/null +++ b/docs/public/sharp-logo.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/docs/robots.txt b/docs/robots.txt deleted file mode 100644 index 6f27bb66a..000000000 --- a/docs/robots.txt +++ /dev/null @@ -1,2 +0,0 @@ -User-agent: * -Disallow: \ No newline at end of file diff --git a/docs/search-index.json b/docs/search-index.json deleted file mode 100644 index 2f0138c8e..000000000 --- a/docs/search-index.json +++ /dev/null @@ -1 +0,0 @@ -[{"t":"Prerequisites","d":"Node.js v10","k":"prerequisites node","l":"/install#prerequisites"},{"t":"Prebuilt binaries","d":"Ready-compiled sharp and libvips binaries are provided for use with Node.js v10 on the most common platforms macOS x64 10.13 Linux x64 glibc 2.17, musl 1.1.24 Linux ARM64 glibc 2.2","k":"prebuilt binaries compiled sharp libvips node common platforms macos linux glibc musl arm","l":"/install#prebuilt-binaries"},{"t":"Common problems","d":"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. When using npm v6 or earlier, the npm in","k":"common problems architecture platform node npm install runtime earlier","l":"/install#common-problems"},{"t":"Apple M1","d":"Prebuilt libvips binaries are provided for macOS on ARM64 since sharp v0.28.0. During npm install sharp will be built locally, which requires Xcode and Python - see building from s","k":"apple prebuilt libvips binaries macos arm sharp during npm install built locally requires xcode python building","l":"/install#apple-m1"},{"t":"Custom libvips","d":"To use a custom, globally-installed version of libvips instead of the provided binaries, make sure it is at least the version listed under config.libvips in the package.json file a","k":"custom libvips globally installed version instead binaries make listed config package json file","l":"/install#custom-libvips"},{"t":"Building from source","d":"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, pr","k":"building source module compiled npm install time globally installed libvips detected environment variable skip","l":"/install#building-from-source"},{"t":"Custom prebuilt binaries","d":"This is an advanced approach that most people will not require. To install the prebuilt sharp binaries from a custom URL, set the sharp_binary_host npm config option or the npm_con","k":"custom prebuilt binaries advanced approach people require install sharp url npm config option npmcon","l":"/install#custom-prebuilt-binaries"},{"t":"Chinese mirror","d":"Alibaba provide a mirror site based in China containing binaries for both sharp and libvips. To use this either set the following configuration sh npm config set sharp_binary_host","k":"chinese mirror alibaba site china binaries sharp libvips following configuration npm config","l":"/install#chinese-mirror"},{"t":"FreeBSD","d":"The vips package must be installed before npm install is run. sh pkg install -y pkgconf vips sh cd /usr/ports/graphics/vips/ make install clean","k":"freebsd vips package installed npm install run pkg pkgconf usr ports graphics make clean","l":"/install#freebsd"},{"t":"Linux memory allocator","d":"The default memory allocator on most glibc-based Linux systems e.g. Debian, Red Hat is unsuitable for long-running, multi-threaded processes that involve lots of small memory alloc","k":"linux memory allocator glibc systems debian red hat unsuitable long running multi threaded processes small alloc","l":"/install#linux-memory-allocator"},{"t":"Heroku","d":"Add the jemalloc buildpack to reduce the effects of memory fragmentation. Set NODE_MODULES_CACHE","k":"heroku add jemalloc buildpack reduce effects memory fragmentation","l":"/install#heroku"},{"t":"AWS Lambda","d":"Set the Lambda runtime to nodejs12.x. The binaries in the node_modules directory of the deployment package must be for the Linux x64 platform. When building your deployment package","k":"aws lambda runtime nodejs binaries nodemodules directory deployment package linux platform building your","l":"/install#aws-lambda"},{"t":"Webpack","d":"Ensure sharp is added to the externals configuration. js externals sharp commonjs sharp","k":"webpack sharp added externals configuration commonjs","l":"/install#webpack"},{"t":"Worker threads","d":"The main thread must call requiresharp before worker threads are created to ensure shared libraries remain loaded in memory until after all threads are complete.","k":"worker threads main thread requiresharp created shared libraries remain loaded memory complete","l":"/install#worker-threads"},{"t":"Canvas and Windows","d":"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.","k":"canvas windows prebuilt binaries depend unmaintained gtk last updated conflict modern date sharp","l":"/install#canvas-and-windows"},{"t":"Sharp","d":"Constructor factory to create an instance of sharp, to which further methods are chained.","k":"sharp constructor factory create instance further methods chained","l":"/api-constructor#sharp"},{"t":"clone","d":"Take a snapshot of the Sharp instance, returning a new instance. Cloned instances inherit the input of their parent instance. This allows multiple output Streams and therefore mult","k":"clone snapshot sharp instance returning new cloned instances inherit input parent multiple output streams mult","l":"/api-constructor#clone"},{"t":"metadata","d":"Fast access to uncached image metadata without decoding any compressed image data. A Promise is returned when callback is not provided.","k":"metadata fast access uncached decoding compressed data promise returned callback","l":"/api-input#metadata"},{"t":"stats","d":"Access to pixel-derived image statistics for every channel in the image. A Promise is returned when callback is not provided.","k":"stats access pixel derived statistics channel promise returned callback","l":"/api-input#stats"},{"t":"toFile","d":"Write output image data to a file.","k":"tofile write output data file","l":"/api-output#tofile"},{"t":"toBuffer","d":"Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF and raw pixel data output are supported.","k":"tobuffer write output buffer jpeg png webp avif tiff raw pixel data","l":"/api-output#tobuffer"},{"t":"withMetadata","d":"Include all metadata EXIF, XMP, IPTC from the input image in the output image. This will also convert to and add a web-friendly sRGB ICC profile unless a custom output profile is p","k":"withmetadata include metadata exif xmp iptc input output convert add web friendly srgb icc profile custom","l":"/api-output#withmetadata"},{"t":"toFormat","d":"Force output to a given format.","k":"toformat force output format","l":"/api-output#toformat"},{"t":"jpeg","d":"Use these JPEG options for output image.","k":"jpeg output","l":"/api-output#jpeg"},{"t":"png","d":"Use these PNG options for output image.","k":"png output","l":"/api-output#png"},{"t":"webp","d":"Use these WebP options for output image.","k":"webp output","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for output image.","k":"gif output","l":"/api-output#gif"},{"t":"tiff","d":"Use these TIFF options for output image.","k":"tiff output","l":"/api-output#tiff"},{"t":"avif","d":"Use these AVIF options for output image.","k":"avif output","l":"/api-output#avif"},{"t":"heif","d":"Use these HEIF options for output image.","k":"heif output","l":"/api-output#heif"},{"t":"raw","d":"Force output to be raw, uncompressed, 8-bit unsigned integer unit8 pixel data. Pixel ordering is left-to-right, top-to-bottom, without padding. Channel ordering will be RGB or RGBA","k":"raw force output uncompressed bit unsigned integer unit pixel data ordering left right top bottom padding channel rgb rgba","l":"/api-output#raw"},{"t":"tile","d":"Use tile-based deep zoom image pyramid output. Set the format and options for tile images via the toFormat, jpeg, png or webp functions. Use a .zip or .szi file extension with toFi","k":"tile deep zoom pyramid output format images via toformat jpeg png webp functions zip szi file extension tofi","l":"/api-output#tile"},{"t":"resize","d":"Resize image to width, height or width x height.","k":"resize width height","l":"/api-resize#resize"},{"t":"extend","d":"Extends/pads the edges of the image with the provided background colour. This operation will always occur after resizing and extraction, if any.","k":"extend extends pads edges background colour operation resizing extraction","l":"/api-resize#extend"},{"t":"extract","d":"Extract/crop a region of the image.","k":"extract crop region","l":"/api-resize#extract"},{"t":"trim","d":"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","k":"trim boring pixels edges contain similar top left pixel images consisting entirely single colour calculate alpha channel","l":"/api-resize#trim"},{"t":"composite","d":"Composite images over the processed resized, extracted etc. image.","k":"composite images processed resized extracted","l":"/api-composite#composite"},{"t":"rotate","d":"Rotate the output image by either an explicit angle or auto-orient based on the EXIF Orientation tag.","k":"rotate output explicit angle auto orient exif orientation tag","l":"/api-operation#rotate"},{"t":"flip","d":"Flip the image about the vertical Y axis. This always occurs after rotation, if any. The use of flip implies the removal of the EXIF Orientation tag, if any.","k":"flip vertical axis rotation implies removal exif orientation tag","l":"/api-operation#flip"},{"t":"flop","d":"Flop the image about the horizontal X axis. This always occurs after rotation, if any. The use of flop implies the removal of the EXIF Orientation tag, if any.","k":"flop horizontal axis rotation implies removal exif orientation tag","l":"/api-operation#flop"},{"t":"affine","d":"Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.","k":"affine transform operation resizing extraction rotation","l":"/api-operation#affine"},{"t":"sharpen","d":"Sharpen the image. When used without parameters, performs a fast, mild sharpen of the output image. When a sigma is provided, performs a slower, more accurate sharpen of the L chan","k":"sharpen parameters fast mild output sigma slower accurate chan","l":"/api-operation#sharpen"},{"t":"median","d":"Apply median filter. When used without parameters the default window is 3x3.","k":"median apply filter parameters window","l":"/api-operation#median"},{"t":"blur","d":"Blur the image. When used without parameters, performs a fast, mild blur of the output image. When a sigma is provided, performs a slower, more accurate Gaussian blur.","k":"blur parameters fast mild output sigma slower accurate gaussian","l":"/api-operation#blur"},{"t":"flatten","d":"Merge alpha transparency channel, if any, with a background, then remove the alpha channel.","k":"flatten merge alpha transparency channel background then remove","l":"/api-operation#flatten"},{"t":"gamma","d":"Apply a gamma correction by reducing the encoding darken pre-resize at a factor of 1/gamma then increasing the encoding brighten post-resize at a factor of gamma. This can improve","k":"gamma apply correction reducing encoding darken pre resize factor then increasing brighten post improve","l":"/api-operation#gamma"},{"t":"negate","d":"Produce the negative of the image.","k":"negate produce negative","l":"/api-operation#negate"},{"t":"normalise","d":"Enhance output image contrast by stretching its luminance to cover the full dynamic range.","k":"normalise enhance output contrast stretching luminance cover full dynamic range","l":"/api-operation#normalise"},{"t":"normalize","d":"Alternative spelling of normalise.","k":"normalize normalise","l":"/api-operation#normalize"},{"t":"convolve","d":"Convolve the image with the specified kernel.","k":"convolve specified kernel","l":"/api-operation#convolve"},{"t":"threshold","d":"Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0.","k":"threshold pixel greater than equal otherwise","l":"/api-operation#threshold"},{"t":"boolean","d":"Perform a bitwise boolean operation with operand image.","k":"boolean bitwise operation operand","l":"/api-operation#boolean"},{"t":"linear","d":"Apply the linear formula a input b to the image levels adjustment","k":"linear apply formula input levels adjustment","l":"/api-operation#linear"},{"t":"recomb","d":"Recomb the image with the specified matrix.","k":"recomb specified matrix","l":"/api-operation#recomb"},{"t":"modulate","d":"Transforms the image using brightness, saturation and hue rotation.","k":"modulate transforms brightness saturation hue rotation","l":"/api-operation#modulate"},{"t":"removeAlpha","d":"Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.","k":"removealpha remove alpha channel","l":"/api-channel#removealpha"},{"t":"ensureAlpha","d":"Ensure alpha channel, if missing. The added alpha channel will be fully opaque. This is a no-op if the image already has an alpha channel.","k":"ensurealpha alpha channel missing added fully opaque","l":"/api-channel#ensurealpha"},{"t":"extractChannel","d":"Extract a single channel from a multi-channel image.","k":"extractchannel extract single channel multi","l":"/api-channel#extractchannel"},{"t":"joinChannel","d":"Join one or more channels to the image. The meaning of the added channels depends on the output colourspace, set with toColourspace. By default the output image will be web-friendl","k":"joinchannel join one channels meaning added depends output colourspace tocolourspace web friendl","l":"/api-channel#joinchannel"},{"t":"bandbool","d":"Perform a bitwise boolean operation on all input image channels bands to produce a single channel output image.","k":"bandbool bitwise boolean operation input channels bands produce single channel output","l":"/api-channel#bandbool"},{"t":"tint","d":"Tint the image using the provided chroma while preserving the image luminance. An alpha channel may be present and will be unchanged by the operation.","k":"tint chroma preserving luminance alpha channel present unchanged operation","l":"/api-colour#tint"},{"t":"greyscale","d":"Convert to 8-bit greyscale 256 shades of grey. This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use gamma with greyscale for the best re","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma best","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"toColourspace","d":"Set the output colourspace. By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.","k":"tocolourspace output colourspace web friendly srgb additional channels interpreted alpha","l":"/api-colour#tocolourspace"},{"t":"toColorspace","d":"Alternative spelling of toColourspace.","k":"tocolorspace tocolourspace","l":"/api-colour#tocolorspace"},{"t":"format","d":"An Object containing nested boolean values representing the available input and output formats/methods.","k":"format object nested boolean representing available input output formats methods","l":"/api-utility#format"},{"t":"interpolators","d":"An Object containing the available interpolators and their proper values","k":"interpolators object available proper","l":"/api-utility#interpolators"},{"t":"versions","d":"An Object containing the version numbers of libvips and its dependencies.","k":"versions object version numbers libvips dependencies","l":"/api-utility#versions"},{"t":"cache","d":"Gets or, when options are provided, sets the limits of _libvips_ operation cache. Existing entries in the cache will be trimmed after any change in limits. This method always retur","k":"cache limits libvips operation existing entries trimmed change method retur","l":"/api-utility#cache"},{"t":"concurrency","d":"Gets or, when a concurrency is provided, sets the number of threads _libvips_ should create to process each image.","k":"concurrency number threads libvips create process","l":"/api-utility#concurrency"},{"t":"queue","d":"An EventEmitter that emits a change event when a task is either","k":"queue eventemitter emits change event task","l":"/api-utility#queue"},{"t":"counters","d":"Provides access to internal task counters.","k":"counters provides access internal task","l":"/api-utility#counters"},{"t":"simd","d":"Get and set use of SIMD vector unit instructions. Requires libvips to have been compiled with liborc support.","k":"simd vector unit instructions requires libvips compiled liborc","l":"/api-utility#simd"}] \ No newline at end of file diff --git a/docs/search-index/build.js b/docs/search-index/build.js deleted file mode 100644 index f8f2d673b..000000000 --- a/docs/search-index/build.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const { extractDescription, extractKeywords } = require('./extract'); - -const searchIndex = []; - -// Install -const contents = fs.readFileSync(path.join(__dirname, '..', 'install.md'), 'utf8'); -const matches = contents.matchAll( - /## (?[A-Za-z0-9 ]+)\n\n(?<body>[^#]+)/gs -); -for (const match of matches) { - const { title, body } = match.groups; - const description = extractDescription(body); - - searchIndex.push({ - t: title, - d: description, - k: extractKeywords(`${title} ${description}`), - l: `/install#${title.toLowerCase().replace(/ /g, '-')}` - }); -} - -// API -[ - 'constructor', - 'input', - 'output', - 'resize', - 'composite', - 'operation', - 'channel', - 'colour', - 'utility' -].forEach((section) => { - 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 - ); - for (const match of matches) { - const { title, firstparagraph } = match.groups; - const description = firstparagraph.startsWith('###') - ? 'Constructor' - : extractDescription(firstparagraph); - - searchIndex.push({ - t: title, - d: description, - k: extractKeywords(`${title} ${description}`), - l: `/api-${section}#${title.toLowerCase()}` - }); - } -}); - -fs.writeFileSync( - path.join(__dirname, '..', 'search-index.json'), - JSON.stringify(searchIndex) -); diff --git a/docs/search-index/extract.js b/docs/search-index/extract.js deleted file mode 100644 index 4d97f2905..000000000 --- a/docs/search-index/extract.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; - -const stopWords = require('./stop-words'); - -const extractDescription = (str) => - str - .replace(/\(http[^)]+/g, '') - .replace(/\s+/g, ' ') - .replace(/[^A-Za-z0-9_/\-,. ]/g, '') - .replace(/\s+/g, ' ') - .substr(0, 180) - .trim(); - -const extractKeywords = (str) => - [ - ...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 }; diff --git a/docs/search-index/stop-words.js b/docs/search-index/stop-words.js deleted file mode 100644 index 0a777fbce..000000000 --- a/docs/search-index/stop-words.js +++ /dev/null @@ -1,92 +0,0 @@ -'use strict'; - -module.exports = [ - 'about', - 'after', - 'all', - 'allows', - 'already', - 'also', - 'alternative', - 'always', - 'and', - 'any', - 'are', - 'based', - 'been', - 'before', - 'both', - 'call', - 'can', - 'containing', - 'default', - 'does', - 'each', - 'either', - 'ensure', - 'etc', - 'every', - 'for', - 'from', - 'get', - 'gets', - 'given', - 'has', - 'have', - 'how', - 'image', - 'involve', - 'its', - 'least', - 'lots', - 'may', - 'more', - 'most', - 'much', - 'must', - 'non', - 'not', - 'occur', - 'occurs', - 'options', - 'over', - 'perform', - 'performs', - 'provide', - 'provided', - 'ready', - 'same', - 'see', - 'set', - 'sets', - 'should', - 'since', - 'spelling', - 'such', - 'support', - 'supported', - 'sure', - 'take', - 'that', - 'the', - 'their', - 'there', - 'therefore', - 'these', - 'this', - 'under', - 'unless', - 'until', - 'use', - 'used', - 'using', - 'value', - 'values', - 'when', - 'which', - 'while', - 'will', - 'with', - 'without', - 'you' -]; diff --git a/docs/src/assets/sharp-logo.svg b/docs/src/assets/sharp-logo.svg new file mode 100644 index 000000000..fc185469f --- /dev/null +++ b/docs/src/assets/sharp-logo.svg @@ -0,0 +1,5 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="86 86 550 550"> +<!-- 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> \ No newline at end of file diff --git a/docs/src/content.config.ts b/docs/src/content.config.ts new file mode 100644 index 000000000..06cf12929 --- /dev/null +++ b/docs/src/content.config.ts @@ -0,0 +1,10 @@ +import { defineCollection } from 'astro:content'; +import { docsLoader } from '@astrojs/starlight/loaders'; +import { docsSchema } from '@astrojs/starlight/schema'; +import { autoSidebarLoader } from 'starlight-auto-sidebar/loader' +import { autoSidebarSchema } from 'starlight-auto-sidebar/schema' + +export const collections = { + docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }), + autoSidebar: defineCollection({ loader: autoSidebarLoader(), schema: autoSidebarSchema() }) +}; diff --git a/docs/src/content/docs/api-channel.md b/docs/src/content/docs/api-channel.md new file mode 100644 index 000000000..214e8611d --- /dev/null +++ b/docs/src/content/docs/api-channel.md @@ -0,0 +1,143 @@ +--- +# This file was auto-generated from JSDoc in lib/channel.js +title: Channel manipulation +--- + +## removeAlpha +> removeAlpha() ⇒ <code>Sharp</code> + +Remove alpha channels, if any. This is a no-op if the image does not have an alpha channel. + +See also [flatten](/api-operation/#flatten). + + +**Example** +```js +sharp('rgba.png') + .removeAlpha() + .toFile('rgb.png', function(err, info) { + // rgb.png is a 3 channel image without an alpha channel + }); +``` + + +## ensureAlpha +> ensureAlpha([alpha]) ⇒ <code>Sharp</code> + +Ensure the output image has an alpha transparency channel. +If missing, the added alpha channel will have the specified +transparency level, defaulting to fully-opaque (1). +This is a no-op if the image already has an alpha channel. + + +**Throws**: + +- <code>Error</code> Invalid alpha transparency level + +**Since**: 0.21.2 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [alpha] | <code>number</code> | <code>1</code> | alpha transparency level (0=fully-transparent, 1=fully-opaque) | + +**Example** +```js +// rgba.png will be a 4 channel image with a fully-opaque alpha channel +await sharp('rgb.jpg') + .ensureAlpha() + .toFile('rgba.png') +``` +**Example** +```js +// rgba is a 4 channel image with a fully-transparent alpha channel +const rgba = await sharp(rgb) + .ensureAlpha(0) + .toBuffer(); +``` + + +## extractChannel +> extractChannel(channel) ⇒ <code>Sharp</code> + +Extract a single channel from a multi-channel image. + +The output colourspace will be either `b-w` (8-bit) or `grey16` (16-bit). + + +**Throws**: + +- <code>Error</code> Invalid channel + + +| Param | Type | Description | +| --- | --- | --- | +| channel | <code>number</code> \| <code>string</code> | zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`. | + +**Example** +```js +// green.jpg is a greyscale image containing the green channel of the input +await sharp(input) + .extractChannel('green') + .toFile('green.jpg'); +``` +**Example** +```js +// red1 is the red value of the first pixel, red2 the second pixel etc. +const [red1, red2, ...] = await sharp(input) + .extractChannel(0) + .raw() + .toBuffer(); +``` + + +## joinChannel +> joinChannel(images, options) ⇒ <code>Sharp</code> + +Join one or more channels to the image. +The meaning of the added channels depends on the output colourspace, set with `toColourspace()`. +By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. +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. +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. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| images | <code>Array.<(string\|Buffer)></code> \| <code>string</code> \| <code>Buffer</code> | one or more images (file paths, Buffers). | +| options | <code>Object</code> | image options, see `sharp()` constructor. | + + + +## bandbool +> bandbool(boolOp) ⇒ <code>Sharp</code> + +Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| boolOp | <code>string</code> | one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively. | + +**Example** +```js +sharp('3-channel-rgb-input.png') + .bandbool(sharp.bool.and) + .toFile('1-channel-output.png', function (err, info) { + // The output will be a single channel image where each pixel `P = R & G & B`. + // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]` + // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`. + }); +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-colour.md b/docs/src/content/docs/api-colour.md new file mode 100644 index 000000000..6fb81debd --- /dev/null +++ b/docs/src/content/docs/api-colour.md @@ -0,0 +1,150 @@ +--- +# This file was auto-generated from JSDoc in lib/colour.js +title: Colour manipulation +--- + +## tint +> tint(tint) ⇒ <code>Sharp</code> + +Tint the image using the provided colour. +An alpha channel may be present and will be unchanged by the operation. + + +**Throws**: + +- <code>Error</code> Invalid parameter + + +| Param | Type | Description | +| --- | --- | --- | +| tint | <code>string</code> \| <code>Object</code> | Parsed by the [color](https://www.npmjs.org/package/color) module. | + +**Example** +```js +const output = await sharp(input) + .tint({ r: 255, g: 240, b: 16 }) + .toBuffer(); +``` + + +## greyscale +> greyscale([greyscale]) ⇒ <code>Sharp</code> + +Convert to 8-bit greyscale; 256 shades of grey. +This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use `gamma()` with `greyscale()` for the best results. +By default the output image will be web-friendly sRGB and contain three (identical) colour channels. +This may be overridden by other sharp operations such as `toColourspace('b-w')`, +which will produce an output image containing one colour channel. +An alpha channel may be present, and will be unchanged by the operation. + + + +| Param | Type | Default | +| --- | --- | --- | +| [greyscale] | <code>Boolean</code> | <code>true</code> | + +**Example** +```js +const output = await sharp(input).greyscale().toBuffer(); +``` + + +## grayscale +> grayscale([grayscale]) ⇒ <code>Sharp</code> + +Alternative spelling of `greyscale`. + + + +| Param | Type | Default | +| --- | --- | --- | +| [grayscale] | <code>Boolean</code> | <code>true</code> | + + + +## pipelineColourspace +> pipelineColourspace([colourspace]) ⇒ <code>Sharp</code> + +Set the pipeline colourspace. + +The input image will be converted to the provided colourspace at the start of the pipeline. +All operations will use this colourspace before converting to the output colourspace, +as defined by [toColourspace](#tocolourspace). + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.29.0 + +| Param | Type | Description | +| --- | --- | --- | +| [colourspace] | <code>string</code> | pipeline colourspace e.g. `rgb16`, `scrgb`, `lab`, `grey16` [...](https://www.libvips.org/API/current/enum.Interpretation.html) | + +**Example** +```js +// Run pipeline in 16 bits per channel RGB while converting final result to 8 bits per channel sRGB. +await sharp(input) + .pipelineColourspace('rgb16') + .toColourspace('srgb') + .toFile('16bpc-pipeline-to-8bpc-output.png') +``` + + +## pipelineColorspace +> pipelineColorspace([colorspace]) ⇒ <code>Sharp</code> + +Alternative spelling of `pipelineColourspace`. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| [colorspace] | <code>string</code> | pipeline colorspace. | + + + +## toColourspace +> toColourspace([colourspace]) ⇒ <code>Sharp</code> + +Set the output colourspace. +By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| [colourspace] | <code>string</code> | output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html) | + +**Example** +```js +// Output 16 bits per pixel RGB +await sharp(input) + .toColourspace('rgb16') + .toFile('16-bpp.png') +``` + + +## toColorspace +> toColorspace([colorspace]) ⇒ <code>Sharp</code> + +Alternative spelling of `toColourspace`. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| [colorspace] | <code>string</code> | output colorspace. | \ No newline at end of file diff --git a/docs/src/content/docs/api-composite.md b/docs/src/content/docs/api-composite.md new file mode 100644 index 000000000..d4f9b8550 --- /dev/null +++ b/docs/src/content/docs/api-composite.md @@ -0,0 +1,103 @@ +--- +# This file was auto-generated from JSDoc in lib/composite.js +title: Compositing images +--- + +## composite +> composite(images) ⇒ <code>Sharp</code> + +Composite image(s) over the processed (resized, extracted etc.) image. + +The images to composite must be the same size or smaller than the processed image. +If both `top` and `left` options are provided, they take precedence over `gravity`. + +Other operations in the same processing pipeline (e.g. resize, rotate, flip, +flop, extract) will always be applied to the input image before composition. + +The `blend` option can be one of `clear`, `source`, `over`, `in`, `out`, `atop`, +`dest`, `dest-over`, `dest-in`, `dest-out`, `dest-atop`, +`xor`, `add`, `saturate`, `multiply`, `screen`, `overlay`, `darken`, `lighten`, +`colour-dodge`, `color-dodge`, `colour-burn`,`color-burn`, +`hard-light`, `soft-light`, `difference`, `exclusion`. + +More information about blend modes can be found at +https://www.libvips.org/API/current/enum.BlendMode.html +and https://www.cairographics.org/operators/ + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.22.0 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| images | <code>Array.<Object></code> | | Ordered list of images to composite | +| [images[].input] | <code>Buffer</code> \| <code>String</code> | | Buffer containing image data, String containing the path to an image file, or Create object (see below) | +| [images[].input.create] | <code>Object</code> | | describes a blank overlay to be created. | +| [images[].input.create.width] | <code>Number</code> | | | +| [images[].input.create.height] | <code>Number</code> | | | +| [images[].input.create.channels] | <code>Number</code> | | 3-4 | +| [images[].input.create.background] | <code>String</code> \| <code>Object</code> | | parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. | +| [images[].input.text] | <code>Object</code> | | describes a new text image to be created. | +| [images[].input.text.text] | <code>string</code> | | text to render as a UTF-8 string. It can contain Pango markup, for example `<i>Le</i>Monde`. | +| [images[].input.text.font] | <code>string</code> | | font name to render with. | +| [images[].input.text.fontfile] | <code>string</code> | | absolute filesystem path to a font file that can be used by `font`. | +| [images[].input.text.width] | <code>number</code> | <code>0</code> | integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. | +| [images[].input.text.height] | <code>number</code> | <code>0</code> | integral number of pixels high. When defined, `dpi` will be ignored and the text will automatically fit the pixel resolution defined by `width` and `height`. Will be ignored if `width` is not specified or set to 0. | +| [images[].input.text.align] | <code>string</code> | <code>"'left'"</code> | text alignment (`'left'`, `'centre'`, `'center'`, `'right'`). | +| [images[].input.text.justify] | <code>boolean</code> | <code>false</code> | set this to true to apply justification to the text. | +| [images[].input.text.dpi] | <code>number</code> | <code>72</code> | the resolution (size) at which to render the text. Does not take effect if `height` is specified. | +| [images[].input.text.rgba] | <code>boolean</code> | <code>false</code> | set this to true to enable RGBA output. This is useful for colour emoji rendering, or support for Pango markup features like `<span foreground="red">Red!</span>`. | +| [images[].input.text.spacing] | <code>number</code> | <code>0</code> | text line height in points. Will use the font line height if none is specified. | +| [images[].autoOrient] | <code>Boolean</code> | <code>false</code> | set to true to use EXIF orientation data, if present, to orient the image. | +| [images[].blend] | <code>String</code> | <code>'over'</code> | how to blend this image with the image below. | +| [images[].gravity] | <code>String</code> | <code>'centre'</code> | gravity at which to place the overlay. | +| [images[].top] | <code>Number</code> | | the pixel offset from the top edge. | +| [images[].left] | <code>Number</code> | | the pixel offset from the left edge. | +| [images[].tile] | <code>Boolean</code> | <code>false</code> | set to true to repeat the overlay image across the entire image with the given `gravity`. | +| [images[].premultiplied] | <code>Boolean</code> | <code>false</code> | set to true to avoid premultiplying the image below. Equivalent to the `--premultiplied` vips option. | +| [images[].density] | <code>Number</code> | <code>72</code> | number representing the DPI for vector overlay image. | +| [images[].raw] | <code>Object</code> | | describes overlay when using raw pixel data. | +| [images[].raw.width] | <code>Number</code> | | | +| [images[].raw.height] | <code>Number</code> | | | +| [images[].raw.channels] | <code>Number</code> | | | +| [images[].animated] | <code>boolean</code> | <code>false</code> | Set to `true` to read all frames/pages of an animated image. | +| [images[].failOn] | <code>string</code> | <code>"'warning'"</code> | @see [constructor parameters](/api-constructor/) | +| [images[].limitInputPixels] | <code>number</code> \| <code>boolean</code> | <code>268402689</code> | @see [constructor parameters](/api-constructor/) | + +**Example** +```js +await sharp(background) + .composite([ + { input: layer1, gravity: 'northwest' }, + { input: layer2, gravity: 'southeast' }, + ]) + .toFile('combined.png'); +``` +**Example** +```js +const output = await sharp('input.gif', { animated: true }) + .composite([ + { input: 'overlay.png', tile: true, blend: 'saturate' } + ]) + .toBuffer(); +``` +**Example** +```js +sharp('input.png') + .rotate(180) + .resize(300) + .flatten( { background: '#ff6600' } ) + .composite([{ input: 'overlay.png', gravity: 'southeast' }]) + .sharpen() + .withMetadata() + .webp( { quality: 90 } ) + .toBuffer() + .then(function(outputBuffer) { + // outputBuffer contains upside down, 300px wide, alpha channel flattened + // onto orange background, composited with overlay.png with SE gravity, + // sharpened, with metadata, 90% quality WebP image data. Phew! + }); +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-constructor.md b/docs/src/content/docs/api-constructor.md new file mode 100644 index 000000000..3504239dc --- /dev/null +++ b/docs/src/content/docs/api-constructor.md @@ -0,0 +1,274 @@ +--- +# This file was auto-generated from JSDoc in lib/constructor.js +title: Constructor +--- + +## Sharp +> Sharp + + +**Emits**: <code>Sharp#event:info</code>, <code>Sharp#event:warning</code> +<a name="new_Sharp_new"></a> + +### new +> new Sharp([input], [options]) + +Constructor factory to create an instance of `sharp`, to which further methods are chained. + +JPEG, PNG, WebP, GIF, 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. + +Implements the [stream.Duplex](http://nodejs.org/api/stream.html#stream_class_stream_duplex) class. + +When loading more than one page/frame of an animated image, +these are combined as a vertically-stacked "toilet roll" image +where the overall height is the `pageHeight` multiplied by the number of `pages`. + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [input] | <code>Buffer</code> \| <code>ArrayBuffer</code> \| <code>Uint8Array</code> \| <code>Uint8ClampedArray</code> \| <code>Int8Array</code> \| <code>Uint16Array</code> \| <code>Int16Array</code> \| <code>Uint32Array</code> \| <code>Int32Array</code> \| <code>Float32Array</code> \| <code>Float64Array</code> \| <code>string</code> \| <code>Array</code> | | if present, can be a Buffer / ArrayBuffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image data, or a TypedArray containing raw pixel image data, or a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file. An array of inputs can be provided, and these will be joined together. JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present. | +| [options] | <code>Object</code> | | if present, is an Object with optional attributes. | +| [options.failOn] | <code>string</code> | <code>"'warning'"</code> | When to abort processing of invalid pixel data, one of (in order of sensitivity, least to most): 'none', 'truncated', 'error', 'warning'. Higher levels imply lower levels. Invalid metadata will always abort. | +| [options.limitInputPixels] | <code>number</code> \| <code>boolean</code> | <code>268402689</code> | 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). | +| [options.unlimited] | <code>boolean</code> | <code>false</code> | Set this to `true` to remove safety features that help prevent memory exhaustion (JPEG, PNG, SVG, HEIF). | +| [options.autoOrient] | <code>boolean</code> | <code>false</code> | Set this to `true` to rotate/flip the image to match EXIF `Orientation`, if any. | +| [options.sequentialRead] | <code>boolean</code> | <code>true</code> | Set this to `false` to use random access rather than sequential read. Some operations will do this automatically. | +| [options.density] | <code>number</code> | <code>72</code> | number representing the DPI for vector images in the range 1 to 100000. | +| [options.ignoreIcc] | <code>number</code> | <code>false</code> | should the embedded ICC profile, if any, be ignored. | +| [options.pages] | <code>number</code> | <code>1</code> | Number of pages to extract for multi-page input (GIF, WebP, TIFF), use -1 for all pages. | +| [options.page] | <code>number</code> | <code>0</code> | Page number to start extracting from for multi-page input (GIF, WebP, TIFF), zero based. | +| [options.animated] | <code>boolean</code> | <code>false</code> | Set to `true` to read all frames/pages of an animated image (GIF, WebP, TIFF), equivalent of setting `pages` to `-1`. | +| [options.raw] | <code>Object</code> | | describes raw pixel input image data. See `raw()` for pixel ordering. | +| [options.raw.width] | <code>number</code> | | integral number of pixels wide. | +| [options.raw.height] | <code>number</code> | | integral number of pixels high. | +| [options.raw.channels] | <code>number</code> | | integral number of channels, between 1 and 4. | +| [options.raw.premultiplied] | <code>boolean</code> | | specifies that the raw input has already been premultiplied, set to `true` to avoid sharp premultiplying the image. (optional, default `false`) | +| [options.raw.pageHeight] | <code>number</code> | | The pixel height of each page/frame for animated images, must be an integral factor of `raw.height`. | +| [options.create] | <code>Object</code> | | describes a new image to be created. | +| [options.create.width] | <code>number</code> | | integral number of pixels wide. | +| [options.create.height] | <code>number</code> | | integral number of pixels high. | +| [options.create.channels] | <code>number</code> | | integral number of channels, either 3 (RGB) or 4 (RGBA). | +| [options.create.background] | <code>string</code> \| <code>Object</code> | | parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. | +| [options.create.pageHeight] | <code>number</code> | | The pixel height of each page/frame for animated images, must be an integral factor of `create.height`. | +| [options.create.noise] | <code>Object</code> | | describes a noise to be created. | +| [options.create.noise.type] | <code>string</code> | | type of generated noise, currently only `gaussian` is supported. | +| [options.create.noise.mean] | <code>number</code> | <code>128</code> | Mean value of pixels in the generated noise. | +| [options.create.noise.sigma] | <code>number</code> | <code>30</code> | Standard deviation of pixel values in the generated noise. | +| [options.text] | <code>Object</code> | | describes a new text image to be created. | +| [options.text.text] | <code>string</code> | | text to render as a UTF-8 string. It can contain Pango markup, for example `<i>Le</i>Monde`. | +| [options.text.font] | <code>string</code> | | font name to render with. | +| [options.text.fontfile] | <code>string</code> | | absolute filesystem path to a font file that can be used by `font`. | +| [options.text.width] | <code>number</code> | <code>0</code> | Integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. | +| [options.text.height] | <code>number</code> | <code>0</code> | Maximum integral number of pixels high. When defined, `dpi` will be ignored and the text will automatically fit the pixel resolution defined by `width` and `height`. Will be ignored if `width` is not specified or set to 0. | +| [options.text.align] | <code>string</code> | <code>"'left'"</code> | Alignment style for multi-line text (`'left'`, `'centre'`, `'center'`, `'right'`). | +| [options.text.justify] | <code>boolean</code> | <code>false</code> | set this to true to apply justification to the text. | +| [options.text.dpi] | <code>number</code> | <code>72</code> | the resolution (size) at which to render the text. Does not take effect if `height` is specified. | +| [options.text.rgba] | <code>boolean</code> | <code>false</code> | set this to true to enable RGBA output. This is useful for colour emoji rendering, or support for pango markup features like `<span foreground="red">Red!</span>`. | +| [options.text.spacing] | <code>number</code> | <code>0</code> | text line height in points. Will use the font line height if none is specified. | +| [options.text.wrap] | <code>string</code> | <code>"'word'"</code> | word wrapping style when width is provided, one of: 'word', 'char', 'word-char' (prefer word, fallback to char) or 'none'. | +| [options.join] | <code>Object</code> | | describes how an array of input images should be joined. | +| [options.join.across] | <code>number</code> | <code>1</code> | number of images to join horizontally. | +| [options.join.animated] | <code>boolean</code> | <code>false</code> | set this to `true` to join the images as an animated image. | +| [options.join.shim] | <code>number</code> | <code>0</code> | number of pixels to insert between joined images. | +| [options.join.background] | <code>string</code> \| <code>Object</code> | | parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. | +| [options.join.halign] | <code>string</code> | <code>"'left'"</code> | horizontal alignment style for images joined horizontally (`'left'`, `'centre'`, `'center'`, `'right'`). | +| [options.join.valign] | <code>string</code> | <code>"'top'"</code> | vertical alignment style for images joined vertically (`'top'`, `'centre'`, `'center'`, `'bottom'`). | +| [options.tiff] | <code>Object</code> | | Describes TIFF specific options. | +| [options.tiff.subifd] | <code>number</code> | <code>-1</code> | Sub Image File Directory to extract for OME-TIFF, defaults to main image. | +| [options.svg] | <code>Object</code> | | Describes SVG specific options. | +| [options.svg.stylesheet] | <code>string</code> | | Custom CSS for SVG input, applied with a User Origin during the CSS cascade. | +| [options.svg.highBitdepth] | <code>boolean</code> | <code>false</code> | Set to `true` to render SVG input at 32-bits per channel (128-bit) instead of 8-bits per channel (32-bit) RGBA. | +| [options.pdf] | <code>Object</code> | | Describes PDF specific options. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick. | +| [options.pdf.background] | <code>string</code> \| <code>Object</code> | | Background colour to use when PDF is partially transparent. Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. | +| [options.openSlide] | <code>Object</code> | | Describes OpenSlide specific options. Requires the use of a globally-installed libvips compiled with support for OpenSlide. | +| [options.openSlide.level] | <code>number</code> | <code>0</code> | Level to extract from a multi-level input, zero based. | +| [options.jp2] | <code>Object</code> | | Describes JPEG 2000 specific options. Requires the use of a globally-installed libvips compiled with support for OpenJPEG. | +| [options.jp2.oneshot] | <code>boolean</code> | <code>false</code> | Set to `true` to decode tiled JPEG 2000 images in a single operation, improving compatibility. | + +**Example** +```js +sharp('input.jpg') + .resize(300, 200) + .toFile('output.jpg', function(err) { + // output.jpg is a 300 pixels wide and 200 pixels high image + // containing a scaled and cropped version of input.jpg + }); +``` +**Example** +```js +// Read image data from remote URL, +// resize to 300 pixels wide, +// emit an 'info' event with calculated dimensions +// and finally write image data to writableStream +const { body } = fetch('https://...'); +const readableStream = Readable.fromWeb(body); +const transformer = sharp() + .resize(300) + .on('info', ({ height }) => { + console.log(`Image height is ${height}`); + }); +readableStream.pipe(transformer).pipe(writableStream); +``` +**Example** +```js +// Create a blank 300x200 PNG image of semi-translucent red pixels +sharp({ + create: { + width: 300, + height: 200, + channels: 4, + background: { r: 255, g: 0, b: 0, alpha: 0.5 } + } +}) +.png() +.toBuffer() +.then( ... ); +``` +**Example** +```js +// Convert an animated GIF to an animated WebP +await sharp('in.gif', { animated: true }).toFile('out.webp'); +``` +**Example** +```js +// 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** +```js +// Generate RGB Gaussian noise +await sharp({ + create: { + width: 300, + height: 200, + channels: 3, + noise: { + type: 'gaussian', + mean: 128, + sigma: 30 + } + } +}).toFile('noise.png'); +``` +**Example** +```js +// Generate an image from text +await sharp({ + text: { + text: 'Hello, world!', + width: 400, // max width + height: 300 // max height + } +}).toFile('text_bw.png'); +``` +**Example** +```js +// Generate an rgba image from text using pango markup and font +await sharp({ + text: { + text: '<span foreground="red">Red!</span><span background="cyan">blue</span>', + font: 'sans', + rgba: true, + dpi: 300 + } +}).toFile('text_rgba.png'); +``` +**Example** +```js +// Join four input images as a 2x2 grid with a 4 pixel gutter +const data = await sharp( + [image1, image2, image3, image4], + { join: { across: 2, shim: 4 } } +).toBuffer(); +``` +**Example** +```js +// Generate a two-frame animated image from emoji +const images = ['😀', '😛'].map(text => ({ + text: { text, width: 64, height: 64, channels: 4, rgba: true } +})); +await sharp(images, { join: { animated: true } }).toFile('out.gif'); +``` + + +## clone +> clone() ⇒ [<code>Sharp</code>](#Sharp) + +Take a "snapshot" of the Sharp instance, returning a new instance. +Cloned instances inherit the input of their parent instance. +This allows multiple output Streams and therefore multiple processing pipelines to share a single input Stream. + + +**Example** +```js +const pipeline = sharp().rotate(); +pipeline.clone().resize(800, 600).pipe(firstWritableStream); +pipeline.clone().extract({ left: 20, top: 20, width: 100, height: 100 }).pipe(secondWritableStream); +readableStream.pipe(pipeline); +// firstWritableStream receives auto-rotated, resized readableStream +// secondWritableStream receives auto-rotated, extracted region of readableStream +``` +**Example** +```js +// Create a pipeline that will download an image, resize it and format it to different files +// Using Promises to know when the pipeline is complete +const fs = require("fs"); +const got = require("got"); +const sharpStream = sharp({ failOn: 'none' }); + +const promises = []; + +promises.push( + sharpStream + .clone() + .jpeg({ quality: 100 }) + .toFile("originalFile.jpg") +); + +promises.push( + sharpStream + .clone() + .resize({ width: 500 }) + .jpeg({ quality: 80 }) + .toFile("optimized-500.jpg") +); + +promises.push( + sharpStream + .clone() + .resize({ width: 500 }) + .webp({ quality: 80 }) + .toFile("optimized-500.webp") +); + +// https://github.com/sindresorhus/got/blob/main/documentation/3-streams.md +got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream); + +Promise.all(promises) + .then(res => { console.log("Done!", res); }) + .catch(err => { + console.error("Error processing files, let's clean it up", err); + try { + fs.unlinkSync("originalFile.jpg"); + fs.unlinkSync("optimized-500.jpg"); + fs.unlinkSync("optimized-500.webp"); + } catch (e) {} + }); +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-input.md b/docs/src/content/docs/api-input.md new file mode 100644 index 000000000..8477f1ff5 --- /dev/null +++ b/docs/src/content/docs/api-input.md @@ -0,0 +1,139 @@ +--- +# This file was auto-generated from JSDoc in lib/input.js +title: Input metadata +--- + +## metadata +> metadata([callback]) ⇒ <code>Promise.<Object></code> \| <code>Sharp</code> + +Fast access to (uncached) image metadata without decoding any compressed pixel data. + +This is read from the header of the input image. +It does not take into consideration any operations to be applied to the output image, +such as resize or rotate. + +Dimensions in the response will respect the `page` and `pages` properties of the +[constructor parameters](/api-constructor/). + +A `Promise` is returned when `callback` is not provided. + +- `format`: Name of decoder used to parse image e.g. `jpeg`, `png`, `webp`, `gif`, `svg`, `heif`, `tiff` +- `size`: Total size of image in bytes, for Stream and Buffer input only +- `width`: Number of pixels wide (EXIF orientation is not taken into consideration, see example below) +- `height`: Number of pixels high (EXIF orientation is not taken into consideration, see example below) +- `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html) +- `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK +- `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://www.libvips.org/API/current/enum.BandFormat.html) +- `density`: Number of pixels per inch (DPI), if present +- `chromaSubsampling`: String containing JPEG chroma subsampling, `4:2:0` or `4:4:4` for RGB, `4:2:0:4` or `4:4:4:4` for CMYK +- `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan +- `isPalette`: Boolean indicating whether the image is palette-based (GIF, PNG). +- `bitsPerSample`: Number of bits per sample for each channel (GIF, PNG, HEIF). +- `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP +- `pageHeight`: Number of pixels high each page in a multi-page image will be. +- `loop`: Number of times to loop an animated image, zero refers to a continuous loop. +- `delay`: Delay in ms between each page in an animated image, provided as an array of integers. +- `pagePrimary`: Number of the primary page in a HEIF image +- `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide +- `subifds`: Number of Sub Image File Directories in an OME-TIFF image +- `background`: Default background colour, if present, for PNG (bKGD) and GIF images +- `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) +- `resolutionUnit`: The unit of resolution (density), either `inch` or `cm`, if present +- `hasProfile`: Boolean indicating the presence of an embedded ICC profile +- `hasAlpha`: Boolean indicating the presence of an alpha transparency channel +- `orientation`: Number value of the EXIF Orientation header, if present +- `exif`: Buffer containing raw EXIF data, if present +- `icc`: Buffer containing raw [ICC](https://www.npmjs.com/package/icc) profile data, if present +- `iptc`: Buffer containing raw IPTC data, if present +- `xmp`: Buffer containing raw XMP data, if present +- `xmpAsString`: String containing XMP data, if valid UTF-8. +- `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present +- `formatMagick`: String containing format for images loaded via *magick +- `comments`: Array of keyword/text pairs representing PNG text blocks, if present. + + + +| Param | Type | Description | +| --- | --- | --- | +| [callback] | <code>function</code> | called with the arguments `(err, metadata)` | + +**Example** +```js +const metadata = await sharp(input).metadata(); +``` +**Example** +```js +const image = sharp(inputJpg); +image + .metadata() + .then(function(metadata) { + return image + .resize(Math.round(metadata.width / 2)) + .webp() + .toBuffer(); + }) + .then(function(data) { + // data contains a WebP image half the width and height of the original JPEG + }); +``` +**Example** +```js +// Get dimensions taking EXIF Orientation into account. +const { autoOrient } = await sharp(input).metadata(); +const { width, height } = autoOrient; +``` + + +## stats +> stats([callback]) ⇒ <code>Promise.<Object></code> + +Access to pixel-derived image statistics for every channel in the image. +A `Promise` is returned when `callback` is not provided. + +- `channels`: Array of channel statistics for each channel in the image. Each channel statistic contains + - `min` (minimum value in the channel) + - `max` (maximum value in the channel) + - `sum` (sum of all values in a channel) + - `squaresSum` (sum of squared values in a channel) + - `mean` (mean of the values in a channel) + - `stdev` (standard deviation for the values in a channel) + - `minX` (x-coordinate of one of the pixel where the minimum lies) + - `minY` (y-coordinate of one of the pixel where the minimum lies) + - `maxX` (x-coordinate of one of the pixel where the maximum lies) + - `maxY` (y-coordinate of one of the pixel where the maximum lies) +- `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque. +- `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any. +- `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any. +- `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram. + +**Note**: Statistics are derived from the original input image. Any operations performed on the image must first be +written to a buffer in order to run `stats` on the result (see third example). + + + +| Param | Type | Description | +| --- | --- | --- | +| [callback] | <code>function</code> | called with the arguments `(err, stats)` | + +**Example** +```js +const image = sharp(inputJpg); +image + .stats() + .then(function(stats) { + // stats contains the channel-wise statistics array and the isOpaque value + }); +``` +**Example** +```js +const { entropy, sharpness, dominant } = await sharp(input).stats(); +const { r, g, b } = dominant; +``` +**Example** +```js +const image = sharp(input); +// store intermediate result +const part = await image.extract(region).toBuffer(); +// create new instance to obtain statistics of extracted region +const stats = await sharp(part).stats(); +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-operation.md b/docs/src/content/docs/api-operation.md new file mode 100644 index 000000000..abcb41fc1 --- /dev/null +++ b/docs/src/content/docs/api-operation.md @@ -0,0 +1,723 @@ +--- +# This file was auto-generated from JSDoc in lib/operation.js +title: Image operations +--- + +## rotate +> rotate([angle], [options]) ⇒ <code>Sharp</code> + +Rotate the output image. + +The provided angle is converted to a valid positive degree rotation. +For example, `-450` will produce a 270 degree rotation. + +When rotating by an angle other than a multiple of 90, +the background colour can be provided with the `background` option. + +For backwards compatibility, if no angle is provided, `.autoOrient()` will be called. + +Only one rotation can occur per pipeline (aside from an initial call without +arguments to orient via EXIF data). Previous calls to `rotate` in the same +pipeline will be ignored. + +Multi-page images can only be rotated by 180 degrees. + +Method order is important when rotating, resizing and/or extracting regions, +for example `.rotate(x).extract(y)` will produce a different result to `.extract(y).rotate(x)`. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [angle] | <code>number</code> | <code>auto</code> | angle of rotation. | +| [options] | <code>Object</code> | | if present, is an Object with optional attributes. | +| [options.background] | <code>string</code> \| <code>Object</code> | <code>"\"#000000\""</code> | parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. | + +**Example** +```js +const rotateThenResize = await sharp(input) + .rotate(90) + .resize({ width: 16, height: 8, fit: 'fill' }) + .toBuffer(); +const resizeThenRotate = await sharp(input) + .resize({ width: 16, height: 8, fit: 'fill' }) + .rotate(90) + .toBuffer(); +``` + + +## autoOrient +> autoOrient() ⇒ <code>Sharp</code> + +Auto-orient based on the EXIF `Orientation` tag, then remove the tag. +Mirroring is supported and may infer the use of a flip operation. + +Previous or subsequent use of `rotate(angle)` and either `flip()` or `flop()` +will logically occur after auto-orientation, regardless of call order. + + +**Example** +```js +const output = await sharp(input).autoOrient().toBuffer(); +``` +**Example** +```js +const pipeline = sharp() + .autoOrient() + .resize(null, 200) + .toBuffer(function (err, outputBuffer, info) { + // outputBuffer contains 200px high JPEG image data, + // auto-oriented using EXIF Orientation tag + // info.width and info.height contain the dimensions of the resized image + }); +readableStream.pipe(pipeline); +``` + + +## flip +> flip([flip]) ⇒ <code>Sharp</code> + +Mirror the image vertically (up-down) about the x-axis. +This always occurs before rotation, if any. + +This operation does not work correctly with multi-page images. + + + +| Param | Type | Default | +| --- | --- | --- | +| [flip] | <code>Boolean</code> | <code>true</code> | + +**Example** +```js +const output = await sharp(input).flip().toBuffer(); +``` + + +## flop +> flop([flop]) ⇒ <code>Sharp</code> + +Mirror the image horizontally (left-right) about the y-axis. +This always occurs before rotation, if any. + + + +| Param | Type | Default | +| --- | --- | --- | +| [flop] | <code>Boolean</code> | <code>true</code> | + +**Example** +```js +const output = await sharp(input).flop().toBuffer(); +``` + + +## affine +> affine(matrix, [options]) ⇒ <code>Sharp</code> + +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 colour with the `background` option. +A particular interpolator may also be specified. Set the `interpolator` option to an attribute of the `sharp.interpolators` Object e.g. `sharp.interpolators.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. + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.27.0 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| matrix | <code>Array.<Array.<number>></code> \| <code>Array.<number></code> | | affine transformation matrix | +| [options] | <code>Object</code> | | if present, is an Object with optional attributes. | +| [options.background] | <code>String</code> \| <code>Object</code> | <code>"#000000"</code> | parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. | +| [options.idx] | <code>Number</code> | <code>0</code> | input horizontal offset | +| [options.idy] | <code>Number</code> | <code>0</code> | input vertical offset | +| [options.odx] | <code>Number</code> | <code>0</code> | output horizontal offset | +| [options.ody] | <code>Number</code> | <code>0</code> | output vertical offset | +| [options.interpolator] | <code>String</code> | <code>sharp.interpolators.bicubic</code> | interpolator | + +**Example** +```js +const pipeline = sharp() + .affine([[1, 0.3], [0.1, 0.7]], { + background: 'white', + interpolator: sharp.interpolators.nohalo + }) + .toBuffer((err, outputBuffer, info) => { + // outputBuffer contains the transformed image + // info.width and info.height contain the new dimensions + }); + +inputStream + .pipe(pipeline); +``` + + +## sharpen +> sharpen([options], [flat], [jagged]) ⇒ <code>Sharp</code> + +Sharpen the image. + +When used without parameters, performs a fast, mild sharpen of the output image. + +When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space. +Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available. + +See [libvips sharpen](https://www.libvips.org/API/current/method.Image.sharpen.html) operation. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> \| <code>number</code> | | if present, is an Object with attributes | +| [options.sigma] | <code>number</code> | | the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`, between 0.000001 and 10 | +| [options.m1] | <code>number</code> | <code>1.0</code> | the level of sharpening to apply to "flat" areas, between 0 and 1000000 | +| [options.m2] | <code>number</code> | <code>2.0</code> | the level of sharpening to apply to "jagged" areas, between 0 and 1000000 | +| [options.x1] | <code>number</code> | <code>2.0</code> | threshold between "flat" and "jagged", between 0 and 1000000 | +| [options.y2] | <code>number</code> | <code>10.0</code> | maximum amount of brightening, between 0 and 1000000 | +| [options.y3] | <code>number</code> | <code>20.0</code> | maximum amount of darkening, between 0 and 1000000 | +| [flat] | <code>number</code> | | (deprecated) see `options.m1`. | +| [jagged] | <code>number</code> | | (deprecated) see `options.m2`. | + +**Example** +```js +const data = await sharp(input).sharpen().toBuffer(); +``` +**Example** +```js +const data = await sharp(input).sharpen({ sigma: 2 }).toBuffer(); +``` +**Example** +```js +const data = await sharp(input) + .sharpen({ + sigma: 2, + m1: 0, + m2: 3, + x1: 3, + y2: 15, + y3: 15, + }) + .toBuffer(); +``` + + +## median +> median([size]) ⇒ <code>Sharp</code> + +Apply median filter. +When used without parameters the default window is 3x3. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [size] | <code>number</code> | <code>3</code> | square mask size: size x size | + +**Example** +```js +const output = await sharp(input).median().toBuffer(); +``` +**Example** +```js +const output = await sharp(input).median(5).toBuffer(); +``` + + +## blur +> blur([options]) ⇒ <code>Sharp</code> + +Blur the image. + +When used without parameters, performs a fast 3x3 box blur (equivalent to a box linear filter). + +When a `sigma` is provided, performs a slower, more accurate Gaussian blur. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> \| <code>number</code> \| <code>Boolean</code> | | | +| [options.sigma] | <code>number</code> | | a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. | +| [options.precision] | <code>string</code> | <code>"'integer'"</code> | How accurate the operation should be, one of: integer, float, approximate. | +| [options.minAmplitude] | <code>number</code> | <code>0.2</code> | A value between 0.001 and 1. A smaller value will generate a larger, more accurate mask. | + +**Example** +```js +const boxBlurred = await sharp(input) + .blur() + .toBuffer(); +``` +**Example** +```js +const gaussianBlurred = await sharp(input) + .blur(5) + .toBuffer(); +``` + + +## dilate +> dilate([width]) ⇒ <code>Sharp</code> + +Expand foreground objects using the dilate morphological operator. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [width] | <code>Number</code> | <code>1</code> | dilation width in pixels. | + +**Example** +```js +const output = await sharp(input) + .dilate() + .toBuffer(); +``` + + +## erode +> erode([width]) ⇒ <code>Sharp</code> + +Shrink foreground objects using the erode morphological operator. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [width] | <code>Number</code> | <code>1</code> | erosion width in pixels. | + +**Example** +```js +const output = await sharp(input) + .erode() + .toBuffer(); +``` + + +## flatten +> flatten([options]) ⇒ <code>Sharp</code> + +Merge alpha transparency channel, if any, with a background, then remove the alpha channel. + +See also [removeAlpha](/api-channel#removealpha). + + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.background] | <code>string</code> \| <code>Object</code> | <code>"{r: 0, g: 0, b: 0}"</code> | background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black. | + +**Example** +```js +await sharp(rgbaInput) + .flatten({ background: '#F0A703' }) + .toBuffer(); +``` + + +## unflatten +> unflatten() + +Ensure the image has an alpha channel +with all white pixel values made fully transparent. + +Existing alpha channel values for non-white pixels remain unchanged. + +This feature is experimental and the API may change. + + +**Since**: 0.32.1 +**Example** +```js +await sharp(rgbInput) + .unflatten() + .toBuffer(); +``` +**Example** +```js +await sharp(rgbInput) + .threshold(128, { grayscale: false }) // converter bright pixels to white + .unflatten() + .toBuffer(); +``` + + +## gamma +> gamma([gamma], [gammaOut]) ⇒ <code>Sharp</code> + +Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of `1/gamma` +then increasing the encoding (brighten) post-resize at a factor of `gamma`. +This can improve the perceived brightness of a resized image in non-linear colour spaces. +JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation +when applying a gamma correction. + +Supply a second argument to use a different output gamma value, otherwise the first value is used in both cases. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [gamma] | <code>number</code> | <code>2.2</code> | value between 1.0 and 3.0. | +| [gammaOut] | <code>number</code> | | value between 1.0 and 3.0. (optional, defaults to same as `gamma`) | + + + +## negate +> negate([options]) ⇒ <code>Sharp</code> + +Produce the "negative" of the image. + + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.alpha] | <code>Boolean</code> | <code>true</code> | Whether or not to negate any alpha channel | + +**Example** +```js +const output = await sharp(input) + .negate() + .toBuffer(); +``` +**Example** +```js +const output = await sharp(input) + .negate({ alpha: false }) + .toBuffer(); +``` + + +## normalise +> normalise([options]) ⇒ <code>Sharp</code> + +Enhance output image contrast by stretching its luminance to cover a full dynamic range. + +Uses a histogram-based approach, taking a default range of 1% to 99% to reduce sensitivity to noise at the extremes. + +Luminance values below the `lower` percentile will be underexposed by clipping to zero. +Luminance values above the `upper` percentile will be overexposed by clipping to the max pixel value. + + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.lower] | <code>number</code> | <code>1</code> | Percentile below which luminance values will be underexposed. | +| [options.upper] | <code>number</code> | <code>99</code> | Percentile above which luminance values will be overexposed. | + +**Example** +```js +const output = await sharp(input) + .normalise() + .toBuffer(); +``` +**Example** +```js +const output = await sharp(input) + .normalise({ lower: 0, upper: 100 }) + .toBuffer(); +``` + + +## normalize +> normalize([options]) ⇒ <code>Sharp</code> + +Alternative spelling of normalise. + + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.lower] | <code>number</code> | <code>1</code> | Percentile below which luminance values will be underexposed. | +| [options.upper] | <code>number</code> | <code>99</code> | Percentile above which luminance values will be overexposed. | + +**Example** +```js +const output = await sharp(input) + .normalize() + .toBuffer(); +``` + + +## clahe +> clahe(options) ⇒ <code>Sharp</code> + +Perform contrast limiting adaptive histogram equalization +[CLAHE](https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE). + +This will, in general, enhance the clarity of the image by bringing out darker details. + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.28.3 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| options | <code>Object</code> | | | +| options.width | <code>number</code> | | Integral width of the search window, in pixels. | +| options.height | <code>number</code> | | Integral height of the search window, in pixels. | +| [options.maxSlope] | <code>number</code> | <code>3</code> | Integral level of brightening, between 0 and 100, where 0 disables contrast limiting. | + +**Example** +```js +const output = await sharp(input) + .clahe({ + width: 3, + height: 3, + }) + .toBuffer(); +``` + + +## convolve +> convolve(kernel) ⇒ <code>Sharp</code> + +Convolve the image with the specified kernel. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| kernel | <code>Object</code> | | | +| kernel.width | <code>number</code> | | width of the kernel in pixels. | +| kernel.height | <code>number</code> | | height of the kernel in pixels. | +| kernel.kernel | <code>Array.<number></code> | | Array of length `width*height` containing the kernel values. | +| [kernel.scale] | <code>number</code> | <code>sum</code> | the scale of the kernel in pixels. | +| [kernel.offset] | <code>number</code> | <code>0</code> | the offset of the kernel in pixels. | + +**Example** +```js +sharp(input) + .convolve({ + width: 3, + height: 3, + kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1] + }) + .raw() + .toBuffer(function(err, data, info) { + // data contains the raw pixel data representing the convolution + // of the input image with the horizontal Sobel operator + }); +``` + + +## threshold +> threshold([threshold], [options]) ⇒ <code>Sharp</code> + +Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [threshold] | <code>number</code> | <code>128</code> | a value in the range 0-255 representing the level at which the threshold will be applied. | +| [options] | <code>Object</code> | | | +| [options.greyscale] | <code>Boolean</code> | <code>true</code> | convert to single channel greyscale. | +| [options.grayscale] | <code>Boolean</code> | <code>true</code> | alternative spelling for greyscale. | + + + +## boolean +> boolean(operand, operator, [options]) ⇒ <code>Sharp</code> + +Perform a bitwise boolean operation with operand image. + +This operation creates an output image where each pixel is the result of +the selected bitwise boolean `operation` between the corresponding pixels of the input images. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| operand | <code>Buffer</code> \| <code>string</code> | Buffer containing image data or string containing the path to an image file. | +| operator | <code>string</code> | one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively. | +| [options] | <code>Object</code> | | +| [options.raw] | <code>Object</code> | describes operand when using raw pixel data. | +| [options.raw.width] | <code>number</code> | | +| [options.raw.height] | <code>number</code> | | +| [options.raw.channels] | <code>number</code> | | + + + +## linear +> linear([a], [b]) ⇒ <code>Sharp</code> + +Apply the linear formula `a` * input + `b` to the image to adjust image levels. + +When a single number is provided, it will be used for all image channels. +When an array of numbers is provided, the array length must match the number of channels. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [a] | <code>number</code> \| <code>Array.<number></code> | <code>[]</code> | multiplier | +| [b] | <code>number</code> \| <code>Array.<number></code> | <code>[]</code> | offset | + +**Example** +```js +await sharp(input) + .linear(0.5, 2) + .toBuffer(); +``` +**Example** +```js +await sharp(rgbInput) + .linear( + [0.25, 0.5, 0.75], + [150, 100, 50] + ) + .toBuffer(); +``` + + +## recomb +> recomb(inputMatrix) ⇒ <code>Sharp</code> + +Recombine the image with the specified matrix. + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.21.1 + +| Param | Type | Description | +| --- | --- | --- | +| inputMatrix | <code>Array.<Array.<number>></code> | 3x3 or 4x4 Recombination matrix | + +**Example** +```js +sharp(input) + .recomb([ + [0.3588, 0.7044, 0.1368], + [0.2990, 0.5870, 0.1140], + [0.2392, 0.4696, 0.0912], + ]) + .raw() + .toBuffer(function(err, data, info) { + // data contains the raw pixel data after applying the matrix + // With this example input, a sepia filter has been applied + }); +``` + + +## modulate +> modulate([options]) ⇒ <code>Sharp</code> + +Transforms the image using brightness, saturation, hue rotation, and lightness. +Brightness and lightness both operate on luminance, with the difference being that +brightness is multiplicative whereas lightness is additive. + + +**Since**: 0.22.1 + +| Param | Type | Description | +| --- | --- | --- | +| [options] | <code>Object</code> | | +| [options.brightness] | <code>number</code> | Brightness multiplier | +| [options.saturation] | <code>number</code> | Saturation multiplier | +| [options.hue] | <code>number</code> | Degrees for hue rotation | +| [options.lightness] | <code>number</code> | Lightness addend | + +**Example** +```js +// increase brightness by a factor of 2 +const output = await sharp(input) + .modulate({ + brightness: 2 + }) + .toBuffer(); +``` +**Example** +```js +// hue-rotate by 180 degrees +const output = await sharp(input) + .modulate({ + hue: 180 + }) + .toBuffer(); +``` +**Example** +```js +// increase lightness by +50 +const output = await sharp(input) + .modulate({ + lightness: 50 + }) + .toBuffer(); +``` +**Example** +```js +// decrease brightness and saturation while also hue-rotating by 90 degrees +const output = await sharp(input) + .modulate({ + brightness: 0.5, + saturation: 0.5, + hue: 90, + }) + .toBuffer(); +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-output.md b/docs/src/content/docs/api-output.md new file mode 100644 index 000000000..5acd2fadb --- /dev/null +++ b/docs/src/content/docs/api-output.md @@ -0,0 +1,911 @@ +--- +# This file was auto-generated from JSDoc in lib/output.js +title: Output options +--- + +## toFile +> toFile(fileOut, [callback]) ⇒ <code>Promise.<Object></code> + +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, AVIF, TIFF, GIF, 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. +See [withMetadata](#withmetadata) for control over this. + +The caller is responsible for ensuring directory structures and permissions exist. + +A `Promise` is returned when `callback` is not provided. + + +**Returns**: <code>Promise.<Object></code> - - when no callback is provided +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| fileOut | <code>string</code> | the path to write the image data to. | +| [callback] | <code>function</code> | called on completion with two arguments `(err, info)`. `info` contains the output image `format`, `size` (bytes), `width`, `height`, `channels` and `premultiplied` (indicating if premultiplication was used). When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. When using the attention crop strategy also contains `attentionX` and `attentionY`, the focal point of the cropped region. Animated output will also contain `pageHeight` and `pages`. May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text. | + +**Example** +```js +sharp(input) + .toFile('output.png', (err, info) => { ... }); +``` +**Example** +```js +sharp(input) + .toFile('output.png') + .then(info => { ... }) + .catch(err => { ... }); +``` + + +## toBuffer +> toBuffer([options], [callback]) ⇒ <code>Promise.<Buffer></code> + +Write output to a Buffer. +JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported. + +Use [toFormat](#toformat) or one of the format-specific functions such as [jpeg](#jpeg), [png](#png) etc. to set the output format. + +If no explicit format is set, the output format will match the input image, except SVG input which becomes PNG output. + +By default all metadata will be removed, which includes EXIF-based orientation. +See [withMetadata](#withmetadata) for control over this. + +`callback`, if present, gets three arguments `(err, data, info)` where: +- `err` is an error, if any. +- `data` is the output image data. +- `info` contains the output image `format`, `size` (bytes), `width`, `height`, +`channels` and `premultiplied` (indicating if premultiplication was used). +When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. +Animated output will also contain `pageHeight` and `pages`. +May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text. + +A `Promise` is returned when `callback` is not provided. + + +**Returns**: <code>Promise.<Buffer></code> - - when no callback is provided + +| Param | Type | Description | +| --- | --- | --- | +| [options] | <code>Object</code> | | +| [options.resolveWithObject] | <code>boolean</code> | Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`. | +| [callback] | <code>function</code> | | + +**Example** +```js +sharp(input) + .toBuffer((err, data, info) => { ... }); +``` +**Example** +```js +sharp(input) + .toBuffer() + .then(data => { ... }) + .catch(err => { ... }); +``` +**Example** +```js +sharp(input) + .png() + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { ... }) + .catch(err => { ... }); +``` +**Example** +```js +const { data, info } = await sharp('my-image.jpg') + // output the raw pixels + .raw() + .toBuffer({ resolveWithObject: true }); + +// 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 +const { width, height, channels } = info; +await sharp(pixelArray, { raw: { width, height, channels } }) + .toFile('my-changed-image.jpg'); +``` + + +## keepExif +> keepExif() ⇒ <code>Sharp</code> + +Keep all EXIF metadata from the input image in the output image. + +EXIF metadata is unsupported for TIFF output. + + +**Since**: 0.33.0 +**Example** +```js +const outputWithExif = await sharp(inputWithExif) + .keepExif() + .toBuffer(); +``` + + +## withExif +> withExif(exif) ⇒ <code>Sharp</code> + +Set EXIF metadata in the output image, ignoring any EXIF in the input image. + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.33.0 + +| Param | Type | Description | +| --- | --- | --- | +| exif | <code>Object.<string, Object.<string, string>></code> | Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. | + +**Example** +```js +const dataWithExif = await sharp(input) + .withExif({ + IFD0: { + Copyright: 'The National Gallery' + }, + IFD3: { + GPSLatitudeRef: 'N', + GPSLatitude: '51/1 30/1 3230/100', + GPSLongitudeRef: 'W', + GPSLongitude: '0/1 7/1 4366/100' + } + }) + .toBuffer(); +``` + + +## withExifMerge +> withExifMerge(exif) ⇒ <code>Sharp</code> + +Update EXIF metadata from the input image in the output image. + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.33.0 + +| Param | Type | Description | +| --- | --- | --- | +| exif | <code>Object.<string, Object.<string, string>></code> | Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. | + +**Example** +```js +const dataWithMergedExif = await sharp(inputWithExif) + .withExifMerge({ + IFD0: { + Copyright: 'The National Gallery' + } + }) + .toBuffer(); +``` + + +## keepIccProfile +> keepIccProfile() ⇒ <code>Sharp</code> + +Keep ICC profile from the input image in the output image. + +When input and output colour spaces differ, use with [toColourspace](/api-colour/#tocolourspace) and optionally [pipelineColourspace](/api-colour/#pipelinecolourspace). + + +**Since**: 0.33.0 +**Example** +```js +const outputWithIccProfile = await sharp(inputWithIccProfile) + .keepIccProfile() + .toBuffer(); +``` +**Example** +```js +const cmykOutputWithIccProfile = await sharp(cmykInputWithIccProfile) + .pipelineColourspace('cmyk') + .toColourspace('cmyk') + .keepIccProfile() + .toBuffer(); +``` + + +## withIccProfile +> withIccProfile(icc, [options]) ⇒ <code>Sharp</code> + +Transform using an ICC profile and attach to the output image. + +This can either be an absolute filesystem path or +built-in profile name (`srgb`, `p3`, `cmyk`). + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.33.0 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| icc | <code>string</code> | | Absolute filesystem path to output ICC profile or built-in profile name (srgb, p3, cmyk). | +| [options] | <code>Object</code> | | | +| [options.attach] | <code>number</code> | <code>true</code> | Should the ICC profile be included in the output image metadata? | + +**Example** +```js +const outputWithP3 = await sharp(input) + .withIccProfile('p3') + .toBuffer(); +``` + + +## keepXmp +> keepXmp() ⇒ <code>Sharp</code> + +Keep XMP metadata from the input image in the output image. + + +**Since**: 0.34.3 +**Example** +```js +const outputWithXmp = await sharp(inputWithXmp) + .keepXmp() + .toBuffer(); +``` + + +## withXmp +> withXmp(xmp) ⇒ <code>Sharp</code> + +Set XMP metadata in the output image. + +Supported by PNG, JPEG, WebP, and TIFF output. + + +**Throws**: + +- <code>Error</code> Invalid parameters + +**Since**: 0.34.3 + +| Param | Type | Description | +| --- | --- | --- | +| xmp | <code>string</code> | String containing XMP metadata to be embedded in the output image. | + +**Example** +```js +const xmpString = ` + <?xml version="1.0"?> + <x:xmpmeta xmlns:x="adobe:ns:meta/"> + <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"> + <dc:creator><rdf:Seq><rdf:li>John Doe</rdf:li></rdf:Seq></dc:creator> + </rdf:Description> + </rdf:RDF> + </x:xmpmeta>`; + +const data = await sharp(input) + .withXmp(xmpString) + .toBuffer(); +``` + + +## keepMetadata +> keepMetadata() ⇒ <code>Sharp</code> + +Keep all metadata (EXIF, ICC, XMP, IPTC) from the input image in the output image. + +The default behaviour, when `keepMetadata` is not used, is to convert to the device-independent +sRGB colour space and strip all metadata, including the removal of any ICC profile. + + +**Since**: 0.33.0 +**Example** +```js +const outputWithMetadata = await sharp(inputWithMetadata) + .keepMetadata() + .toBuffer(); +``` + + +## withMetadata +> withMetadata([options]) ⇒ <code>Sharp</code> + +Keep most metadata (EXIF, XMP, IPTC) from the input image in the output image. + +This will also convert to and add a web-friendly sRGB ICC profile if appropriate. + +Allows orientation and density to be set or updated. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| [options] | <code>Object</code> | | +| [options.orientation] | <code>number</code> | Used to update the EXIF `Orientation` tag, integer between 1 and 8. | +| [options.density] | <code>number</code> | Number of pixels per inch (DPI). | + +**Example** +```js +const outputSrgbWithMetadata = await sharp(inputRgbWithMetadata) + .withMetadata() + .toBuffer(); +``` +**Example** +```js +// Set output metadata to 96 DPI +const data = await sharp(input) + .withMetadata({ density: 96 }) + .toBuffer(); +``` + + +## toFormat +> toFormat(format, options) ⇒ <code>Sharp</code> + +Force output to a given format. + + +**Throws**: + +- <code>Error</code> unsupported format or options + + +| Param | Type | Description | +| --- | --- | --- | +| format | <code>string</code> \| <code>Object</code> | as a string or an Object with an 'id' attribute | +| options | <code>Object</code> | output options | + +**Example** +```js +// Convert any input to PNG output +const data = await sharp(input) + .toFormat('png') + .toBuffer(); +``` + + +## jpeg +> jpeg([options]) ⇒ <code>Sharp</code> + +Use these JPEG options for output image. + + +**Throws**: + +- <code>Error</code> Invalid options + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.quality] | <code>number</code> | <code>80</code> | quality, integer 1-100 | +| [options.progressive] | <code>boolean</code> | <code>false</code> | use progressive (interlace) scan | +| [options.chromaSubsampling] | <code>string</code> | <code>"'4:2:0'"</code> | set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling | +| [options.optimiseCoding] | <code>boolean</code> | <code>true</code> | optimise Huffman coding tables | +| [options.optimizeCoding] | <code>boolean</code> | <code>true</code> | alternative spelling of optimiseCoding | +| [options.mozjpeg] | <code>boolean</code> | <code>false</code> | use mozjpeg defaults, equivalent to `{ trellisQuantisation: true, overshootDeringing: true, optimiseScans: true, quantisationTable: 3 }` | +| [options.trellisQuantisation] | <code>boolean</code> | <code>false</code> | apply trellis quantisation | +| [options.overshootDeringing] | <code>boolean</code> | <code>false</code> | apply overshoot deringing | +| [options.optimiseScans] | <code>boolean</code> | <code>false</code> | optimise progressive scans, forces progressive | +| [options.optimizeScans] | <code>boolean</code> | <code>false</code> | alternative spelling of optimiseScans | +| [options.quantisationTable] | <code>number</code> | <code>0</code> | quantization table to use, integer 0-8 | +| [options.quantizationTable] | <code>number</code> | <code>0</code> | alternative spelling of quantisationTable | +| [options.force] | <code>boolean</code> | <code>true</code> | force JPEG output, otherwise attempt to use input format | + +**Example** +```js +// Convert any input to very high quality JPEG output +const data = await sharp(input) + .jpeg({ + quality: 100, + chromaSubsampling: '4:4:4' + }) + .toBuffer(); +``` +**Example** +```js +// Use mozjpeg to reduce output JPEG file size (slower) +const data = await sharp(input) + .jpeg({ mozjpeg: true }) + .toBuffer(); +``` + + +## png +> png([options]) ⇒ <code>Sharp</code> + +Use these PNG options for output image. + +By default, PNG output is full colour at 8 bits per pixel. + +Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel. +Set `palette` to `true` for slower, indexed PNG output. + +For 16 bits per pixel output, convert to `rgb16` via +[toColourspace](/api-colour/#tocolourspace). + + +**Throws**: + +- <code>Error</code> Invalid options + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.progressive] | <code>boolean</code> | <code>false</code> | use progressive (interlace) scan | +| [options.compressionLevel] | <code>number</code> | <code>6</code> | zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest) | +| [options.adaptiveFiltering] | <code>boolean</code> | <code>false</code> | use adaptive row filtering | +| [options.palette] | <code>boolean</code> | <code>false</code> | quantise to a palette-based image with alpha transparency support | +| [options.quality] | <code>number</code> | <code>100</code> | use the lowest number of colours needed to achieve given quality, sets `palette` to `true` | +| [options.effort] | <code>number</code> | <code>7</code> | CPU effort, between 1 (fastest) and 10 (slowest), sets `palette` to `true` | +| [options.colours] | <code>number</code> | <code>256</code> | maximum number of palette entries, sets `palette` to `true` | +| [options.colors] | <code>number</code> | <code>256</code> | alternative spelling of `options.colours`, sets `palette` to `true` | +| [options.dither] | <code>number</code> | <code>1.0</code> | level of Floyd-Steinberg error diffusion, sets `palette` to `true` | +| [options.force] | <code>boolean</code> | <code>true</code> | force PNG output, otherwise attempt to use input format | + +**Example** +```js +// Convert any input to full colour PNG output +const data = await sharp(input) + .png() + .toBuffer(); +``` +**Example** +```js +// Convert any input to indexed PNG output (slower) +const data = await sharp(input) + .png({ palette: true }) + .toBuffer(); +``` +**Example** +```js +// Output 16 bits per pixel RGB(A) +const data = await sharp(input) + .toColourspace('rgb16') + .png() + .toBuffer(); +``` + + +## webp +> webp([options]) ⇒ <code>Sharp</code> + +Use these WebP options for output image. + + +**Throws**: + +- <code>Error</code> Invalid options + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.quality] | <code>number</code> | <code>80</code> | quality, integer 1-100 | +| [options.alphaQuality] | <code>number</code> | <code>100</code> | quality of alpha layer, integer 0-100 | +| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression mode | +| [options.nearLossless] | <code>boolean</code> | <code>false</code> | use near_lossless compression mode | +| [options.smartSubsample] | <code>boolean</code> | <code>false</code> | use high quality chroma subsampling | +| [options.smartDeblock] | <code>boolean</code> | <code>false</code> | auto-adjust the deblocking filter, can improve low contrast edges (slow) | +| [options.preset] | <code>string</code> | <code>"'default'"</code> | named preset for preprocessing/filtering, one of: default, photo, picture, drawing, icon, text | +| [options.effort] | <code>number</code> | <code>4</code> | CPU effort, between 0 (fastest) and 6 (slowest) | +| [options.loop] | <code>number</code> | <code>0</code> | number of animation iterations, use 0 for infinite animation | +| [options.delay] | <code>number</code> \| <code>Array.<number></code> | | delay(s) between animation frames (in milliseconds) | +| [options.minSize] | <code>boolean</code> | <code>false</code> | prevent use of animation key frames to minimise file size (slow) | +| [options.mixed] | <code>boolean</code> | <code>false</code> | allow mixture of lossy and lossless animation frames (slow) | +| [options.force] | <code>boolean</code> | <code>true</code> | force WebP output, otherwise attempt to use input format | + +**Example** +```js +// Convert any input to lossless WebP output +const data = await sharp(input) + .webp({ lossless: true }) + .toBuffer(); +``` +**Example** +```js +// Optimise the file size of an animated WebP +const outputWebp = await sharp(inputWebp, { animated: true }) + .webp({ effort: 6 }) + .toBuffer(); +``` + + +## gif +> gif([options]) ⇒ <code>Sharp</code> + +Use these GIF options for the output image. + +The first entry in the palette is reserved for transparency. + +The palette of the input image will be re-used if possible. + + +**Throws**: + +- <code>Error</code> Invalid options + +**Since**: 0.30.0 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.reuse] | <code>boolean</code> | <code>true</code> | re-use existing palette, otherwise generate new (slow) | +| [options.progressive] | <code>boolean</code> | <code>false</code> | use progressive (interlace) scan | +| [options.colours] | <code>number</code> | <code>256</code> | maximum number of palette entries, including transparency, between 2 and 256 | +| [options.colors] | <code>number</code> | <code>256</code> | alternative spelling of `options.colours` | +| [options.effort] | <code>number</code> | <code>7</code> | CPU effort, between 1 (fastest) and 10 (slowest) | +| [options.dither] | <code>number</code> | <code>1.0</code> | level of Floyd-Steinberg error diffusion, between 0 (least) and 1 (most) | +| [options.interFrameMaxError] | <code>number</code> | <code>0</code> | maximum inter-frame error for transparency, between 0 (lossless) and 32 | +| [options.interPaletteMaxError] | <code>number</code> | <code>3</code> | maximum inter-palette error for palette reuse, between 0 and 256 | +| [options.keepDuplicateFrames] | <code>boolean</code> | <code>false</code> | keep duplicate frames in the output instead of combining them | +| [options.loop] | <code>number</code> | <code>0</code> | number of animation iterations, use 0 for infinite animation | +| [options.delay] | <code>number</code> \| <code>Array.<number></code> | | delay(s) between animation frames (in milliseconds) | +| [options.force] | <code>boolean</code> | <code>true</code> | force GIF output, otherwise attempt to use input format | + +**Example** +```js +// Convert PNG to GIF +await sharp(pngBuffer) + .gif() + .toBuffer(); +``` +**Example** +```js +// Convert animated WebP to animated GIF +await sharp('animated.webp', { animated: true }) + .toFile('animated.gif'); +``` +**Example** +```js +// Create a 128x128, cropped, non-dithered, animated thumbnail of an animated GIF +const out = await sharp('in.gif', { animated: true }) + .resize({ width: 128, height: 128 }) + .gif({ dither: 0 }) + .toBuffer(); +``` +**Example** +```js +// Lossy file size reduction of animated GIF +await sharp('in.gif', { animated: true }) + .gif({ interFrameMaxError: 8 }) + .toFile('optim.gif'); +``` + + +## jp2 +> jp2([options]) ⇒ <code>Sharp</code> + +Use these JP2 options for output image. + +Requires libvips compiled with support for OpenJPEG. +The prebuilt binaries do not include this - see +[installing a custom libvips](/install#custom-libvips). + + +**Throws**: + +- <code>Error</code> Invalid options + +**Since**: 0.29.1 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.quality] | <code>number</code> | <code>80</code> | quality, integer 1-100 | +| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression mode | +| [options.tileWidth] | <code>number</code> | <code>512</code> | horizontal tile size | +| [options.tileHeight] | <code>number</code> | <code>512</code> | vertical tile size | +| [options.chromaSubsampling] | <code>string</code> | <code>"'4:4:4'"</code> | set to '4:2:0' to use chroma subsampling | + +**Example** +```js +// Convert any input to lossless JP2 output +const data = await sharp(input) + .jp2({ lossless: true }) + .toBuffer(); +``` +**Example** +```js +// Convert any input to very high quality JP2 output +const data = await sharp(input) + .jp2({ + quality: 100, + chromaSubsampling: '4:4:4' + }) + .toBuffer(); +``` + + +## tiff +> tiff([options]) ⇒ <code>Sharp</code> + +Use these TIFF options for output image. + +The `density` can be set in pixels/inch via [withMetadata](#withmetadata) +instead of providing `xres` and `yres` in pixels/mm. + + +**Throws**: + +- <code>Error</code> Invalid options + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.quality] | <code>number</code> | <code>80</code> | quality, integer 1-100 | +| [options.force] | <code>boolean</code> | <code>true</code> | force TIFF output, otherwise attempt to use input format | +| [options.compression] | <code>string</code> | <code>"'jpeg'"</code> | compression options: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k | +| [options.bigtiff] | <code>boolean</code> | <code>false</code> | use BigTIFF variant (has no effect when compression is none) | +| [options.predictor] | <code>string</code> | <code>"'horizontal'"</code> | compression predictor options: none, horizontal, float | +| [options.pyramid] | <code>boolean</code> | <code>false</code> | write an image pyramid | +| [options.tile] | <code>boolean</code> | <code>false</code> | write a tiled tiff | +| [options.tileWidth] | <code>number</code> | <code>256</code> | horizontal tile size | +| [options.tileHeight] | <code>number</code> | <code>256</code> | vertical tile size | +| [options.xres] | <code>number</code> | <code>1.0</code> | horizontal resolution in pixels/mm | +| [options.yres] | <code>number</code> | <code>1.0</code> | vertical resolution in pixels/mm | +| [options.resolutionUnit] | <code>string</code> | <code>"'inch'"</code> | resolution unit options: inch, cm | +| [options.bitdepth] | <code>number</code> | <code>8</code> | reduce bitdepth to 1, 2 or 4 bit | +| [options.miniswhite] | <code>boolean</code> | <code>false</code> | write 1-bit images as miniswhite | + +**Example** +```js +// Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output +sharp('input.svg') + .tiff({ + compression: 'lzw', + bitdepth: 1 + }) + .toFile('1-bpp-output.tiff') + .then(info => { ... }); +``` + + +## avif +> avif([options]) ⇒ <code>Sharp</code> + +Use these AVIF options for output image. + +AVIF image sequences are not supported. +Prebuilt binaries support a bitdepth of 8 only. + +This feature is experimental on the Windows ARM64 platform +and requires a CPU with ARM64v8.4 or later. + + +**Throws**: + +- <code>Error</code> Invalid options + +**Since**: 0.27.0 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.quality] | <code>number</code> | <code>50</code> | quality, integer 1-100 | +| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression | +| [options.effort] | <code>number</code> | <code>4</code> | CPU effort, between 0 (fastest) and 9 (slowest) | +| [options.chromaSubsampling] | <code>string</code> | <code>"'4:4:4'"</code> | set to '4:2:0' to use chroma subsampling | +| [options.bitdepth] | <code>number</code> | <code>8</code> | set bitdepth to 8, 10 or 12 bit | + +**Example** +```js +const data = await sharp(input) + .avif({ effort: 2 }) + .toBuffer(); +``` +**Example** +```js +const data = await sharp(input) + .avif({ lossless: true }) + .toBuffer(); +``` + + +## heif +> heif(options) ⇒ <code>Sharp</code> + +Use these HEIF options for output image. + +Support for patent-encumbered HEIC images using `hevc` compression requires the use of a +globally-installed libvips compiled with support for libheif, libde265 and x265. + + +**Throws**: + +- <code>Error</code> Invalid options + +**Since**: 0.23.0 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| options | <code>Object</code> | | output options | +| options.compression | <code>string</code> | | compression format: av1, hevc | +| [options.quality] | <code>number</code> | <code>50</code> | quality, integer 1-100 | +| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression | +| [options.effort] | <code>number</code> | <code>4</code> | CPU effort, between 0 (fastest) and 9 (slowest) | +| [options.chromaSubsampling] | <code>string</code> | <code>"'4:4:4'"</code> | set to '4:2:0' to use chroma subsampling | +| [options.bitdepth] | <code>number</code> | <code>8</code> | set bitdepth to 8, 10 or 12 bit | + +**Example** +```js +const data = await sharp(input) + .heif({ compression: 'hevc' }) + .toBuffer(); +``` + + +## jxl +> jxl([options]) ⇒ <code>Sharp</code> + +Use these JPEG-XL (JXL) options for output image. + +This feature is experimental, please do not use in production systems. + +Requires libvips compiled with support for libjxl. +The prebuilt binaries do not include this - see +[installing a custom libvips](/install/#custom-libvips). + + +**Throws**: + +- <code>Error</code> Invalid options + +**Since**: 0.31.3 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.distance] | <code>number</code> | <code>1.0</code> | maximum encoding error, between 0 (highest quality) and 15 (lowest quality) | +| [options.quality] | <code>number</code> | | calculate `distance` based on JPEG-like quality, between 1 and 100, overrides distance if specified | +| [options.decodingTier] | <code>number</code> | <code>0</code> | target decode speed tier, between 0 (highest quality) and 4 (lowest quality) | +| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression | +| [options.effort] | <code>number</code> | <code>7</code> | CPU effort, between 1 (fastest) and 9 (slowest) | +| [options.loop] | <code>number</code> | <code>0</code> | number of animation iterations, use 0 for infinite animation | +| [options.delay] | <code>number</code> \| <code>Array.<number></code> | | delay(s) between animation frames (in milliseconds) | + + + +## raw +> raw([options]) ⇒ <code>Sharp</code> + +Force output to be raw, uncompressed pixel data. +Pixel ordering is left-to-right, top-to-bottom, without padding. +Channel ordering will be RGB or RGBA for non-greyscale colourspaces. + + +**Throws**: + +- <code>Error</code> Invalid options + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | output options | +| [options.depth] | <code>string</code> | <code>"'uchar'"</code> | bit depth, one of: char, uchar (default), short, ushort, int, uint, float, complex, double, dpcomplex | + +**Example** +```js +// Extract raw, unsigned 8-bit RGB pixel data from JPEG input +const { data, info } = await sharp('input.jpg') + .raw() + .toBuffer({ resolveWithObject: true }); +``` +**Example** +```js +// Extract alpha channel as raw, unsigned 16-bit pixel data from PNG input +const data = await sharp('input.png') + .ensureAlpha() + .extractChannel(3) + .toColourspace('b-w') + .raw({ depth: 'ushort' }) + .toBuffer(); +``` + + +## tile +> tile([options]) ⇒ <code>Sharp</code> + +Use tile-based deep zoom (image pyramid) output. + +Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions. +Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format. + +The container will be set to `zip` when the output is a Buffer or Stream, otherwise it will default to `fs`. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.size] | <code>number</code> | <code>256</code> | tile size in pixels, a value between 1 and 8192. | +| [options.overlap] | <code>number</code> | <code>0</code> | tile overlap in pixels, a value between 0 and 8192. | +| [options.angle] | <code>number</code> | <code>0</code> | tile angle of rotation, must be a multiple of 90. | +| [options.background] | <code>string</code> \| <code>Object</code> | <code>"{r: 255, g: 255, b: 255, alpha: 1}"</code> | background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to white without transparency. | +| [options.depth] | <code>string</code> | | how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout. | +| [options.skipBlanks] | <code>number</code> | <code>-1</code> | Threshold to skip tile generation. Range is 0-255 for 8-bit images, 0-65535 for 16-bit images. Default is 5 for `google` layout, -1 (no skip) otherwise. | +| [options.container] | <code>string</code> | <code>"'fs'"</code> | tile container, with value `fs` (filesystem) or `zip` (compressed file). | +| [options.layout] | <code>string</code> | <code>"'dz'"</code> | filesystem layout, possible values are `dz`, `iiif`, `iiif3`, `zoomify` or `google`. | +| [options.centre] | <code>boolean</code> | <code>false</code> | centre image in tile. | +| [options.center] | <code>boolean</code> | <code>false</code> | alternative spelling of centre. | +| [options.id] | <code>string</code> | <code>"'https://example.com/iiif'"</code> | when `layout` is `iiif`/`iiif3`, sets the `@id`/`id` attribute of `info.json` | +| [options.basename] | <code>string</code> | | the name of the directory within the zip file when container is `zip`. | + +**Example** +```js +sharp('input.tiff') + .png() + .tile({ + size: 512 + }) + .toFile('output.dz', function(err, info) { + // output.dzi is the Deep Zoom XML definition + // output_files contains 512x512 tiles grouped by zoom level + }); +``` +**Example** +```js +const zipFileWithTiles = await sharp(input) + .tile({ basename: "tiles" }) + .toBuffer(); +``` +**Example** +```js +const iiififier = sharp().tile({ layout: "iiif" }); +readableStream + .pipe(iiififier) + .pipe(writeableStream); +``` + + +## timeout +> timeout(options) ⇒ <code>Sharp</code> + +Set a timeout for processing, in seconds. +Use a value of zero to continue processing indefinitely, the default behaviour. + +The clock starts when libvips opens an input image for processing. +Time spent waiting for a libuv thread to become available is not included. + + +**Since**: 0.29.2 + +| Param | Type | Description | +| --- | --- | --- | +| options | <code>Object</code> | | +| options.seconds | <code>number</code> | Number of seconds after which processing will be stopped | + +**Example** +```js +// Ensure processing takes no longer than 3 seconds +try { + const data = await sharp(input) + .blur(1000) + .timeout({ seconds: 3 }) + .toBuffer(); +} catch (err) { + if (err.message.includes('timeout')) { ... } +} +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-resize.md b/docs/src/content/docs/api-resize.md new file mode 100644 index 000000000..6452c3455 --- /dev/null +++ b/docs/src/content/docs/api-resize.md @@ -0,0 +1,323 @@ +--- +# This file was auto-generated from JSDoc in lib/resize.js +title: Resizing images +--- + +## resize +> resize([width], [height], [options]) ⇒ <code>Sharp</code> + +Resize image to `width`, `height` or `width x height`. + +When both a `width` and `height` are provided, the possible methods by which the image should **fit** these are: +- `cover`: (default) Preserving aspect ratio, attempt to ensure the image covers both provided dimensions by cropping/clipping to fit. +- `contain`: Preserving aspect ratio, contain within both provided dimensions using "letterboxing" where necessary. +- `fill`: Ignore the aspect ratio of the input and stretch to both provided dimensions. +- `inside`: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified. +- `outside`: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified. + +Some of these values are based on the [object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) CSS property. + +<img alt="Examples of various values for the fit property when resizing" width="100%" style="aspect-ratio: 998/243" src="/api-resize-fit.svg"> + +When using a **fit** of `cover` or `contain`, the default **position** is `centre`. Other options are: +- `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`. +- `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`. +- `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy. + +Some of these values are based on the [object-position](https://developer.mozilla.org/en-US/docs/Web/CSS/object-position) CSS property. + +The strategy-based approach initially resizes so one dimension is at its target length +then repeatedly ranks edge regions, discarding the edge with the lowest score based on the selected strategy. +- `entropy`: focus on the region with the highest [Shannon entropy](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29). +- `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones. + +Possible downsizing kernels are: +- `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation). +- `linear`: Use a [triangle filter](https://en.wikipedia.org/wiki/Triangular_function). +- `cubic`: Use a [Catmull-Rom spline](https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline). +- `mitchell`: Use a [Mitchell-Netravali spline](https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf). +- `lanczos2`: Use a [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel) with `a=2`. +- `lanczos3`: Use a Lanczos kernel with `a=3` (the default). +- `mks2013`: Use a [Magic Kernel Sharp](https://johncostella.com/magic/mks.pdf) 2013 kernel, as adopted by Facebook. +- `mks2021`: Use a Magic Kernel Sharp 2021 kernel, with more accurate (reduced) sharpening than the 2013 version. + +When upsampling, these kernels map to `nearest`, `linear` and `cubic` interpolators. +Downsampling kernels without a matching upsampling interpolator map to `cubic`. + +Only one resize can occur per pipeline. +Previous calls to `resize` in the same pipeline will be ignored. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [width] | <code>number</code> | | How many pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. | +| [height] | <code>number</code> | | How many pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. | +| [options] | <code>Object</code> | | | +| [options.width] | <code>number</code> | | An alternative means of specifying `width`. If both are present this takes priority. | +| [options.height] | <code>number</code> | | An alternative means of specifying `height`. If both are present this takes priority. | +| [options.fit] | <code>String</code> | <code>'cover'</code> | How the image should be resized/cropped to fit the target dimension(s), one of `cover`, `contain`, `fill`, `inside` or `outside`. | +| [options.position] | <code>String</code> | <code>'centre'</code> | A position, gravity or strategy to use when `fit` is `cover` or `contain`. | +| [options.background] | <code>String</code> \| <code>Object</code> | <code>{r: 0, g: 0, b: 0, alpha: 1}</code> | background colour when `fit` is `contain`, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency. | +| [options.kernel] | <code>String</code> | <code>'lanczos3'</code> | The kernel to use for image reduction and the inferred interpolator to use for upsampling. Use the `fastShrinkOnLoad` option to control kernel vs shrink-on-load. | +| [options.withoutEnlargement] | <code>Boolean</code> | <code>false</code> | Do not scale up if the width *or* height are already less than the target dimensions, equivalent to GraphicsMagick's `>` geometry option. This may result in output dimensions smaller than the target dimensions. | +| [options.withoutReduction] | <code>Boolean</code> | <code>false</code> | Do not scale down if the width *or* height are already greater than the target dimensions, equivalent to GraphicsMagick's `<` geometry option. This may still result in a crop to reach the target dimensions. | +| [options.fastShrinkOnLoad] | <code>Boolean</code> | <code>true</code> | Take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern or round-down of an auto-scaled dimension. | + +**Example** +```js +sharp(input) + .resize({ width: 100 }) + .toBuffer() + .then(data => { + // 100 pixels wide, auto-scaled height + }); +``` +**Example** +```js +sharp(input) + .resize({ height: 100 }) + .toBuffer() + .then(data => { + // 100 pixels high, auto-scaled width + }); +``` +**Example** +```js +sharp(input) + .resize(200, 300, { + kernel: sharp.kernel.nearest, + fit: 'contain', + position: 'right top', + background: { r: 255, g: 255, b: 255, alpha: 0.5 } + }) + .toFile('output.png') + .then(() => { + // output.png is a 200 pixels wide and 300 pixels high image + // containing a nearest-neighbour scaled version + // contained within the north-east corner of a semi-transparent white canvas + }); +``` +**Example** +```js +const transformer = sharp() + .resize({ + width: 200, + height: 200, + fit: sharp.fit.cover, + position: sharp.strategy.entropy + }); +// Read image data from readableStream +// Write 200px square auto-cropped image data to writableStream +readableStream + .pipe(transformer) + .pipe(writableStream); +``` +**Example** +```js +sharp(input) + .resize(200, 200, { + fit: sharp.fit.inside, + withoutEnlargement: true + }) + .toFormat('jpeg') + .toBuffer() + .then(function(outputBuffer) { + // outputBuffer contains JPEG image data + // no wider and no higher than 200 pixels + // and no larger than the input image + }); +``` +**Example** +```js +sharp(input) + .resize(200, 200, { + fit: sharp.fit.outside, + withoutReduction: true + }) + .toFormat('jpeg') + .toBuffer() + .then(function(outputBuffer) { + // outputBuffer contains JPEG image data + // of at least 200 pixels wide and 200 pixels high while maintaining aspect ratio + // and no smaller than the input image + }); +``` +**Example** +```js +const scaleByHalf = await sharp(input) + .metadata() + .then(({ width }) => sharp(input) + .resize(Math.round(width * 0.5)) + .toBuffer() + ); +``` + + +## extend +> extend(extend) ⇒ <code>Sharp</code> + +Extend / pad / extrude one or more edges of the image with either +the provided background colour or pixels derived from the image. +This operation will always occur after resizing and extraction, if any. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| extend | <code>number</code> \| <code>Object</code> | | single pixel count to add to all edges or an Object with per-edge counts | +| [extend.top] | <code>number</code> | <code>0</code> | | +| [extend.left] | <code>number</code> | <code>0</code> | | +| [extend.bottom] | <code>number</code> | <code>0</code> | | +| [extend.right] | <code>number</code> | <code>0</code> | | +| [extend.extendWith] | <code>String</code> | <code>'background'</code> | populate new pixels using this method, one of: background, copy, repeat, mirror. | +| [extend.background] | <code>String</code> \| <code>Object</code> | <code>{r: 0, g: 0, b: 0, alpha: 1}</code> | background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency. | + +**Example** +```js +// Resize to 140 pixels wide, then add 10 transparent pixels +// to the top, left and right edges and 20 to the bottom edge +sharp(input) + .resize(140) + .extend({ + top: 10, + bottom: 20, + left: 10, + right: 10, + background: { r: 0, g: 0, b: 0, alpha: 0 } + }) + ... +``` +**Example** +```js +// Add a row of 10 red pixels to the bottom +sharp(input) + .extend({ + bottom: 10, + background: 'red' + }) + ... +``` +**Example** +```js +// Extrude image by 8 pixels to the right, mirroring existing right hand edge +sharp(input) + .extend({ + right: 8, + background: 'mirror' + }) + ... +``` + + +## extract +> extract(options) ⇒ <code>Sharp</code> + +Extract/crop a region of the image. + +- Use `extract` before `resize` for pre-resize extraction. +- Use `extract` after `resize` for post-resize extraction. +- Use `extract` twice and `resize` once for extract-then-resize-then-extract in a fixed operation order. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Description | +| --- | --- | --- | +| options | <code>Object</code> | describes the region to extract using integral pixel values | +| options.left | <code>number</code> | zero-indexed offset from left edge | +| options.top | <code>number</code> | zero-indexed offset from top edge | +| options.width | <code>number</code> | width of region to extract | +| options.height | <code>number</code> | height of region to extract | + +**Example** +```js +sharp(input) + .extract({ left: left, top: top, width: width, height: height }) + .toFile(output, function(err) { + // Extract a region of the input image, saving in the same format. + }); +``` +**Example** +```js +sharp(input) + .extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre }) + .resize(width, height) + .extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost }) + .toFile(output, function(err) { + // Extract a region, resize, then extract from the resized image + }); +``` + + +## trim +> trim([options]) ⇒ <code>Sharp</code> + +Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel. + +Images with an alpha channel will use the combined bounding box of alpha and non-alpha channels. + +If the result of this operation would trim an image to nothing then no change is made. + +The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties. + + +**Throws**: + +- <code>Error</code> Invalid parameters + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> | | | +| [options.background] | <code>string</code> \| <code>Object</code> | <code>"'top-left pixel'"</code> | Background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to that of the top-left pixel. | +| [options.threshold] | <code>number</code> | <code>10</code> | Allowed difference from the above colour, a positive number. | +| [options.lineArt] | <code>boolean</code> | <code>false</code> | Does the input more closely resemble line art (e.g. vector) rather than being photographic? | + +**Example** +```js +// Trim pixels with a colour similar to that of the top-left pixel. +await sharp(input) + .trim() + .toFile(output); +``` +**Example** +```js +// Trim pixels with the exact same colour as that of the top-left pixel. +await sharp(input) + .trim({ + threshold: 0 + }) + .toFile(output); +``` +**Example** +```js +// Assume input is line art and trim only pixels with a similar colour to red. +const output = await sharp(input) + .trim({ + background: "#FF0000", + lineArt: true + }) + .toBuffer(); +``` +**Example** +```js +// Trim all "yellow-ish" pixels, being more lenient with the higher threshold. +const output = await sharp(input) + .trim({ + background: "yellow", + threshold: 42, + }) + .toBuffer(); +``` \ No newline at end of file diff --git a/docs/src/content/docs/api-utility.md b/docs/src/content/docs/api-utility.md new file mode 100644 index 000000000..73190fd01 --- /dev/null +++ b/docs/src/content/docs/api-utility.md @@ -0,0 +1,232 @@ +--- +# This file was auto-generated from JSDoc in lib/utility.js +title: Global properties +--- + +## versions +> versions + +An Object containing the version numbers of sharp, libvips +and (when using prebuilt binaries) its dependencies. + + +**Example** +```js +console.log(sharp.versions); +``` + + +## interpolators +> interpolators : <code>enum</code> + +An Object containing the available interpolators and their proper values + + +**Read only**: true +**Properties** + +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| nearest | <code>string</code> | <code>"nearest"</code> | [Nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation). Suitable for image enlargement only. | +| bilinear | <code>string</code> | <code>"bilinear"</code> | [Bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation). Faster than bicubic but with less smooth results. | +| bicubic | <code>string</code> | <code>"bicubic"</code> | [Bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation) (the default). | +| locallyBoundedBicubic | <code>string</code> | <code>"lbb"</code> | [LBB interpolation](https://github.com/libvips/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. | +| nohalo | <code>string</code> | <code>"nohalo"</code> | [Nohalo interpolation](http://eprints.soton.ac.uk/268086/). Prevents acutance but typically reduces performance by a factor of 3. | +| vertexSplitQuadraticBasisSpline | <code>string</code> | <code>"vsqbs"</code> | [VSQBS interpolation](https://github.com/libvips/libvips/blob/master/libvips/resample/vsqbs.cpp#L48). Prevents "staircasing" when enlarging. | + + + +## format +> format ⇒ <code>Object</code> + +An Object containing nested boolean values representing the available input and output formats/methods. + + +**Example** +```js +console.log(sharp.format); +``` + + +## queue +> queue + +An EventEmitter that emits a `change` event when a task is either: +- queued, waiting for _libuv_ to provide a worker thread +- complete + + +**Example** +```js +sharp.queue.on('change', function(queueLength) { + console.log('Queue contains ' + queueLength + ' task(s)'); +}); +``` + + +## cache +> cache([options]) ⇒ <code>Object</code> + +Gets or, when options are provided, sets the limits of _libvips'_ operation cache. +Existing entries in the cache will be trimmed after any change in limits. +This method always returns cache statistics, +useful for determining how much working memory is required for a particular task. + + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | <code>Object</code> \| <code>boolean</code> | <code>true</code> | Object with the following attributes, or boolean where true uses default cache settings and false removes all caching | +| [options.memory] | <code>number</code> | <code>50</code> | is the maximum memory in MB to use for this cache | +| [options.files] | <code>number</code> | <code>20</code> | is the maximum number of files to hold open | +| [options.items] | <code>number</code> | <code>100</code> | is the maximum number of operations to cache | + +**Example** +```js +const stats = sharp.cache(); +``` +**Example** +```js +sharp.cache( { items: 200 } ); +sharp.cache( { files: 0 } ); +sharp.cache(false); +``` + + +## concurrency +> concurrency([concurrency]) ⇒ <code>number</code> + +Gets or, when a concurrency is provided, sets +the maximum number of threads _libvips_ should use to process _each image_. +These are from a thread pool managed by glib, +which helps avoid the overhead of creating new threads. + +This method always returns the current concurrency. + +The default value is the number of CPU cores, +except when using glibc-based Linux without jemalloc, +where the default is `1` to help reduce memory fragmentation. + +A value of `0` will reset this to the number of CPU cores. + +Some image format libraries spawn additional threads, +e.g. libaom manages its own 4 threads when encoding AVIF images, +and these are independent of the value set here. + +:::note +Further [control over performance](/performance/) is available. +::: + + +**Returns**: <code>number</code> - concurrency + +| Param | Type | +| --- | --- | +| [concurrency] | <code>number</code> | + +**Example** +```js +const threads = sharp.concurrency(); // 4 +sharp.concurrency(2); // 2 +sharp.concurrency(0); // 4 +``` + + +## counters +> counters() ⇒ <code>Object</code> + +Provides access to internal task counters. +- queue is the number of tasks this module has queued waiting for _libuv_ to provide a worker thread from its pool. +- process is the number of resize tasks currently being processed. + + +**Example** +```js +const counters = sharp.counters(); // { queue: 2, process: 4 } +``` + + +## simd +> simd([simd]) ⇒ <code>boolean</code> + +Get and set use of SIMD vector unit instructions. +Requires libvips to have been compiled with highway support. + +Improves the performance of `resize`, `blur` and `sharpen` operations +by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM NEON. + + + +| Param | Type | Default | +| --- | --- | --- | +| [simd] | <code>boolean</code> | <code>true</code> | + +**Example** +```js +const simd = sharp.simd(); +// simd is `true` if the runtime use of highway is currently enabled +``` +**Example** +```js +const simd = sharp.simd(false); +// prevent libvips from using highway at runtime +``` + + +## block +> block(options) + +Block libvips operations at runtime. + +This is in addition to the `VIPS_BLOCK_UNTRUSTED` environment variable, +which when set will block all "untrusted" operations. + + +**Since**: 0.32.4 + +| Param | Type | Description | +| --- | --- | --- | +| options | <code>Object</code> | | +| options.operation | <code>Array.<string></code> | List of libvips low-level operation names to block. | + +**Example** *(Block all TIFF input.)* +```js +sharp.block({ + operation: ['VipsForeignLoadTiff'] +}); +``` + + +## unblock +> unblock(options) + +Unblock libvips operations at runtime. + +This is useful for defining a list of allowed operations. + + +**Since**: 0.32.4 + +| Param | Type | Description | +| --- | --- | --- | +| options | <code>Object</code> | | +| options.operation | <code>Array.<string></code> | List of libvips low-level operation names to unblock. | + +**Example** *(Block all input except WebP from the filesystem.)* +```js +sharp.block({ + operation: ['VipsForeignLoad'] +}); +sharp.unblock({ + operation: ['VipsForeignLoadWebpFile'] +}); +``` +**Example** *(Block all input except JPEG and PNG from a Buffer or Stream.)* +```js +sharp.block({ + operation: ['VipsForeignLoad'] +}); +sharp.unblock({ + operation: ['VipsForeignLoadJpegBuffer', 'VipsForeignLoadPngBuffer'] +}); +``` \ No newline at end of file diff --git a/docs/src/content/docs/changelog/_meta.yml b/docs/src/content/docs/changelog/_meta.yml new file mode 100644 index 000000000..5dd257241 --- /dev/null +++ b/docs/src/content/docs/changelog/_meta.yml @@ -0,0 +1 @@ +sort: reverse-slug diff --git a/docs/src/content/docs/changelog/v0.10.0.md b/docs/src/content/docs/changelog/v0.10.0.md new file mode 100644 index 000000000..aae2ca0ee --- /dev/null +++ b/docs/src/content/docs/changelog/v0.10.0.md @@ -0,0 +1,22 @@ +--- +title: v0.10.0 - 23rd April 2015 +slug: changelog/v0.10.0 +--- + +* Add support for Windows (x86). + [#19](https://github.com/lovell/sharp/issues/19) + [@DullReferenceException](https://github.com/DullReferenceException) + [@itsananderson](https://github.com/itsananderson) + +* Add support for Openslide input and DeepZoom output. + [#146](https://github.com/lovell/sharp/issues/146) + [@mvictoras](https://github.com/mvictoras) + +* Allow arbitrary aspect ratios when resizing images via new `ignoreAspectRatio` method. + [#192](https://github.com/lovell/sharp/issues/192) + [@skedastik](https://github.com/skedastik) + +* Enhance output image contrast by stretching its luminance to cover the full dynamic range via new `normalize` method. + [#194](https://github.com/lovell/sharp/issues/194) + [@bkw](https://github.com/bkw) + [@codingforce](https://github.com/codingforce) diff --git a/docs/src/content/docs/changelog/v0.10.1.md b/docs/src/content/docs/changelog/v0.10.1.md new file mode 100644 index 000000000..6f0336861 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.10.1.md @@ -0,0 +1,12 @@ +--- +title: v0.10.1 - 1st June 2015 +slug: changelog/v0.10.1 +--- + +* Allow embed of image with alpha transparency onto non-transparent background. + [#204](https://github.com/lovell/sharp/issues/204) + [@mikemliu](https://github.com/mikemliu) + +* Include C standard library for `atoi` as Xcode 6.3 appears to no longer do this. + [#228](https://github.com/lovell/sharp/issues/228) + [@doggan](https://github.com/doggan) diff --git a/docs/src/content/docs/changelog/v0.11.0.md b/docs/src/content/docs/changelog/v0.11.0.md new file mode 100644 index 000000000..2b07a93a5 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.11.0.md @@ -0,0 +1,30 @@ +--- +title: v0.11.0 - 15th July 2015 +slug: changelog/v0.11.0 +--- + +* Allow alpha transparency compositing via new `overlayWith` method. + [#97](https://github.com/lovell/sharp/issues/97) + [@gasi](https://github.com/gasi) + +* Expose raw ICC profile data as a Buffer when using `metadata`. + [#129](https://github.com/lovell/sharp/issues/129) + [@homerjam](https://github.com/homerjam) + +* Allow image header updates via a parameter passed to existing `withMetadata` method. + Provide initial support for EXIF `Orientation` tag, + which if present is now removed when using `rotate`, `flip` or `flop`. + [#189](https://github.com/lovell/sharp/issues/189) + [@h2non](https://github.com/h2non) + +* Tighten constructor parameter checks. + [#221](https://github.com/lovell/sharp/issues/221) + [@mikemorris](https://github.com/mikemorris) + +* Allow one input Stream to be shared with two or more output Streams via new `clone` method. + [#235](https://github.com/lovell/sharp/issues/235) + [@jaubourg](https://github.com/jaubourg) + +* Use `round` instead of `floor` when auto-scaling dimensions to avoid floating-point rounding errors. + [#238](https://github.com/lovell/sharp/issues/238) + [@richardadjogah](https://github.com/richardadjogah) diff --git a/docs/src/content/docs/changelog/v0.11.1.md b/docs/src/content/docs/changelog/v0.11.1.md new file mode 100644 index 000000000..44bdfc925 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.11.1.md @@ -0,0 +1,12 @@ +--- +title: v0.11.1 - 12th August 2015 +slug: changelog/v0.11.1 +--- + +* Silence MSVC warning: "C4530: C++ exception handler used, but unwind semantics are not enabled". + [#244](https://github.com/lovell/sharp/pull/244) + [@TheThing](https://github.com/TheThing) + +* Suppress gamma correction for input image with alpha transparency. + [#249](https://github.com/lovell/sharp/issues/249) + [@compeak](https://github.com/compeak) diff --git a/docs/src/content/docs/changelog/v0.11.2.md b/docs/src/content/docs/changelog/v0.11.2.md new file mode 100644 index 000000000..9990b05f7 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.11.2.md @@ -0,0 +1,10 @@ +--- +title: v0.11.2 - 28th August 2015 +slug: changelog/v0.11.2 +--- + +* Allow crop gravity to be provided as a String. + [#255](https://github.com/lovell/sharp/pull/255) + [@papandreou](https://github.com/papandreou) +* Add support for io.js v3 and Node v4. + [#246](https://github.com/lovell/sharp/issues/246) diff --git a/docs/src/content/docs/changelog/v0.11.3.md b/docs/src/content/docs/changelog/v0.11.3.md new file mode 100644 index 000000000..a062d1b8b --- /dev/null +++ b/docs/src/content/docs/changelog/v0.11.3.md @@ -0,0 +1,8 @@ +--- +title: v0.11.3 - 8th September 2015 +slug: changelog/v0.11.3 +--- + +* Intrepret blurSigma, sharpenFlat, and sharpenJagged as double precision. + [#263](https://github.com/lovell/sharp/pull/263) + [@chrisriley](https://github.com/chrisriley) diff --git a/docs/src/content/docs/changelog/v0.11.4.md b/docs/src/content/docs/changelog/v0.11.4.md new file mode 100644 index 000000000..f8cba2cd6 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.11.4.md @@ -0,0 +1,16 @@ +--- +title: v0.11.4 - 5th November 2015 +slug: changelog/v0.11.4 +--- + +* Add corners, e.g. `northeast`, to existing `gravity` option. + [#291](https://github.com/lovell/sharp/pull/291) + [@brandonaaron](https://github.com/brandonaaron) + +* Ensure correct auto-rotation for EXIF Orientation values 2 and 4. + [#288](https://github.com/lovell/sharp/pull/288) + [@brandonaaron](https://github.com/brandonaaron) + +* Make static linking possible via `--runtime_link` install option. + [#287](https://github.com/lovell/sharp/pull/287) + [@vlapo](https://github.com/vlapo) diff --git a/docs/src/content/docs/changelog/v0.12.0.md b/docs/src/content/docs/changelog/v0.12.0.md new file mode 100644 index 000000000..c081be9de --- /dev/null +++ b/docs/src/content/docs/changelog/v0.12.0.md @@ -0,0 +1,38 @@ +--- +title: v0.12.0 - 23rd November 2015 +slug: changelog/v0.12.0 +--- + +* Bundle pre-compiled libvips and its dependencies for 64-bit Linux and Windows. + [#42](https://github.com/lovell/sharp/issues/42) + +* Take advantage of libvips v8.1.0+ features. + [#152](https://github.com/lovell/sharp/issues/152) + +* Add support for 64-bit Windows. Drop support for 32-bit Windows. + [#224](https://github.com/lovell/sharp/issues/224) + [@sabrehagen](https://github.com/sabrehagen) + +* Switch default interpolator to bicubic. + [#289](https://github.com/lovell/sharp/issues/289) + [@mahnunchik](https://github.com/mahnunchik) + +* Pre-extract rotatation should not swap width/height. + [#296](https://github.com/lovell/sharp/issues/296) + [@asilvas](https://github.com/asilvas) + +* Ensure 16-bit+alpha input images are (un)premultiplied correctly. + [#301](https://github.com/lovell/sharp/issues/301) + [@izaakschroeder](https://github.com/izaakschroeder) + +* Add `threshold` operation. + [#303](https://github.com/lovell/sharp/pull/303) + [@dacarley](https://github.com/dacarley) + +* Add `negate` operation. + [#306](https://github.com/lovell/sharp/pull/306) + [@dacarley](https://github.com/dacarley) + +* Support `options` Object with existing `extract` operation. + [#309](https://github.com/lovell/sharp/pull/309) + [@papandreou](https://github.com/papandreou) diff --git a/docs/src/content/docs/changelog/v0.12.1.md b/docs/src/content/docs/changelog/v0.12.1.md new file mode 100644 index 000000000..61fb46a80 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.12.1.md @@ -0,0 +1,15 @@ +--- +title: v0.12.1 - 12th December 2015 +slug: changelog/v0.12.1 +--- + +* Allow use of SIMD vector instructions (via liborc) to be toggled on/off. + [#172](https://github.com/lovell/sharp/issues/172) + [@bkw](https://github.com/bkw) + [@puzrin](https://github.com/puzrin) + +* Ensure embedded ICC profiles output with perceptual intent. + [#321](https://github.com/lovell/sharp/issues/321) + [@vlapo](https://github.com/vlapo) + +* Use the NPM-configured HTTPS proxy, if any, for binary downloads. diff --git a/docs/src/content/docs/changelog/v0.12.2.md b/docs/src/content/docs/changelog/v0.12.2.md new file mode 100644 index 000000000..91dd927de --- /dev/null +++ b/docs/src/content/docs/changelog/v0.12.2.md @@ -0,0 +1,20 @@ +--- +title: v0.12.2 - 16th January 2016 +slug: changelog/v0.12.2 +--- + +* Upgrade libvips to v8.2.0 for improved vips_shrink. + +* Add pre-compiled libvips for ARMv6+ CPUs. + +* Ensure 16-bit input images work with embed option. + [#325](https://github.com/lovell/sharp/issues/325) + [@janaz](https://github.com/janaz) + +* Allow compilation with gmake to provide FreeBSD support. + [#326](https://github.com/lovell/sharp/issues/326) + [@c0decafe](https://github.com/c0decafe) + +* Attempt to remove temporary file after installation. + [#331](https://github.com/lovell/sharp/issues/331) + [@dtoubelis](https://github.com/dtoubelis) diff --git a/docs/src/content/docs/changelog/v0.13.0.md b/docs/src/content/docs/changelog/v0.13.0.md new file mode 100644 index 000000000..f1c989a6b --- /dev/null +++ b/docs/src/content/docs/changelog/v0.13.0.md @@ -0,0 +1,47 @@ +--- +title: v0.13.0 - 15th February 2016 +slug: changelog/v0.13.0 +--- + +* Improve vector image support by allowing control of density/DPI. + Switch pre-built libs from Imagemagick to Graphicsmagick. + [#110](https://github.com/lovell/sharp/issues/110) + [@bradisbell](https://github.com/bradisbell) + +* Add support for raw, uncompressed pixel Buffer/Stream input. + [#220](https://github.com/lovell/sharp/issues/220) + [@mikemorris](https://github.com/mikemorris) + +* Switch from libvips' C to C++ bindings, requires upgrade to v8.2.2. + [#299](https://github.com/lovell/sharp/issues/299) + +* Control number of open files in libvips' cache; breaks existing `cache` behaviour. + [#315](https://github.com/lovell/sharp/issues/315) + [@impomezia](https://github.com/impomezia) + +* Ensure 16-bit input images can be normalised and embedded onto transparent backgrounds. + [#339](https://github.com/lovell/sharp/issues/339) + [#340](https://github.com/lovell/sharp/issues/340) + [@janaz](https://github.com/janaz) + +* Ensure selected format takes precedence over any unknown output filename extension. + [#344](https://github.com/lovell/sharp/issues/344) + [@ubaltaci](https://github.com/ubaltaci) + +* Add support for libvips' PBM, PGM, PPM and FITS image format loaders. + [#347](https://github.com/lovell/sharp/issues/347) + [@oaleynik](https://github.com/oaleynik) + +* Ensure default crop gravity is center/centre. + [#351](https://github.com/lovell/sharp/pull/351) + [@joelmukuthu](https://github.com/joelmukuthu) + +* Improve support for musl libc systems e.g. Alpine Linux. + [#354](https://github.com/lovell/sharp/issues/354) + [#359](https://github.com/lovell/sharp/pull/359) + [@download13](https://github.com/download13) + [@wjordan](https://github.com/wjordan) + +* Small optimisation when reducing by an integral factor to favour shrink over affine. + +* Add support for gamma correction of images with an alpha channel. diff --git a/docs/src/content/docs/changelog/v0.13.1.md b/docs/src/content/docs/changelog/v0.13.1.md new file mode 100644 index 000000000..cbaf11ec5 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.13.1.md @@ -0,0 +1,8 @@ +--- +title: v0.13.1 - 27th February 2016 +slug: changelog/v0.13.1 +--- + +* Fix embedding onto transparent backgrounds; regression introduced in v0.13.0. + [#366](https://github.com/lovell/sharp/issues/366) + [@diegocsandrim](https://github.com/diegocsandrim) diff --git a/docs/src/content/docs/changelog/v0.14.0.md b/docs/src/content/docs/changelog/v0.14.0.md new file mode 100644 index 000000000..a23f2e7f0 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.14.0.md @@ -0,0 +1,39 @@ +--- +title: v0.14.0 - 2nd April 2016 +slug: changelog/v0.14.0 +--- + +* Add ability to extend (pad) the edges of an image. + [#128](https://github.com/lovell/sharp/issues/128) + [@blowsie](https://github.com/blowsie) + +* Add support for Zoomify and Google tile layouts. Breaks existing tile API. + [#223](https://github.com/lovell/sharp/issues/223) + [@bdunnette](https://github.com/bdunnette) + +* Improvements to overlayWith: differing sizes/formats, gravity, buffer input. + [#239](https://github.com/lovell/sharp/issues/239) + [@chrisriley](https://github.com/chrisriley) + +* Add entropy-based crop strategy to remove least interesting edges. + [#295](https://github.com/lovell/sharp/issues/295) + [@rightaway](https://github.com/rightaway) + +* Expose density metadata; set density of images from vector input. + [#338](https://github.com/lovell/sharp/issues/338) + [@lookfirst](https://github.com/lookfirst) + +* Emit post-processing 'info' event for Stream output. + [#367](https://github.com/lovell/sharp/issues/367) + [@salzhrani](https://github.com/salzhrani) + +* Ensure output image EXIF Orientation values are within 1-8 range. + [#385](https://github.com/lovell/sharp/pull/385) + [@jtobinisaniceguy](https://github.com/jtobinisaniceguy) + +* Ensure ratios are not swapped when rotating 90/270 and ignoring aspect. + [#387](https://github.com/lovell/sharp/issues/387) + [@kleisauke](https://github.com/kleisauke) + +* Remove deprecated style of calling extract API. Breaks calls using positional arguments. + [#276](https://github.com/lovell/sharp/issues/276) diff --git a/docs/src/content/docs/changelog/v0.14.1.md b/docs/src/content/docs/changelog/v0.14.1.md new file mode 100644 index 000000000..85e88c8b1 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.14.1.md @@ -0,0 +1,26 @@ +--- +title: v0.14.1 - 16th April 2016 +slug: changelog/v0.14.1 +--- + +* Allow removal of limitation on input pixel count via limitInputPixels. Use with care. + [#250](https://github.com/lovell/sharp/issues/250) + [#316](https://github.com/lovell/sharp/pull/316) + [@anandthakker](https://github.com/anandthakker) + [@kentongray](https://github.com/kentongray) + +* Use final output image for metadata passed to callback. + [#399](https://github.com/lovell/sharp/pull/399) + [@salzhrani](https://github.com/salzhrani) + +* Add support for writing tiled images to a zip container. + [#402](https://github.com/lovell/sharp/pull/402) + [@felixbuenemann](https://github.com/felixbuenemann) + +* Allow use of embed with 1 and 2 channel images. + [#411](https://github.com/lovell/sharp/issues/411) + [@janaz](https://github.com/janaz) + +* Improve Electron compatibility by allowing node-gyp rebuilds without npm. + [#412](https://github.com/lovell/sharp/issues/412) + [@nouh](https://github.com/nouh) diff --git a/docs/src/content/docs/changelog/v0.15.0.md b/docs/src/content/docs/changelog/v0.15.0.md new file mode 100644 index 000000000..ed7807f98 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.15.0.md @@ -0,0 +1,20 @@ +--- +title: v0.15.0 - 21st May 2016 +slug: changelog/v0.15.0 +--- + +* Use libvips' new Lanczos 3 kernel as default for image reduction. + Deprecate interpolateWith method, now provided as a resize option. + [#310](https://github.com/lovell/sharp/issues/310) + [@jcupitt](https://github.com/jcupitt) + +* Take advantage of libvips v8.3 features. + Add support for libvips' new GIF and SVG loaders. + Pre-built binaries now include giflib and librsvg, exclude *magick. + Use shrink-on-load for WebP input. + Break existing sharpen API to accept sigma and improve precision. + [#369](https://github.com/lovell/sharp/issues/369) + +* Remove unnecessary (un)premultiply operations when not resizing/compositing. + [#413](https://github.com/lovell/sharp/issues/413) + [@jardakotesovec](https://github.com/jardakotesovec) diff --git a/docs/src/content/docs/changelog/v0.15.1.md b/docs/src/content/docs/changelog/v0.15.1.md new file mode 100644 index 000000000..4cfdfba7d --- /dev/null +++ b/docs/src/content/docs/changelog/v0.15.1.md @@ -0,0 +1,68 @@ +--- +title: v0.15.1 - 12th July 2016 +slug: changelog/v0.15.1 +--- + +* Concat Stream-based input in single operation for ~+3% perf and less GC. + [#429](https://github.com/lovell/sharp/issues/429) + [@papandreou](https://github.com/papandreou) + +* Add alpha channel, if required, before extend operation. + [#439](https://github.com/lovell/sharp/pull/439) + [@frulo](https://github.com/frulo) + +* Allow overlay image to be repeated across entire image via tile option. + [#443](https://github.com/lovell/sharp/pull/443) + [@lemnisk8](https://github.com/lemnisk8) + +* Add cutout option to overlayWith feature, applies only the alpha channel of the overlay image. + [#448](https://github.com/lovell/sharp/pull/448) + [@kleisauke](https://github.com/kleisauke) + +* Ensure scaling factors are calculated independently to prevent rounding errors. + [#452](https://github.com/lovell/sharp/issues/452) + [@puzrin](https://github.com/puzrin) + +* Add --sharp-cxx11 flag to compile with gcc's new C++11 ABI. + [#456](https://github.com/lovell/sharp/pull/456) + [@kapouer](https://github.com/kapouer) + +* Add top/left offset support to overlayWith operation. + [#473](https://github.com/lovell/sharp/pull/473) + [@rnanwani](https://github.com/rnanwani) + +* Add convolve operation for kernel-based convolution. + [#479](https://github.com/lovell/sharp/pull/479) + [@mhirsch](https://github.com/mhirsch) + +* Add greyscale option to threshold operation for colourspace conversion control. + [#480](https://github.com/lovell/sharp/pull/480) + [@mhirsch](https://github.com/mhirsch) + +* Ensure ICC profiles are licenced for distribution. + [#486](https://github.com/lovell/sharp/issues/486) + [@kapouer](https://github.com/kapouer) + +* Allow images with an alpha channel to work with LAB-colourspace based sharpen. + [#490](https://github.com/lovell/sharp/issues/490) + [@jwagner](https://github.com/jwagner) + +* Add trim operation to remove "boring" edges. + [#492](https://github.com/lovell/sharp/pull/492) + [@kleisauke](https://github.com/kleisauke) + +* Add bandbool feature for channel-wise boolean operations. + [#496](https://github.com/lovell/sharp/pull/496) + [@mhirsch](https://github.com/mhirsch) + +* Add extractChannel operation to extract a channel from an image. + [#497](https://github.com/lovell/sharp/pull/497) + [@mhirsch](https://github.com/mhirsch) + +* Add ability to read and write native libvips .v files. + [#500](https://github.com/lovell/sharp/pull/500) + [@mhirsch](https://github.com/mhirsch) + +* Add boolean feature for bitwise image operations. + [#501](https://github.com/lovell/sharp/pull/501) + [@mhirsch](https://github.com/mhirsch) diff --git a/docs/src/content/docs/changelog/v0.16.0.md b/docs/src/content/docs/changelog/v0.16.0.md new file mode 100644 index 000000000..b6af330fd --- /dev/null +++ b/docs/src/content/docs/changelog/v0.16.0.md @@ -0,0 +1,42 @@ +--- +title: v0.16.0 - 18th August 2016 +slug: changelog/v0.16.0 +--- + +* Add pre-compiled libvips for OS X, ARMv7 and ARMv8. + [#312](https://github.com/lovell/sharp/issues/312) + +* Ensure boolean, bandbool, extractChannel ops occur before sRGB conversion. + [#504](https://github.com/lovell/sharp/pull/504) + [@mhirsch](https://github.com/mhirsch) + +* Recalculate factors after WebP shrink-on-load to avoid round-to-zero errors. + [#508](https://github.com/lovell/sharp/issues/508) + [@asilvas](https://github.com/asilvas) + +* Prevent boolean errors during extract operation. + [#511](https://github.com/lovell/sharp/pull/511) + [@mhirsch](https://github.com/mhirsch) + +* Add joinChannel and toColourspace/toColorspace operations. + [#513](https://github.com/lovell/sharp/pull/513) + [@mhirsch](https://github.com/mhirsch) + +* Add support for raw pixel data with boolean and withOverlay operations. + [#516](https://github.com/lovell/sharp/pull/516) + [@mhirsch](https://github.com/mhirsch) + +* Prevent bandbool creating a single channel sRGB image. + [#519](https://github.com/lovell/sharp/pull/519) + [@mhirsch](https://github.com/mhirsch) + +* Ensure ICC profiles are removed from PNG output unless withMetadata used. + [#521](https://github.com/lovell/sharp/issues/521) + [@ChrisPinewood](https://github.com/ChrisPinewood) + +* Add alpha channels, if missing, to overlayWith images. + [#540](https://github.com/lovell/sharp/pull/540) + [@cmtt](https://github.com/cmtt) + +* Remove deprecated interpolateWith method - use resize(w, h, { interpolator: ... }) + [#310](https://github.com/lovell/sharp/issues/310) diff --git a/docs/src/content/docs/changelog/v0.16.1.md b/docs/src/content/docs/changelog/v0.16.1.md new file mode 100644 index 000000000..c49d28981 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.16.1.md @@ -0,0 +1,21 @@ +--- +title: v0.16.1 - 13th October 2016 +slug: changelog/v0.16.1 +--- + +* C++11 ABI version is now auto-detected, remove sharp-cxx11 installation flag. + +* Add experimental 'attention' crop strategy. + [#295](https://github.com/lovell/sharp/issues/295) + +* Include .node extension for Meteor's require() implementation. + [#537](https://github.com/lovell/sharp/issues/537) + [@isjackwild](https://github.com/isjackwild) + +* Ensure convolution kernel scale is clamped to a minimum value of 1. + [#561](https://github.com/lovell/sharp/issues/561) + [@abagshaw](https://github.com/abagshaw) + +* Correct calculation of y-axis placement when overlaying image at a fixed point. + [#566](https://github.com/lovell/sharp/issues/566) + [@Nateowami](https://github.com/Nateowami) diff --git a/docs/src/content/docs/changelog/v0.16.2.md b/docs/src/content/docs/changelog/v0.16.2.md new file mode 100644 index 000000000..8c3976c64 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.16.2.md @@ -0,0 +1,8 @@ +--- +title: v0.16.2 - 22nd October 2016 +slug: changelog/v0.16.2 +--- + +* Restrict readelf usage to Linux only when detecting global libvips version. + [#602](https://github.com/lovell/sharp/issues/602) + [@caoko](https://github.com/caoko) diff --git a/docs/src/content/docs/changelog/v0.17.0.md b/docs/src/content/docs/changelog/v0.17.0.md new file mode 100644 index 000000000..4ec1f1856 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.17.0.md @@ -0,0 +1,39 @@ +--- +title: v0.17.0 - 11th December 2016 +slug: changelog/v0.17.0 +--- + +* Drop support for versions of Node prior to v4. + +* Deprecate the following output format "option" functions: + quality, progressive, compressionLevel, withoutAdaptiveFiltering, + withoutChromaSubsampling, trellisQuantisation, trellisQuantization, + overshootDeringing, optimiseScans and optimizeScans. + Access to these is now via output format functions, for example `quality(n)` + is now `jpeg({quality: n})` and/or `webp({quality: n})`. + +* Autoconvert GIF and SVG input to PNG output if no other format is specified. + +* Expose libvips' "centre" resize option to mimic \*magick's +0.5px convention. + [#568](https://github.com/lovell/sharp/issues/568) + +* Ensure support for embedded base64 PNG and JPEG images within an SVG. + [#601](https://github.com/lovell/sharp/issues/601) + [@dynamite-ready](https://github.com/dynamite-ready) + +* Ensure premultiply operation occurs before box filter shrink. + [#605](https://github.com/lovell/sharp/issues/605) + [@CmdrShepardsPie](https://github.com/CmdrShepardsPie) + [@teroparvinen](https://github.com/teroparvinen) + +* Add support for PNG and WebP tile-based output formats (in addition to JPEG). + [#622](https://github.com/lovell/sharp/pull/622) + [@ppaskaris](https://github.com/ppaskaris) + +* Allow use of extend with greyscale input. + [#623](https://github.com/lovell/sharp/pull/623) + [@ppaskaris](https://github.com/ppaskaris) + +* Allow non-RGB input to embed/extend onto background with an alpha channel. + [#646](https://github.com/lovell/sharp/issues/646) + [@DaGaMs](https://github.com/DaGaMs) diff --git a/docs/src/content/docs/changelog/v0.17.1.md b/docs/src/content/docs/changelog/v0.17.1.md new file mode 100644 index 000000000..28cf12167 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.17.1.md @@ -0,0 +1,15 @@ +--- +title: v0.17.1 - 15th January 2017 +slug: changelog/v0.17.1 +--- + +* Improve error messages for invalid parameters. + [@spikeon](https://github.com/spikeon) + [#644](https://github.com/lovell/sharp/pull/644) + +* Simplify expression for finding vips-cpp libdir. + [#656](https://github.com/lovell/sharp/pull/656) + +* Allow HTTPS-over-HTTP proxy when downloading pre-compiled dependencies. + [@wangzhiwei1888](https://github.com/wangzhiwei1888) + [#679](https://github.com/lovell/sharp/issues/679) diff --git a/docs/src/content/docs/changelog/v0.17.2.md b/docs/src/content/docs/changelog/v0.17.2.md new file mode 100644 index 000000000..02859307e --- /dev/null +++ b/docs/src/content/docs/changelog/v0.17.2.md @@ -0,0 +1,12 @@ +--- +title: v0.17.2 - 11th February 2017 +slug: changelog/v0.17.2 +--- + +* Ensure Readable side of Stream can start flowing after Writable side has finished. + [#671](https://github.com/lovell/sharp/issues/671) + [@danhaller](https://github.com/danhaller) + +* Expose WebP alpha quality, lossless and near-lossless output options. + [#685](https://github.com/lovell/sharp/pull/685) + [@rnanwani](https://github.com/rnanwani) diff --git a/docs/src/content/docs/changelog/v0.17.3.md b/docs/src/content/docs/changelog/v0.17.3.md new file mode 100644 index 000000000..0bc48d942 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.17.3.md @@ -0,0 +1,20 @@ +--- +title: v0.17.3 - 1st April 2017 +slug: changelog/v0.17.3 +--- + +* Allow toBuffer to optionally resolve a Promise with both info and data. + [#143](https://github.com/lovell/sharp/issues/143) + [@salzhrani](https://github.com/salzhrani) + +* Create blank image of given width, height, channels and background. + [#470](https://github.com/lovell/sharp/issues/470) + [@pjarts](https://github.com/pjarts) + +* Add support for the "nearest" kernel for image reductions. + [#732](https://github.com/lovell/sharp/pull/732) + [@alice0meta](https://github.com/alice0meta) + +* Add support for TIFF compression and predictor options. + [#738](https://github.com/lovell/sharp/pull/738) + [@kristojorg](https://github.com/kristojorg) diff --git a/docs/src/content/docs/changelog/v0.18.0.md b/docs/src/content/docs/changelog/v0.18.0.md new file mode 100644 index 000000000..d8afbdd04 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.18.0.md @@ -0,0 +1,60 @@ +--- +title: v0.18.0 - 30th May 2017 +slug: changelog/v0.18.0 +--- + +* Remove the previously-deprecated output format "option" functions: + quality, progressive, compressionLevel, withoutAdaptiveFiltering, + withoutChromaSubsampling, trellisQuantisation, trellisQuantization, + overshootDeringing, optimiseScans and optimizeScans. + +* Ensure maximum output dimensions are based on the format to be used. + [#176](https://github.com/lovell/sharp/issues/176) + [@stephanebachelier](https://github.com/stephanebachelier) + +* Avoid costly (un)premultiply when using overlayWith without alpha channel. + [#573](https://github.com/lovell/sharp/issues/573) + [@strarsis](https://github.com/strarsis) + +* Include pixel depth (e.g. "uchar") when reading metadata. + [#577](https://github.com/lovell/sharp/issues/577) + [@moedusa](https://github.com/moedusa) + +* Add support for Buffer and Stream-based TIFF output. + [#587](https://github.com/lovell/sharp/issues/587) + [@strarsis](https://github.com/strarsis) + +* Expose warnings from libvips via NODE_DEBUG=sharp environment variable. + [#607](https://github.com/lovell/sharp/issues/607) + [@puzrin](https://github.com/puzrin) + +* Switch to the libvips implementation of "attention" and "entropy" crop strategies. + [#727](https://github.com/lovell/sharp/issues/727) + +* Improve performance and accuracy of nearest neighbour integral upsampling. + [#752](https://github.com/lovell/sharp/issues/752) + [@MrIbby](https://github.com/MrIbby) + +* Constructor single argument API: allow plain object, reject null/undefined. + [#768](https://github.com/lovell/sharp/issues/768) + [@kub1x](https://github.com/kub1x) + +* Ensure ARM64 pre-built binaries use correct C++11 ABI version. + [#772](https://github.com/lovell/sharp/issues/772) + [@ajiratech2](https://github.com/ajiratech2) + +* Prevent aliasing by using dynamic values for shrink(-on-load). + [#781](https://github.com/lovell/sharp/issues/781) + [@kleisauke](https://github.com/kleisauke) + +* Expose libvips' "squash" parameter to enable 1-bit TIFF output. + [#783](https://github.com/lovell/sharp/pull/783) + [@YvesBos](https://github.com/YvesBos) + +* Add support for rotation using any multiple of +/-90 degrees. + [#791](https://github.com/lovell/sharp/pull/791) + [@ncoden](https://github.com/ncoden) + +* Add "jpg" alias to toFormat as shortened form of "jpeg". + [#814](https://github.com/lovell/sharp/pull/814) + [@jingsam](https://github.com/jingsam) diff --git a/docs/src/content/docs/changelog/v0.18.1.md b/docs/src/content/docs/changelog/v0.18.1.md new file mode 100644 index 000000000..3121a9cf3 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.18.1.md @@ -0,0 +1,8 @@ +--- +title: v0.18.1 - 30th May 2017 +slug: changelog/v0.18.1 +--- + +* Remove regression from #781 that could cause incorrect shrink calculation. + [#831](https://github.com/lovell/sharp/issues/831) + [@suprMax](https://github.com/suprMax) diff --git a/docs/src/content/docs/changelog/v0.18.2.md b/docs/src/content/docs/changelog/v0.18.2.md new file mode 100644 index 000000000..2eabf8374 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.18.2.md @@ -0,0 +1,19 @@ +--- +title: v0.18.2 - 1st July 2017 +slug: changelog/v0.18.2 +--- + +* Expose libvips' xres and yres properties for TIFF output. + [#828](https://github.com/lovell/sharp/pull/828) + [@YvesBos](https://github.com/YvesBos) + +* Ensure flip and flop operations work with auto-rotate. + [#837](https://github.com/lovell/sharp/issues/837) + [@rexxars](https://github.com/rexxars) + +* Allow binary download URL override via SHARP_DIST_BASE_URL env variable. + [#841](https://github.com/lovell/sharp/issues/841) + +* Add support for Solus Linux. + [#857](https://github.com/lovell/sharp/pull/857) + [@ekremkaraca](https://github.com/ekremkaraca) diff --git a/docs/src/content/docs/changelog/v0.18.3.md b/docs/src/content/docs/changelog/v0.18.3.md new file mode 100644 index 000000000..ea66e8112 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.18.3.md @@ -0,0 +1,12 @@ +--- +title: v0.18.3 - 13th September 2017 +slug: changelog/v0.18.3 +--- + +* Skip shrink-on-load when trimming. + [#888](https://github.com/lovell/sharp/pull/888) + [@kleisauke](https://github.com/kleisauke) + +* Migrate from got to simple-get for basic auth support. + [#945](https://github.com/lovell/sharp/pull/945) + [@pbomb](https://github.com/pbomb) diff --git a/docs/src/content/docs/changelog/v0.18.4.md b/docs/src/content/docs/changelog/v0.18.4.md new file mode 100644 index 000000000..002e06c26 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.18.4.md @@ -0,0 +1,8 @@ +--- +title: v0.18.4 - 18th September 2017 +slug: changelog/v0.18.4 +--- + +* Ensure input Buffer really is marked as Persistent, prevents mark-sweep GC. + [#950](https://github.com/lovell/sharp/issues/950) + [@lfdoherty](https://github.com/lfdoherty) diff --git a/docs/src/content/docs/changelog/v0.19.0.md b/docs/src/content/docs/changelog/v0.19.0.md new file mode 100644 index 000000000..d9ecfefcb --- /dev/null +++ b/docs/src/content/docs/changelog/v0.19.0.md @@ -0,0 +1,46 @@ +--- +title: v0.19.0 - 11th January 2018 +slug: changelog/v0.19.0 +--- + +* Expose offset coordinates of strategy-based crop. + [#868](https://github.com/lovell/sharp/issues/868) + [@mirohristov-com](https://github.com/mirohristov-com) + +* PNG output now defaults to adaptiveFiltering=false, compressionLevel=9 + [#872](https://github.com/lovell/sharp/issues/872) + [@wmertens](https://github.com/wmertens) + +* Add stats feature for pixel-derived image statistics. + [#915](https://github.com/lovell/sharp/pull/915) + [@rnanwani](https://github.com/rnanwani) + +* Add failOnError option to fail-fast on bad input image data. + [#976](https://github.com/lovell/sharp/pull/976) + [@mceachen](https://github.com/mceachen) + +* Resize: switch to libvips' implementation, make fastShrinkOnLoad optional, remove interpolator and centreSampling options. + [#977](https://github.com/lovell/sharp/pull/977) + [@jardakotesovec](https://github.com/jardakotesovec) + +* Attach finish event listener to a clone only for Stream-based input. + [#995](https://github.com/lovell/sharp/issues/995) + [@whmountains](https://github.com/whmountains) + +* Add tilecache before smartcrop to avoid over-computation of previous operations. + [#1028](https://github.com/lovell/sharp/issues/1028) + [@coffeebite](https://github.com/coffeebite) + +* Prevent toFile extension taking precedence over requested format. + [#1037](https://github.com/lovell/sharp/issues/1037) + [@tomgallagher](https://github.com/tomgallagher) + +* Add support for gravity option to existing embed feature. + [#1038](https://github.com/lovell/sharp/pull/1038) + [@AzureByte](https://github.com/AzureByte) + +* Expose IPTC and XMP metadata when available. + [#1079](https://github.com/lovell/sharp/pull/1079) + [@oaleynik](https://github.com/oaleynik) + +* TIFF output: switch default predictor from 'none' to 'horizontal' to match libvips' behaviour. diff --git a/docs/src/content/docs/changelog/v0.19.1.md b/docs/src/content/docs/changelog/v0.19.1.md new file mode 100644 index 000000000..deeea20d1 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.19.1.md @@ -0,0 +1,16 @@ +--- +title: v0.19.1 - 24th February 2018 +slug: changelog/v0.19.1 +--- + +* Expose libvips' linear transform feature. + [#1024](https://github.com/lovell/sharp/pull/1024) + [@3epnm](https://github.com/3epnm) + +* Expose angle option for tile-based output. + [#1121](https://github.com/lovell/sharp/pull/1121) + [@BiancoA](https://github.com/BiancoA) + +* Prevent crop operation when image already at or below target dimensions. + [#1134](https://github.com/lovell/sharp/issues/1134) + [@pieh](https://github.com/pieh) diff --git a/docs/src/content/docs/changelog/v0.20.0.md b/docs/src/content/docs/changelog/v0.20.0.md new file mode 100644 index 000000000..1f0bf56a2 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.0.md @@ -0,0 +1,7 @@ +--- +title: v0.20.0 - 5th March 2018 +slug: changelog/v0.20.0 +--- + +* Add support for prebuilt sharp binaries on common platforms. + [#186](https://github.com/lovell/sharp/issues/186) diff --git a/docs/src/content/docs/changelog/v0.20.1.md b/docs/src/content/docs/changelog/v0.20.1.md new file mode 100644 index 000000000..f4af4314c --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.1.md @@ -0,0 +1,15 @@ +--- +title: v0.20.1 - 17th March 2018 +slug: changelog/v0.20.1 +--- + +* Improve installation experience when a globally-installed libvips below the minimum required version is found. + [#1148](https://github.com/lovell/sharp/issues/1148) + +* Prevent smartcrop error when cumulative rounding is below target size. + [#1154](https://github.com/lovell/sharp/issues/1154) + [@ralrom](https://github.com/ralrom) + +* Expose libvips' median filter operation. + [#1161](https://github.com/lovell/sharp/pull/1161) + [@BiancoA](https://github.com/BiancoA) diff --git a/docs/src/content/docs/changelog/v0.20.2.md b/docs/src/content/docs/changelog/v0.20.2.md new file mode 100644 index 000000000..202d5c339 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.2.md @@ -0,0 +1,20 @@ +--- +title: v0.20.2 - 28th April 2018 +slug: changelog/v0.20.2 +--- + +* Add tint operation to set image chroma. + [#825](https://github.com/lovell/sharp/pull/825) + [@rikh42](https://github.com/rikh42) + +* Add environment variable to ignore globally-installed libvips. + [#1165](https://github.com/lovell/sharp/pull/1165) + [@oncletom](https://github.com/oncletom) + +* Add support for page selection with multi-page input (GIF/TIFF). + [#1204](https://github.com/lovell/sharp/pull/1204) + [@woolite64](https://github.com/woolite64) + +* Add support for Group4 (CCITTFAX4) compression with TIFF output. + [#1208](https://github.com/lovell/sharp/pull/1208) + [@woolite64](https://github.com/woolite64) diff --git a/docs/src/content/docs/changelog/v0.20.3.md b/docs/src/content/docs/changelog/v0.20.3.md new file mode 100644 index 000000000..b00c64910 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.3.md @@ -0,0 +1,8 @@ +--- +title: v0.20.3 - 29th May 2018 +slug: changelog/v0.20.3 +--- + +* Fix tint operation by ensuring LAB interpretation and allowing negative values. + [#1235](https://github.com/lovell/sharp/issues/1235) + [@wezside](https://github.com/wezside) diff --git a/docs/src/content/docs/changelog/v0.20.4.md b/docs/src/content/docs/changelog/v0.20.4.md new file mode 100644 index 000000000..a9580dea3 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.4.md @@ -0,0 +1,12 @@ +--- +title: v0.20.4 - 20th June 2018 +slug: changelog/v0.20.4 +--- + +* Prevent possible rounding error when using shrink-on-load and 90/270 degree rotation. + [#1241](https://github.com/lovell/sharp/issues/1241) + [@anahit42](https://github.com/anahit42) + +* Ensure extractChannel sets correct single-channel colour space interpretation. + [#1257](https://github.com/lovell/sharp/issues/1257) + [@jeremychone](https://github.com/jeremychone) diff --git a/docs/src/content/docs/changelog/v0.20.5.md b/docs/src/content/docs/changelog/v0.20.5.md new file mode 100644 index 000000000..07f166149 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.5.md @@ -0,0 +1,8 @@ +--- +title: v0.20.5 - 27th June 2018 +slug: changelog/v0.20.5 +--- + +* Expose libjpeg optimize_coding flag. + [#1265](https://github.com/lovell/sharp/pull/1265) + [@tomlokhorst](https://github.com/tomlokhorst) diff --git a/docs/src/content/docs/changelog/v0.20.6.md b/docs/src/content/docs/changelog/v0.20.6.md new file mode 100644 index 000000000..284a67a27 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.6.md @@ -0,0 +1,33 @@ +--- +title: v0.20.6 - 20th August 2018 +slug: changelog/v0.20.6 +--- + +* Add removeAlpha operation to remove alpha channel, if any. + [#1248](https://github.com/lovell/sharp/issues/1248) + +* Expose mozjpeg quant_table flag. + [#1285](https://github.com/lovell/sharp/pull/1285) + [@rexxars](https://github.com/rexxars) + +* Allow full WebP alphaQuality range of 0-100. + [#1290](https://github.com/lovell/sharp/pull/1290) + [@sylvaindumont](https://github.com/sylvaindumont) + +* Cache libvips binaries to reduce re-install time. + [#1301](https://github.com/lovell/sharp/issues/1301) + +* Ensure vendor platform mismatch throws error at install time. + [#1303](https://github.com/lovell/sharp/issues/1303) + +* Improve install time error messages for FreeBSD users. + [#1310](https://github.com/lovell/sharp/issues/1310) + +* Ensure extractChannel works with 16-bit images. + [#1330](https://github.com/lovell/sharp/issues/1330) + +* Expose depth option for tile-based output. + [#1342](https://github.com/lovell/sharp/pull/1342) + [@alundavies](https://github.com/alundavies) + +* Add experimental entropy field to stats response. diff --git a/docs/src/content/docs/changelog/v0.20.7.md b/docs/src/content/docs/changelog/v0.20.7.md new file mode 100644 index 000000000..e9503a29d --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.7.md @@ -0,0 +1,7 @@ +--- +title: v0.20.7 - 21st August 2018 +slug: changelog/v0.20.7 +--- + +* Use copy+unlink if rename operation fails during installation. + [#1345](https://github.com/lovell/sharp/issues/1345) diff --git a/docs/src/content/docs/changelog/v0.20.8.md b/docs/src/content/docs/changelog/v0.20.8.md new file mode 100644 index 000000000..8ab9a062a --- /dev/null +++ b/docs/src/content/docs/changelog/v0.20.8.md @@ -0,0 +1,12 @@ +--- +title: v0.20.8 - 5th September 2018 +slug: changelog/v0.20.8 +--- + +* Avoid race conditions when creating directories during installation. + [#1358](https://github.com/lovell/sharp/pull/1358) + [@ajhool](https://github.com/ajhool) + +* Accept floating point values for input density parameter. + [#1362](https://github.com/lovell/sharp/pull/1362) + [@aeirola](https://github.com/aeirola) diff --git a/docs/src/content/docs/changelog/v0.21.0.md b/docs/src/content/docs/changelog/v0.21.0.md new file mode 100644 index 000000000..762cc65a4 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.21.0.md @@ -0,0 +1,39 @@ +--- +title: v0.21.0 - 4th October 2018 +slug: changelog/v0.21.0 +--- + +* Deprecate the following resize-related functions: + `crop`, `embed`, `ignoreAspectRatio`, `max`, `min` and `withoutEnlargement`. + Access to these is now via options passed to the `resize` function. + For example: + `embed('north')` is now `resize(width, height, { fit: 'contain', position: 'north' })`, + `crop('attention')` is now `resize(width, height, { fit: 'cover', position: 'attention' })`, + `max().withoutEnlargement()` is now `resize(width, height, { fit: 'inside', withoutEnlargement: true })`. + [#1135](https://github.com/lovell/sharp/issues/1135) + +* Deprecate the `background` function. + Per-operation `background` options added to `resize`, `extend` and `flatten` operations. + [#1392](https://github.com/lovell/sharp/issues/1392) + +* Add `size` to `metadata` response (Stream and Buffer input only). + [#695](https://github.com/lovell/sharp/issues/695) + +* Switch from custom trim operation to `vips_find_trim`. + [#914](https://github.com/lovell/sharp/issues/914) + +* Add `chromaSubsampling` and `isProgressive` properties to `metadata` response. + [#1186](https://github.com/lovell/sharp/issues/1186) + +* Drop Node 4 support. + [#1212](https://github.com/lovell/sharp/issues/1212) + +* Enable SIMD convolution by default. + [#1213](https://github.com/lovell/sharp/issues/1213) + +* Add experimental prebuilt binaries for musl-based Linux. + [#1379](https://github.com/lovell/sharp/issues/1379) + +* Add support for arbitrary rotation angle via vips_rotate. + [#1385](https://github.com/lovell/sharp/pull/1385) + [@freezy](https://github.com/freezy) diff --git a/docs/src/content/docs/changelog/v0.21.1.md b/docs/src/content/docs/changelog/v0.21.1.md new file mode 100644 index 000000000..bb685ef2b --- /dev/null +++ b/docs/src/content/docs/changelog/v0.21.1.md @@ -0,0 +1,31 @@ +--- +title: v0.21.1 - 7th December 2018 +slug: changelog/v0.21.1 +--- + +* Install: support `sharp_dist_base_url` npm config, like existing `SHARP_DIST_BASE_URL`. + [#1422](https://github.com/lovell/sharp/pull/1422) + [@SethWen](https://github.com/SethWen) + +* Ensure `channel` metadata is correct for raw, greyscale output. + [#1425](https://github.com/lovell/sharp/issues/1425) + +* Add support for the "mitchell" kernel for image reductions. + [#1438](https://github.com/lovell/sharp/pull/1438) + [@Daiz](https://github.com/Daiz) + +* Allow separate parameters for gamma encoding and decoding. + [#1439](https://github.com/lovell/sharp/pull/1439) + [@Daiz](https://github.com/Daiz) + +* Build prototype with `Object.assign` to allow minification. + [#1475](https://github.com/lovell/sharp/pull/1475) + [@jaubourg](https://github.com/jaubourg) + +* Expose libvips' recombination matrix operation. + [#1477](https://github.com/lovell/sharp/pull/1477) + [@fromkeith](https://github.com/fromkeith) + +* Expose libvips' pyramid/tile options for TIFF output. + [#1483](https://github.com/lovell/sharp/pull/1483) + [@mbklein](https://github.com/mbklein) diff --git a/docs/src/content/docs/changelog/v0.21.2.md b/docs/src/content/docs/changelog/v0.21.2.md new file mode 100644 index 000000000..be7f6758d --- /dev/null +++ b/docs/src/content/docs/changelog/v0.21.2.md @@ -0,0 +1,27 @@ +--- +title: v0.21.2 - 13th January 2019 +slug: changelog/v0.21.2 +--- + +* Ensure all metadata is removed from PNG output unless `withMetadata` used. + +* Ensure shortest edge is at least one pixel after resizing. + [#1003](https://github.com/lovell/sharp/issues/1003) + +* Add `ensureAlpha` operation to add an alpha channel, if missing. + [#1153](https://github.com/lovell/sharp/issues/1153) + +* Expose `pages` and `pageHeight` metadata for multi-page input images. + [#1205](https://github.com/lovell/sharp/issues/1205) + +* Expose PNG output options requiring libimagequant. + [#1484](https://github.com/lovell/sharp/issues/1484) + +* Expose underlying error message for invalid input. + [#1505](https://github.com/lovell/sharp/issues/1505) + +* Prevent mutatation of options passed to `jpeg`. + [#1516](https://github.com/lovell/sharp/issues/1516) + +* Ensure forced output format applied correctly when output chaining. + [#1528](https://github.com/lovell/sharp/issues/1528) diff --git a/docs/src/content/docs/changelog/v0.21.3.md b/docs/src/content/docs/changelog/v0.21.3.md new file mode 100644 index 000000000..1cde28c63 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.21.3.md @@ -0,0 +1,9 @@ +--- +title: v0.21.3 - 19th January 2019 +slug: changelog/v0.21.3 +--- + +* Input image decoding now fails fast, set `failOnError` to change this behaviour. + +* Failed filesystem-based input now separates missing file and invalid format errors. + [#1542](https://github.com/lovell/sharp/issues/1542) diff --git a/docs/src/content/docs/changelog/v0.22.0.md b/docs/src/content/docs/changelog/v0.22.0.md new file mode 100644 index 000000000..797c743bc --- /dev/null +++ b/docs/src/content/docs/changelog/v0.22.0.md @@ -0,0 +1,20 @@ +--- +title: v0.22.0 - 18th March 2019 +slug: changelog/v0.22.0 +--- + +* Remove functions previously deprecated in v0.21.0: + `background`, `crop`, `embed`, `ignoreAspectRatio`, `max`, `min` and `withoutEnlargement`. + +* Add `composite` operation supporting multiple images and blend modes; deprecate `overlayWith`. + [#728](https://github.com/lovell/sharp/issues/728) + +* Add support for `pages` input option for multi-page input. + [#1566](https://github.com/lovell/sharp/issues/1566) + +* Allow Stream-based input of raw pixel data. + [#1579](https://github.com/lovell/sharp/issues/1579) + +* Add support for `page` input option to GIF and PDF. + [#1595](https://github.com/lovell/sharp/pull/1595) + [@ramiel](https://github.com/ramiel) diff --git a/docs/src/content/docs/changelog/v0.22.1.md b/docs/src/content/docs/changelog/v0.22.1.md new file mode 100644 index 000000000..cf8e3d464 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.22.1.md @@ -0,0 +1,15 @@ +--- +title: v0.22.1 - 25th April 2019 +slug: changelog/v0.22.1 +--- + +* Add `modulate` operation for brightness, saturation and hue. + [#1601](https://github.com/lovell/sharp/pull/1601) + [@Goues](https://github.com/Goues) + +* Improve help messaging should `require("sharp")` fail. + [#1638](https://github.com/lovell/sharp/pull/1638) + [@sidharthachatterjee](https://github.com/sidharthachatterjee) + +* Add support for Node 12. + [#1668](https://github.com/lovell/sharp/issues/1668) diff --git a/docs/src/content/docs/changelog/v0.23.0.md b/docs/src/content/docs/changelog/v0.23.0.md new file mode 100644 index 000000000..24b18827c --- /dev/null +++ b/docs/src/content/docs/changelog/v0.23.0.md @@ -0,0 +1,32 @@ +--- +title: v0.23.0 - 29th July 2019 +slug: changelog/v0.23.0 +--- + +* Remove `overlayWith` previously deprecated in v0.22.0. + +* Add experimental support for HEIF images. Requires libvips compiled with libheif. + [#1105](https://github.com/lovell/sharp/issues/1105) + +* Expose libwebp `smartSubsample` and `reductionEffort` options. + [#1545](https://github.com/lovell/sharp/issues/1545) + +* Add experimental support for Worker Threads. + [#1558](https://github.com/lovell/sharp/issues/1558) + +* Use libvips' built-in CMYK and sRGB profiles when required. + [#1619](https://github.com/lovell/sharp/issues/1619) + +* Drop support for Node.js versions 6 and 11. + [#1674](https://github.com/lovell/sharp/issues/1674) + +* Expose `skipBlanks` option for tile-based output. + [#1687](https://github.com/lovell/sharp/pull/1687) + [@RaboliotTheGrey](https://github.com/RaboliotTheGrey) + +* Allow use of `failOnError` option with Stream-based input. + [#1691](https://github.com/lovell/sharp/issues/1691) + +* Fix rotate/extract ordering for non-90 angles. + [#1755](https://github.com/lovell/sharp/pull/1755) + [@iovdin](https://github.com/iovdin) diff --git a/docs/src/content/docs/changelog/v0.23.1.md b/docs/src/content/docs/changelog/v0.23.1.md new file mode 100644 index 000000000..c180d9952 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.23.1.md @@ -0,0 +1,24 @@ +--- +title: v0.23.1 - 26th September 2019 +slug: changelog/v0.23.1 +--- + +* Ensure `sharp.format.vips` is present and correct (filesystem only). + [#1813](https://github.com/lovell/sharp/issues/1813) + +* Ensure invalid `width` and `height` provided as options to `resize` throw. + [#1817](https://github.com/lovell/sharp/issues/1817) + +* Allow use of 'heic' and 'heif' identifiers with `toFormat`. + [#1834](https://github.com/lovell/sharp/pull/1834) + [@jaubourg](https://github.com/jaubourg) + +* Add `premultiplied` option to `composite` operation. + [#1835](https://github.com/lovell/sharp/pull/1835) + [@Andargor](https://github.com/Andargor) + +* Allow instance reuse with differing `toBuffer` options. + [#1860](https://github.com/lovell/sharp/pull/1860) + [@RaboliotTheGrey](https://github.com/RaboliotTheGrey) + +* Ensure image is at least 3x3 pixels before attempting trim operation. diff --git a/docs/src/content/docs/changelog/v0.23.2.md b/docs/src/content/docs/changelog/v0.23.2.md new file mode 100644 index 000000000..1eb59491d --- /dev/null +++ b/docs/src/content/docs/changelog/v0.23.2.md @@ -0,0 +1,12 @@ +--- +title: v0.23.2 - 28th October 2019 +slug: changelog/v0.23.2 +--- + +* Add `background` option to tile output operation. + [#1924](https://github.com/lovell/sharp/pull/1924) + [@neave](https://github.com/neave) + +* Add support for Node.js 13. + [#1932](https://github.com/lovell/sharp/pull/1932) + [@MayhemYDG](https://github.com/MayhemYDG) diff --git a/docs/src/content/docs/changelog/v0.23.3.md b/docs/src/content/docs/changelog/v0.23.3.md new file mode 100644 index 000000000..4c19795a5 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.23.3.md @@ -0,0 +1,18 @@ +--- +title: v0.23.3 - 17th November 2019 +slug: changelog/v0.23.3 +--- + +* Ensure `trim` operation supports images contained in the alpha channel. + [#1597](https://github.com/lovell/sharp/issues/1597) + +* Ensure tile `overlap` option works as expected. + [#1921](https://github.com/lovell/sharp/pull/1921) + [@rustyguts](https://github.com/rustyguts) + +* Allow compilation on FreeBSD and variants (broken since v0.23.0) + [#1952](https://github.com/lovell/sharp/pull/1952) + [@pouya-eghbali](https://github.com/pouya-eghbali) + +* Ensure `modulate` and other colour-based operations can co-exist. + [#1958](https://github.com/lovell/sharp/issues/1958) diff --git a/docs/src/content/docs/changelog/v0.23.4.md b/docs/src/content/docs/changelog/v0.23.4.md new file mode 100644 index 000000000..3d8bfed73 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.23.4.md @@ -0,0 +1,12 @@ +--- +title: v0.23.4 - 5th December 2019 +slug: changelog/v0.23.4 +--- + +* Handle zero-length Buffer objects when using Node.js v13.2.0+. + +* Expose raw TIFFTAG_PHOTOSHOP metadata. + [#1600](https://github.com/lovell/sharp/issues/1600) + +* Improve thread safety by using copy-on-write when updating metadata. + [#1986](https://github.com/lovell/sharp/issues/1986) diff --git a/docs/src/content/docs/changelog/v0.24.0.md b/docs/src/content/docs/changelog/v0.24.0.md new file mode 100644 index 000000000..378dad771 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.24.0.md @@ -0,0 +1,28 @@ +--- +title: v0.24.0 - 16th January 2020 +slug: changelog/v0.24.0 +--- + +* Drop support for Node.js 8. + [#1910](https://github.com/lovell/sharp/issues/1910) + +* Drop support for undefined input where options also provided. + [#1768](https://github.com/lovell/sharp/issues/1768) + +* Move `limitInputPixels` and `sequentialRead` to input options, deprecating functions of the same name. + +* Expose `delay` and `loop` metadata for animated images. + [#1905](https://github.com/lovell/sharp/issues/1905) + +* Ensure correct colour output for 16-bit, 2-channel PNG input with ICC profile. + [#2013](https://github.com/lovell/sharp/issues/2013) + +* Prevent use of sequentialRead for rotate operations. + [#2016](https://github.com/lovell/sharp/issues/2016) + +* Correctly bind max width and height values when using withoutEnlargement. + [#2024](https://github.com/lovell/sharp/pull/2024) + [@BrychanOdlum](https://github.com/BrychanOdlum) + +* Add support for input with 16-bit RGB profile. + [#2037](https://github.com/lovell/sharp/issues/2037) diff --git a/docs/src/content/docs/changelog/v0.24.1.md b/docs/src/content/docs/changelog/v0.24.1.md new file mode 100644 index 000000000..2155c2005 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.24.1.md @@ -0,0 +1,10 @@ +--- +title: v0.24.1 - 15th February 2020 +slug: changelog/v0.24.1 +--- + +* Prevent use of sequentialRead for EXIF-based rotate operation. + [#2042](https://github.com/lovell/sharp/issues/2042) + +* Ensure RGBA LZW TIFF returns correct channel count. + [#2064](https://github.com/lovell/sharp/issues/2064) diff --git a/docs/src/content/docs/changelog/v0.25.0.md b/docs/src/content/docs/changelog/v0.25.0.md new file mode 100644 index 000000000..b42978a98 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.25.0.md @@ -0,0 +1,18 @@ +--- +title: v0.25.0 - 7th March 2020 +slug: changelog/v0.25.0 +--- + +* Remove `limitInputPixels` and `sequentialRead` previously deprecated in v0.24.0. + +* Migrate internals to N-API. + [#1282](https://github.com/lovell/sharp/issues/1282) + +* Add support for 32-bit Windows. + [#2088](https://github.com/lovell/sharp/issues/2088) + +* Ensure correct ordering of rotate-then-trim operations. + [#2087](https://github.com/lovell/sharp/issues/2087) + +* Ensure composite accepts `limitInputPixels` and `sequentialRead` input options. + [#2099](https://github.com/lovell/sharp/issues/2099) diff --git a/docs/src/content/docs/changelog/v0.25.1.md b/docs/src/content/docs/changelog/v0.25.1.md new file mode 100644 index 000000000..af0fe39d4 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.25.1.md @@ -0,0 +1,7 @@ +--- +title: v0.25.1 - 7th March 2020 +slug: changelog/v0.25.1 +--- + +* Ensure prebuilt binaries are fetched based on N-API version. + [#2117](https://github.com/lovell/sharp/issues/2117) diff --git a/docs/src/content/docs/changelog/v0.25.2.md b/docs/src/content/docs/changelog/v0.25.2.md new file mode 100644 index 000000000..298db561a --- /dev/null +++ b/docs/src/content/docs/changelog/v0.25.2.md @@ -0,0 +1,19 @@ +--- +title: v0.25.2 - 20th March 2020 +slug: changelog/v0.25.2 +--- + +* Provide prebuilt binaries for Linux ARM64v8. + +* Add IIIF layout support to tile-based output. + [#2098](https://github.com/lovell/sharp/pull/2098) + [@edsilv](https://github.com/edsilv) + +* Ensure input options are consistently and correctly detected. + [#2118](https://github.com/lovell/sharp/issues/2118) + +* Ensure N-API prebuilt binaries work on RHEL7 and its derivatives. + [#2119](https://github.com/lovell/sharp/issues/2119) + +* Ensure AsyncWorker options are persisted. + [#2130](https://github.com/lovell/sharp/issues/2130) diff --git a/docs/src/content/docs/changelog/v0.25.3.md b/docs/src/content/docs/changelog/v0.25.3.md new file mode 100644 index 000000000..0a1a76038 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.25.3.md @@ -0,0 +1,14 @@ +--- +title: v0.25.3 - 17th May 2020 +slug: changelog/v0.25.3 +--- + +* Ensure libvips is initialised only once, improves worker thread safety. + [#2143](https://github.com/lovell/sharp/issues/2143) + +* Ensure npm platform flag is respected when copying DLLs. + [#2188](https://github.com/lovell/sharp/pull/2188) + [@dimadeveatii](https://github.com/dimadeveatii) + +* Allow SVG input with large inline images to be parsed. + [#2195](https://github.com/lovell/sharp/issues/2195) diff --git a/docs/src/content/docs/changelog/v0.25.4.md b/docs/src/content/docs/changelog/v0.25.4.md new file mode 100644 index 000000000..6bd4deb59 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.25.4.md @@ -0,0 +1,25 @@ +--- +title: v0.25.4 - 12th June 2020 +slug: changelog/v0.25.4 +--- + +* Allow libvips binary location override where version is appended. + [#2217](https://github.com/lovell/sharp/pull/2217) + [@malice00](https://github.com/malice00) + +* Enable PNG palette when setting quality, colours, colors or dither. + [#2226](https://github.com/lovell/sharp/pull/2226) + [@romaleev](https://github.com/romaleev) + +* Add `level` constructor option to use a specific level of a multi-level image. + Expose `levels` metadata for multi-level images. + [#2222](https://github.com/lovell/sharp/issues/2222) + +* Add support for named `alpha` channel to `extractChannel` operation. + [#2138](https://github.com/lovell/sharp/issues/2138) + +* Add experimental `sharpness` calculation to `stats()` response. + [#2251](https://github.com/lovell/sharp/issues/2251) + +* Emit `warning` event for non-critical processing problems. + [#2032](https://github.com/lovell/sharp/issues/2032) diff --git a/docs/src/content/docs/changelog/v0.26.0.md b/docs/src/content/docs/changelog/v0.26.0.md new file mode 100644 index 000000000..6fedd7e35 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.26.0.md @@ -0,0 +1,33 @@ +--- +title: v0.26.0 - 25th August 2020 +slug: changelog/v0.26.0 +--- + +* Prebuilt libvips binaries are now statically-linked and Brotli-compressed, requiring Node.js 10.16.0+. + +* TIFF output `squash` is replaced by `bitdepth` to reduce to 1, 2 or 4 bit. + +* JPEG output `quality` >= 90 no longer automatically sets `chromaSubsampling` to `4:4:4`. + +* Add most `dominant` colour to image `stats`. + [#640](https://github.com/lovell/sharp/issues/640) + +* Add support for animated GIF (requires \*magick) and WebP output. + [#2012](https://github.com/lovell/sharp/pull/2012) + [@deftomat](https://github.com/deftomat) + +* Add support for libvips ImageMagick v7 loaders. + [#2258](https://github.com/lovell/sharp/pull/2258) + [@vouillon](https://github.com/vouillon) + +* Allow multi-page input via \*magick. + [#2259](https://github.com/lovell/sharp/pull/2259) + [@vouillon](https://github.com/vouillon) + +* Add support to `withMetadata` for custom ICC profile. + [#2271](https://github.com/lovell/sharp/pull/2271) + [@roborourke](https://github.com/roborourke) + +* Ensure prebuilt binaries for ARM default to v7 when using Electron. + [#2292](https://github.com/lovell/sharp/pull/2292) + [@diegodev3](https://github.com/diegodev3) diff --git a/docs/src/content/docs/changelog/v0.26.1.md b/docs/src/content/docs/changelog/v0.26.1.md new file mode 100644 index 000000000..080b05da3 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.26.1.md @@ -0,0 +1,22 @@ +--- +title: v0.26.1 - 20th September 2020 +slug: changelog/v0.26.1 +--- + +* 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) diff --git a/docs/src/content/docs/changelog/v0.26.2.md b/docs/src/content/docs/changelog/v0.26.2.md new file mode 100644 index 000000000..2031300a2 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.26.2.md @@ -0,0 +1,15 @@ +--- +title: v0.26.2 - 14th October 2020 +slug: changelog/v0.26.2 +--- + +* 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) diff --git a/docs/src/content/docs/changelog/v0.26.3.md b/docs/src/content/docs/changelog/v0.26.3.md new file mode 100644 index 000000000..a12a54480 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.26.3.md @@ -0,0 +1,12 @@ +--- +title: v0.26.3 - 16th November 2020 +slug: changelog/v0.26.3 +--- + +* 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) diff --git a/docs/src/content/docs/changelog/v0.27.0.md b/docs/src/content/docs/changelog/v0.27.0.md new file mode 100644 index 000000000..d21378479 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.27.0.md @@ -0,0 +1,15 @@ +--- +title: v0.27.0 - 22nd December 2020 +slug: changelog/v0.27.0 +--- + +* 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) diff --git a/docs/src/content/docs/changelog/v0.27.1.md b/docs/src/content/docs/changelog/v0.27.1.md new file mode 100644 index 000000000..09b24da60 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.27.1.md @@ -0,0 +1,19 @@ +--- +title: v0.27.1 - 27th January 2021 +slug: changelog/v0.27.1 +--- + +* 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) diff --git a/docs/src/content/docs/changelog/v0.27.2.md b/docs/src/content/docs/changelog/v0.27.2.md new file mode 100644 index 000000000..b8eb9c386 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.27.2.md @@ -0,0 +1,20 @@ +--- +title: v0.27.2 - 22nd February 2021 +slug: changelog/v0.27.2 +--- + +* 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) diff --git a/docs/src/content/docs/changelog/v0.28.0.md b/docs/src/content/docs/changelog/v0.28.0.md new file mode 100644 index 000000000..e3a72f815 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.28.0.md @@ -0,0 +1,32 @@ +--- +title: v0.28.0 - 29th March 2021 +slug: changelog/v0.28.0 +--- + +* Prebuilt binaries now include mozjpeg and libimagequant (BSD 2-Clause). + +* Prebuilt binaries limit AVIF support to the most common 8-bit depth. + +* Add `mozjpeg` option to `jpeg` method, sets mozjpeg defaults. + +* Reduce the default PNG `compressionLevel` to the more commonly used 6. + +* Reduce concurrency on glibc-based Linux when using the default memory allocator to help prevent fragmentation. + +* Default missing edge properties of extend operation to zero. + [#2578](https://github.com/lovell/sharp/issues/2578) + +* Ensure composite does not clip top and left offsets. + [#2594](https://github.com/lovell/sharp/pull/2594) + [@SHG42](https://github.com/SHG42) + +* Improve error handling of network failure at install time. + [#2608](https://github.com/lovell/sharp/pull/2608) + [@abradley](https://github.com/abradley) + +* Ensure `@id` attribute can be set for IIIF tile-based output. + [#2612](https://github.com/lovell/sharp/issues/2612) + [@edsilv](https://github.com/edsilv) + +* Ensure composite replicates the correct number of tiles for centred gravities. + [#2626](https://github.com/lovell/sharp/issues/2626) diff --git a/docs/src/content/docs/changelog/v0.28.1.md b/docs/src/content/docs/changelog/v0.28.1.md new file mode 100644 index 000000000..38e11a38a --- /dev/null +++ b/docs/src/content/docs/changelog/v0.28.1.md @@ -0,0 +1,15 @@ +--- +title: v0.28.1 - 5th April 2021 +slug: changelog/v0.28.1 +--- + +* Ensure all installation errors are logged with a more obvious prefix. + +* Allow `withMetadata` to set and update EXIF metadata. + [#650](https://github.com/lovell/sharp/issues/650) + +* Add support for OME-TIFF Sub Image File Directories (subIFD). + [#2557](https://github.com/lovell/sharp/issues/2557) + +* Allow `ensureAlpha` to set the alpha transparency level. + [#2634](https://github.com/lovell/sharp/issues/2634) diff --git a/docs/src/content/docs/changelog/v0.28.2.md b/docs/src/content/docs/changelog/v0.28.2.md new file mode 100644 index 000000000..9f067c44f --- /dev/null +++ b/docs/src/content/docs/changelog/v0.28.2.md @@ -0,0 +1,26 @@ +--- +title: v0.28.2 - 10th May 2021 +slug: changelog/v0.28.2 +--- + +* Allow `withMetadata` to set `density`. + [#967](https://github.com/lovell/sharp/issues/967) + +* Skip shrink-on-load where one dimension <4px. + [#2653](https://github.com/lovell/sharp/issues/2653) + +* Allow escaped proxy credentials. + [#2664](https://github.com/lovell/sharp/pull/2664) + [@msalettes](https://github.com/msalettes) + +* Add `premultiplied` flag for raw pixel data input. + [#2685](https://github.com/lovell/sharp/pull/2685) + [@mnutt](https://github.com/mnutt) + +* Detect empty input and throw a helpful error. + [#2687](https://github.com/lovell/sharp/pull/2687) + [@JakobJingleheimer](https://github.com/JakobJingleheimer) + +* Add install-time flag to skip version compatibility checks. + [#2692](https://github.com/lovell/sharp/pull/2692) + [@xemle](https://github.com/xemle) diff --git a/docs/src/content/docs/changelog/v0.28.3.md b/docs/src/content/docs/changelog/v0.28.3.md new file mode 100644 index 000000000..dff933269 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.28.3.md @@ -0,0 +1,13 @@ +--- +title: v0.28.3 - 24th May 2021 +slug: changelog/v0.28.3 +--- + +* Ensure presence of libvips, vendored or global, before invoking node-gyp. + +* Skip shrink-on-load for multi-page WebP. + [#2714](https://github.com/lovell/sharp/issues/2714) + +* Add contrast limiting adaptive histogram equalization (CLAHE) operator. + [#2726](https://github.com/lovell/sharp/pull/2726) + [@baparham](https://github.com/baparham) diff --git a/docs/src/content/docs/changelog/v0.29.0.md b/docs/src/content/docs/changelog/v0.29.0.md new file mode 100644 index 000000000..087545991 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.29.0.md @@ -0,0 +1,35 @@ +--- +title: v0.29.0 - 17th August 2021 +slug: changelog/v0.29.0 +--- + +* Drop support for Node.js 10, now requires Node.js >= 12.13.0. + +* Add `background` property to PNG and GIF image metadata. + +* Add `compression` property to HEIF image metadata. + [#2504](https://github.com/lovell/sharp/issues/2504) + +* AVIF encoding now defaults to `4:4:4` chroma subsampling. + [#2562](https://github.com/lovell/sharp/issues/2562) + +* Allow multiple platform-arch binaries in same `node_modules` installation tree. + [#2575](https://github.com/lovell/sharp/issues/2575) + +* Default to single-channel `b-w` space when `extractChannel` is used. + [#2658](https://github.com/lovell/sharp/issues/2658) + +* Allow installation directory to contain spaces (regression in v0.26.0). + [#2777](https://github.com/lovell/sharp/issues/2777) + +* Add `pipelineColourspace` operator to set the processing space. + [#2704](https://github.com/lovell/sharp/pull/2704) + [@Daiz](https://github.com/Daiz) + +* Allow bit depth to be set when using raw input and output. + [#2762](https://github.com/lovell/sharp/pull/2762) + [@mart-jansink](https://github.com/mart-jansink) + +* Allow `negate` to act only on non-alpha channels. + [#2808](https://github.com/lovell/sharp/pull/2808) + [@rexxars](https://github.com/rexxars) diff --git a/docs/src/content/docs/changelog/v0.29.1.md b/docs/src/content/docs/changelog/v0.29.1.md new file mode 100644 index 000000000..58809cbf4 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.29.1.md @@ -0,0 +1,30 @@ +--- +title: v0.29.1 - 7th September 2021 +slug: changelog/v0.29.1 +--- + +* Add `lightness` option to `modulate` operation. + [#2846](https://github.com/lovell/sharp/pull/2846) + +* Ensure correct PNG bitdepth is set based on number of colours. + [#2855](https://github.com/lovell/sharp/issues/2855) + +* Ensure background is always premultiplied when compositing. + [#2858](https://github.com/lovell/sharp/issues/2858) + +* Ensure images with P3 profiles retain full gamut. + [#2862](https://github.com/lovell/sharp/issues/2862) + +* Add support for libvips compiled with OpenJPEG. + [#2868](https://github.com/lovell/sharp/pull/2868) + +* Remove unsupported animation properties from AVIF output. + [#2870](https://github.com/lovell/sharp/issues/2870) + +* Resolve paths before comparing input/output filenames. + [#2878](https://github.com/lovell/sharp/pull/2878) + [@rexxars](https://github.com/rexxars) + +* Allow use of speed 9 (fastest) for HEIF encoding. + [#2879](https://github.com/lovell/sharp/pull/2879) + [@rexxars](https://github.com/rexxars) diff --git a/docs/src/content/docs/changelog/v0.29.2.md b/docs/src/content/docs/changelog/v0.29.2.md new file mode 100644 index 000000000..0628963f0 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.29.2.md @@ -0,0 +1,22 @@ +--- +title: v0.29.2 - 21st October 2021 +slug: changelog/v0.29.2 +--- + +* Add `timeout` function to limit processing time. + +* Ensure `sharp.versions` is populated from vendored libvips. + +* Remove animation properties from single page images. + [#2890](https://github.com/lovell/sharp/issues/2890) + +* Allow use of 'tif' to select TIFF output. + [#2893](https://github.com/lovell/sharp/pull/2893) + [@erf](https://github.com/erf) + +* Improve error message on Windows for version conflict. + [#2918](https://github.com/lovell/sharp/pull/2918) + [@dkrnl](https://github.com/dkrnl) + +* Throw error rather than exit when invalid binaries detected. + [#2931](https://github.com/lovell/sharp/issues/2931) diff --git a/docs/src/content/docs/changelog/v0.29.3.md b/docs/src/content/docs/changelog/v0.29.3.md new file mode 100644 index 000000000..e344d790e --- /dev/null +++ b/docs/src/content/docs/changelog/v0.29.3.md @@ -0,0 +1,11 @@ +--- +title: v0.29.3 - 14th November 2021 +slug: changelog/v0.29.3 +--- + +* Ensure correct dimensions when containing image resized to 1px. + [#2951](https://github.com/lovell/sharp/issues/2951) + +* Impute TIFF `xres`/`yres` from `density` provided to `withMetadata`. + [#2952](https://github.com/lovell/sharp/pull/2952) + [@mbklein](https://github.com/mbklein) diff --git a/docs/src/content/docs/changelog/v0.30.0.md b/docs/src/content/docs/changelog/v0.30.0.md new file mode 100644 index 000000000..2605fbcaa --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.0.md @@ -0,0 +1,48 @@ +--- +title: v0.30.0 - 1st February 2022 +slug: changelog/v0.30.0 +--- + +* Add support for GIF output to prebuilt binaries. + +* Reduce minimum Linux ARM64v8 glibc requirement to 2.17. + +* Verify prebuilt binaries with a Subresource Integrity check. + +* Standardise WebP `effort` option name, deprecate `reductionEffort`. + +* Standardise HEIF `effort` option name, deprecate `speed`. + +* Add support for IIIF v3 tile-based output. + +* Expose control over CPU effort for palette-based PNG output. + [#2541](https://github.com/lovell/sharp/issues/2541) + +* Improve animated (multi-page) image resize and extract. + [#2789](https://github.com/lovell/sharp/pull/2789) + [@kleisauke](https://github.com/kleisauke) + +* Expose platform and architecture of vendored binaries as `sharp.vendor`. + [#2928](https://github.com/lovell/sharp/issues/2928) + +* Ensure 16-bit PNG output uses correct bitdepth. + [#2958](https://github.com/lovell/sharp/pull/2958) + [@gforge](https://github.com/gforge) + +* Properly emit close events for duplex streams. + [#2976](https://github.com/lovell/sharp/pull/2976) + [@driannaude](https://github.com/driannaude) + +* Expose `unlimited` option for SVG and PNG input, switches off safety features. + [#2984](https://github.com/lovell/sharp/issues/2984) + +* Add `withoutReduction` option to resize operation. + [#3006](https://github.com/lovell/sharp/pull/3006) + [@christopherbradleybanks](https://github.com/christopherbradleybanks) + +* Add `resolutionUnit` as `tiff` option and expose in metadata. + [#3023](https://github.com/lovell/sharp/pull/3023) + [@ompal-sisodiya](https://github.com/ompal-sisodiya) + +* Ensure rotate-then-extract works with EXIF mirroring. + [#3024](https://github.com/lovell/sharp/issues/3024) diff --git a/docs/src/content/docs/changelog/v0.30.1.md b/docs/src/content/docs/changelog/v0.30.1.md new file mode 100644 index 000000000..06f890da8 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.1.md @@ -0,0 +1,19 @@ +--- +title: v0.30.1 - 9th February 2022 +slug: changelog/v0.30.1 +--- + +* Allow use of `toBuffer` and `toFile` on the same instance. + [#3044](https://github.com/lovell/sharp/issues/3044) + +* Skip shrink-on-load for known libjpeg rounding errors. + [#3066](https://github.com/lovell/sharp/issues/3066) + [@kleisauke](https://github.com/kleisauke) + +* Ensure withoutReduction does not interfere with contain/crop/embed. + [#3081](https://github.com/lovell/sharp/pull/3081) + [@kleisauke](https://github.com/kleisauke) + +* Ensure affine interpolator is correctly finalised. + [#3083](https://github.com/lovell/sharp/pull/3083) + [@kleisauke](https://github.com/kleisauke) diff --git a/docs/src/content/docs/changelog/v0.30.2.md b/docs/src/content/docs/changelog/v0.30.2.md new file mode 100644 index 000000000..1b5c7e1c5 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.2.md @@ -0,0 +1,17 @@ +--- +title: v0.30.2 - 2nd March 2022 +slug: changelog/v0.30.2 +--- + +* Improve performance and accuracy when compositing multiple images. + [#2286](https://github.com/lovell/sharp/issues/2286) + +* Expand pkgconfig search path for wider BSD support. + [#3106](https://github.com/lovell/sharp/issues/3106) + +* Ensure Windows C++ runtime is linked statically (regression in 0.30.0). + [#3110](https://github.com/lovell/sharp/pull/3110) + [@kleisauke](https://github.com/kleisauke) + +* Temporarily ignore greyscale ICC profiles to workaround lcms bug. + [#3112](https://github.com/lovell/sharp/issues/3112) diff --git a/docs/src/content/docs/changelog/v0.30.3.md b/docs/src/content/docs/changelog/v0.30.3.md new file mode 100644 index 000000000..77a2584a8 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.3.md @@ -0,0 +1,13 @@ +--- +title: v0.30.3 - 14th March 2022 +slug: changelog/v0.30.3 +--- + +* Allow `sharpen` options to be provided more consistently as an Object. + [#2561](https://github.com/lovell/sharp/issues/2561) + +* Expose `x1`, `y2` and `y3` parameters of `sharpen` operation. + [#2935](https://github.com/lovell/sharp/issues/2935) + +* Prevent double unpremultiply with some composite blend modes (regression in 0.30.2). + [#3118](https://github.com/lovell/sharp/issues/3118) diff --git a/docs/src/content/docs/changelog/v0.30.4.md b/docs/src/content/docs/changelog/v0.30.4.md new file mode 100644 index 000000000..a4f0040bf --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.4.md @@ -0,0 +1,20 @@ +--- +title: v0.30.4 - 18th April 2022 +slug: changelog/v0.30.4 +--- + +* Increase control over sensitivity to invalid images via `failOn`, deprecate `failOnError` (equivalent to `failOn: 'warning'`). + +* Ensure `create` input image has correct bit depth and colour space. + [#3139](https://github.com/lovell/sharp/issues/3139) + +* Add support for `TypedArray` input with `byteOffset` and `length`. + [#3146](https://github.com/lovell/sharp/pull/3146) + [@codepage949](https://github.com/codepage949) + +* Improve error message when attempting to render SVG input greater than 32767x32767. + [#3167](https://github.com/lovell/sharp/issues/3167) + +* Add missing file name to 'Input file is missing' error message. + [#3178](https://github.com/lovell/sharp/pull/3178) + [@Brodan](https://github.com/Brodan) diff --git a/docs/src/content/docs/changelog/v0.30.5.md b/docs/src/content/docs/changelog/v0.30.5.md new file mode 100644 index 000000000..406357619 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.5.md @@ -0,0 +1,19 @@ +--- +title: v0.30.5 - 23rd May 2022 +slug: changelog/v0.30.5 +--- + +* Install: pass `PKG_CONFIG_PATH` via env rather than substitution. + [@dwisiswant0](https://github.com/dwisiswant0) + +* Add support for `--libc` flag to improve cross-platform installation. + [#3160](https://github.com/lovell/sharp/pull/3160) + [@joonamo](https://github.com/joonamo) + +* Allow installation of prebuilt libvips binaries from filesystem. + [#3196](https://github.com/lovell/sharp/pull/3196) + [@ankurparihar](https://github.com/ankurparihar) + +* Fix rotate-then-extract for EXIF orientation 2. + [#3218](https://github.com/lovell/sharp/pull/3218) + [@jakob0fischl](https://github.com/jakob0fischl) diff --git a/docs/src/content/docs/changelog/v0.30.6.md b/docs/src/content/docs/changelog/v0.30.6.md new file mode 100644 index 000000000..340921040 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.6.md @@ -0,0 +1,10 @@ +--- +title: v0.30.6 - 30th May 2022 +slug: changelog/v0.30.6 +--- + +* Allow values for `limitInputPixels` larger than 32-bit. + [#3238](https://github.com/lovell/sharp/issues/3238) + +* Ensure brew-installed `vips` can be detected (regression in 0.30.5). + [#3239](https://github.com/lovell/sharp/issues/3239) diff --git a/docs/src/content/docs/changelog/v0.30.7.md b/docs/src/content/docs/changelog/v0.30.7.md new file mode 100644 index 000000000..fd90d905c --- /dev/null +++ b/docs/src/content/docs/changelog/v0.30.7.md @@ -0,0 +1,15 @@ +--- +title: v0.30.7 - 22nd June 2022 +slug: changelog/v0.30.7 +--- + +* Ensure tiled composition always works with outside resizing. + [#3227](https://github.com/lovell/sharp/issues/3227) + +* Allow WebP encoding effort of 0. + [#3261](https://github.com/lovell/sharp/pull/3261) + [@AlexanderTheGrey](https://github.com/AlexanderTheGrey) + +* Prevent upsampling via libwebp. + [#3267](https://github.com/lovell/sharp/pull/3267) + [@blacha](https://github.com/blacha) diff --git a/docs/src/content/docs/changelog/v0.31.0.md b/docs/src/content/docs/changelog/v0.31.0.md new file mode 100644 index 000000000..1998f9be8 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.31.0.md @@ -0,0 +1,63 @@ +--- +title: v0.31.0 - 5th September 2022 +slug: changelog/v0.31.0 +--- + +* Drop support for Node.js 12, now requires Node.js >= 14.15.0. + +* GIF output now re-uses input palette if possible. Use `reoptimise` option to generate a new palette. + +* Add WebP `minSize` and `mixed` options for greater control over animation frames. + +* Remove previously-deprecated WebP `reductionEffort` and HEIF `speed` options. Use `effort` to control these. + +* The `flip` and `flop` operations will now occur before the `rotate` operation. + +* Improve `normalise` operation with use of histogram. + [#200](https://github.com/lovell/sharp/issues/200) + +* Use combined bounding box of alpha and non-alpha channels for `trim` operation. + [#2166](https://github.com/lovell/sharp/issues/2166) + +* Add Buffer and Stream support to tile-based output. + [#2238](https://github.com/lovell/sharp/issues/2238) + +* Add input `fileSuffix` and output `alias` to `format` information. + [#2642](https://github.com/lovell/sharp/issues/2642) + +* Re-introduce support for greyscale ICC profiles (temporarily removed in 0.30.2). + [#3114](https://github.com/lovell/sharp/issues/3114) + +* Add support for WebP and PackBits `compression` options with TIFF output. + [#3198](https://github.com/lovell/sharp/issues/3198) + +* Ensure OpenSlide and FITS input works with custom libvips. + [#3226](https://github.com/lovell/sharp/issues/3226) + +* Ensure `trim` operation is a no-op when it would reduce an image to nothing. + [#3223](https://github.com/lovell/sharp/issues/3223) + +* Expose `vips_text` to create an image containing rendered text. + [#3252](https://github.com/lovell/sharp/pull/3252) + [@brahima](https://github.com/brahima) + +* Ensure only properties owned by the `withMetadata` EXIF Object are parsed. + [#3292](https://github.com/lovell/sharp/issues/3292) + +* Expand `linear` operation to allow use of per-channel arrays. + [#3303](https://github.com/lovell/sharp/pull/3303) + [@antonmarsden](https://github.com/antonmarsden) + +* Ensure the order of `rotate`, `resize` and `extend` operations is respected where possible. + Emit warnings when previous calls in the same pipeline will be ignored. + [#3319](https://github.com/lovell/sharp/issues/3319) + +* Ensure PNG bitdepth can be set for non-palette output. + [#3322](https://github.com/lovell/sharp/issues/3322) + +* Add trim option to provide a specific background colour. + [#3332](https://github.com/lovell/sharp/pull/3332) + [@mart-jansink](https://github.com/mart-jansink) + +* Ensure resized image is unpremultiplied before composite. + [#3334](https://github.com/lovell/sharp/issues/3334) diff --git a/docs/src/content/docs/changelog/v0.31.1.md b/docs/src/content/docs/changelog/v0.31.1.md new file mode 100644 index 000000000..44467514f --- /dev/null +++ b/docs/src/content/docs/changelog/v0.31.1.md @@ -0,0 +1,22 @@ +--- +title: v0.31.1 - 29th September 2022 +slug: changelog/v0.31.1 +--- + +* Upgrade to libvips v8.13.2 for upstream bug fixes. + +* Ensure `close` event occurs after `end` event for Stream-based output. + [#3313](https://github.com/lovell/sharp/issues/3313) + +* Ensure `limitInputPixels` constructor option uses uint64. + [#3349](https://github.com/lovell/sharp/pull/3349) + [@marcosc90](https://github.com/marcosc90) + +* Ensure auto-rotation works with shrink-on-load and extract (regression in 0.31.0). + [#3352](https://github.com/lovell/sharp/issues/3352) + +* Ensure AVIF output is always 8-bit. + [#3358](https://github.com/lovell/sharp/issues/3358) + +* Ensure greyscale images can be trimmed (regression in 0.31.0). + [#3386](https://github.com/lovell/sharp/issues/3386) diff --git a/docs/src/content/docs/changelog/v0.31.2.md b/docs/src/content/docs/changelog/v0.31.2.md new file mode 100644 index 000000000..c0be292b0 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.31.2.md @@ -0,0 +1,12 @@ +--- +title: v0.31.2 - 4th November 2022 +slug: changelog/v0.31.2 +--- + +* Upgrade to libvips v8.13.3 for upstream bug fixes. + +* Ensure manual flip, rotate, resize operation ordering (regression in 0.31.1) + [#3391](https://github.com/lovell/sharp/issues/3391) + +* Ensure auto-rotation works without resize (regression in 0.31.1) + [#3422](https://github.com/lovell/sharp/issues/3422) diff --git a/docs/src/content/docs/changelog/v0.31.3.md b/docs/src/content/docs/changelog/v0.31.3.md new file mode 100644 index 000000000..84c6ac899 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.31.3.md @@ -0,0 +1,34 @@ +--- +title: v0.31.3 - 21st December 2022 +slug: changelog/v0.31.3 +--- + +* Add experimental support for JPEG-XL images. Requires libvips compiled with libjxl. + [#2731](https://github.com/lovell/sharp/issues/2731) + +* Add runtime detection of V8 memory cage, ensures compatibility with Electron 21 onwards. + [#3384](https://github.com/lovell/sharp/issues/3384) + +* Expose `interFrameMaxError` and `interPaletteMaxError` GIF optimisation properties. + [#3401](https://github.com/lovell/sharp/issues/3401) + +* Allow installation on Linux with glibc patch versions e.g. Fedora 38. + [#3423](https://github.com/lovell/sharp/issues/3423) + +* Expand range of existing `sharpen` parameters to match libvips. + [#3427](https://github.com/lovell/sharp/issues/3427) + +* Prevent possible race condition awaiting metadata of Stream-based input. + [#3451](https://github.com/lovell/sharp/issues/3451) + +* Improve `extractChannel` support for 16-bit output colourspaces. + [#3453](https://github.com/lovell/sharp/issues/3453) + +* Ignore `sequentialRead` option when calculating image statistics. + [#3462](https://github.com/lovell/sharp/issues/3462) + +* Small performance improvement for operations that introduce a non-opaque background. + [#3465](https://github.com/lovell/sharp/issues/3465) + +* Ensure integral output of `linear` operation. + [#3468](https://github.com/lovell/sharp/issues/3468) diff --git a/docs/src/content/docs/changelog/v0.32.0.md b/docs/src/content/docs/changelog/v0.32.0.md new file mode 100644 index 000000000..f86104bfa --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.0.md @@ -0,0 +1,61 @@ +--- +title: v0.32.0 - 24th March 2023 +slug: changelog/v0.32.0 +--- + +* Default to using sequential rather than random access read where possible. + +* Replace GIF output `optimise` / `optimize` option with `reuse`. + +* Add `progressive` option to GIF output for interlacing. + +* Add `wrap` option to text image creation. + +* Add `formatMagick` property to metadata of images loaded via *magick. + +* Prefer integer (un)premultiply for faster resizing of RGBA images. + +* Add `ignoreIcc` input option to ignore embedded ICC profile. + +* Allow use of GPS (IFD3) EXIF metadata. + [#2767](https://github.com/lovell/sharp/issues/2767) + +* TypeScript definitions are now maintained and published directly, deprecating the `@types/sharp` package. + [#3369](https://github.com/lovell/sharp/issues/3369) + +* Prebuilt binaries: ensure macOS 10.13+ support, as documented. + [#3438](https://github.com/lovell/sharp/issues/3438) + +* Prebuilt binaries: prevent use of glib slice allocator, improves QEMU support. + [#3448](https://github.com/lovell/sharp/issues/3448) + +* Add focus point coordinates to output when using attention based crop. + [#3470](https://github.com/lovell/sharp/pull/3470) + [@ejoebstl](https://github.com/ejoebstl) + +* Expose sharp version as `sharp.versions.sharp`. + [#3471](https://github.com/lovell/sharp/issues/3471) + +* Respect `fastShrinkOnLoad` resize option for WebP input. + [#3516](https://github.com/lovell/sharp/issues/3516) + +* Reduce sharpen `sigma` maximum from 10000 to 10. + [#3521](https://github.com/lovell/sharp/issues/3521) + +* Add support for `ArrayBuffer` input. + [#3548](https://github.com/lovell/sharp/pull/3548) + [@kapouer](https://github.com/kapouer) + +* Add support to `extend` operation for `extendWith` to allow copy/mirror/repeat. + [#3556](https://github.com/lovell/sharp/pull/3556) + [@janaz](https://github.com/janaz) + +* Ensure all async JS callbacks are wrapped to help avoid possible race condition. + [#3569](https://github.com/lovell/sharp/issues/3569) + +* Prebuilt binaries: support for tile-based output temporarily removed due to licensing issue. + [#3581](https://github.com/lovell/sharp/issues/3581) + +* Add support to `normalise` for `lower` and `upper` percentiles. + [#3583](https://github.com/lovell/sharp/pull/3583) + [@LachlanNewman](https://github.com/LachlanNewman) diff --git a/docs/src/content/docs/changelog/v0.32.1.md b/docs/src/content/docs/changelog/v0.32.1.md new file mode 100644 index 000000000..a73cfb93e --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.1.md @@ -0,0 +1,30 @@ +--- +title: v0.32.1 - 27th April 2023 +slug: changelog/v0.32.1 +--- + +* Add experimental `unflatten` operation. + [#3461](https://github.com/lovell/sharp/pull/3461) + [@antonmarsden](https://github.com/antonmarsden) + +* Ensure use of `flip` operation forces random access read (regression in 0.32.0). + [#3600](https://github.com/lovell/sharp/issues/3600) + +* Ensure `linear` operation works with 16-bit input (regression in 0.31.3). + [#3605](https://github.com/lovell/sharp/issues/3605) + +* Install: ensure proxy URLs are logged correctly. + [#3615](https://github.com/lovell/sharp/pull/3615) + [@TomWis97](https://github.com/TomWis97) + +* Ensure profile-less CMYK to CMYK roundtrip skips colourspace conversion. + [#3620](https://github.com/lovell/sharp/issues/3620) + +* Add support for `modulate` operation when using non-sRGB pipeline colourspace. + [#3620](https://github.com/lovell/sharp/issues/3620) + +* Ensure `trim` operation works with CMYK images (regression in 0.31.0). + [#3636](https://github.com/lovell/sharp/issues/3636) + +* Install: coerce libc version to semver. + [#3641](https://github.com/lovell/sharp/issues/3641) diff --git a/docs/src/content/docs/changelog/v0.32.2.md b/docs/src/content/docs/changelog/v0.32.2.md new file mode 100644 index 000000000..a14aa1410 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.2.md @@ -0,0 +1,25 @@ +--- +title: v0.32.2 - 11th July 2023 +slug: changelog/v0.32.2 +--- + +* Limit HEIF output dimensions to 16384x16384, matches libvips. + +* Ensure exceptions are not thrown when terminating. + [#3569](https://github.com/lovell/sharp/issues/3569) + +* Ensure the same access method is used for all inputs (regression in 0.32.0). + [#3669](https://github.com/lovell/sharp/issues/3669) + +* Improve detection of jp2 filename extensions. + [#3674](https://github.com/lovell/sharp/pull/3674) + [@bianjunjie1981](https://github.com/bianjunjie1981) + +* Guard use of smartcrop premultiplied option to prevent warning (regression in 0.32.1). + [#3710](https://github.com/lovell/sharp/issues/3710) + +* Prevent over-compute in affine-based rotate before resize. + [#3722](https://github.com/lovell/sharp/issues/3722) + +* Allow sequential read for EXIF-based auto-orientation. + [#3725](https://github.com/lovell/sharp/issues/3725) diff --git a/docs/src/content/docs/changelog/v0.32.3.md b/docs/src/content/docs/changelog/v0.32.3.md new file mode 100644 index 000000000..ac3151982 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.3.md @@ -0,0 +1,10 @@ +--- +title: v0.32.3 - 14th July 2023 +slug: changelog/v0.32.3 +--- + +* Expose `preset` option for WebP output. + [#3639](https://github.com/lovell/sharp/issues/3639) + +* Ensure decoding remains sequential for all operations (regression in 0.32.2). + [#3725](https://github.com/lovell/sharp/issues/3725) diff --git a/docs/src/content/docs/changelog/v0.32.4.md b/docs/src/content/docs/changelog/v0.32.4.md new file mode 100644 index 000000000..f327f8d0b --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.4.md @@ -0,0 +1,11 @@ +--- +title: v0.32.4 - 21st July 2023 +slug: changelog/v0.32.4 +--- + +* Upgrade to libvips v8.14.3 for upstream bug fixes. + +* Expose ability to (un)block low-level libvips operations by name. + +* Prebuilt binaries: restore support for tile-based output. + [#3581](https://github.com/lovell/sharp/issues/3581) diff --git a/docs/src/content/docs/changelog/v0.32.5.md b/docs/src/content/docs/changelog/v0.32.5.md new file mode 100644 index 000000000..3a38ec7ec --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.5.md @@ -0,0 +1,24 @@ +--- +title: v0.32.5 - 15th August 2023 +slug: changelog/v0.32.5 +--- + +* Upgrade to libvips v8.14.4 for upstream bug fixes. + +* TypeScript: Add missing `WebpPresetEnum` to definitions. + [#3748](https://github.com/lovell/sharp/pull/3748) + [@pilotso11](https://github.com/pilotso11) + +* Ensure compilation using musl v1.2.4. + [#3755](https://github.com/lovell/sharp/pull/3755) + [@kleisauke](https://github.com/kleisauke) + +* Ensure resize with a `fit` of `inside` respects 90/270 degree rotation. + [#3756](https://github.com/lovell/sharp/issues/3756) + +* TypeScript: Ensure `minSize` property of `WebpOptions` is boolean. + [#3758](https://github.com/lovell/sharp/pull/3758) + [@sho-xizz](https://github.com/sho-xizz) + +* Ensure `withMetadata` adds default sRGB profile. + [#3761](https://github.com/lovell/sharp/issues/3761) diff --git a/docs/src/content/docs/changelog/v0.32.6.md b/docs/src/content/docs/changelog/v0.32.6.md new file mode 100644 index 000000000..6b7c3d431 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.32.6.md @@ -0,0 +1,19 @@ +--- +title: v0.32.6 - 18th September 2023 +slug: changelog/v0.32.6 +--- + +* Upgrade to libvips v8.14.5 for upstream bug fixes. + +* Ensure composite tile images are fully decoded (regression in 0.32.0). + [#3767](https://github.com/lovell/sharp/issues/3767) + +* Ensure `withMetadata` can add ICC profiles to RGB16 output. + [#3773](https://github.com/lovell/sharp/issues/3773) + +* Ensure `withMetadata` does not reduce 16-bit images to 8-bit (regression in 0.32.5). + [#3773](https://github.com/lovell/sharp/issues/3773) + +* TypeScript: Add definitions for block and unblock. + [#3799](https://github.com/lovell/sharp/pull/3799) + [@ldrick](https://github.com/ldrick) diff --git a/docs/src/content/docs/changelog/v0.33.0.md b/docs/src/content/docs/changelog/v0.33.0.md new file mode 100644 index 000000000..1ca8435de --- /dev/null +++ b/docs/src/content/docs/changelog/v0.33.0.md @@ -0,0 +1,47 @@ +--- +title: v0.33.0 - 29th November 2023 +slug: changelog/v0.33.0 +--- + +* Drop support for Node.js 14 and 16, now requires Node.js ^18.17.0 or >= 20.3.0 + +* Prebuilt binaries distributed via npm registry and installed via package manager. + +* Building from source requires dependency on `node-addon-api`. + +* Remove `sharp.vendor`. + +* Partially deprecate `withMetadata()`, use `withExif()` and `withIccProfile()`. + +* Add experimental support for WebAssembly-based runtimes. + [@RReverser](https://github.com/RReverser) + +* Options for `trim` operation must be an Object, add new `lineArt` option. + [#2363](https://github.com/lovell/sharp/issues/2363) + +* Improve luminance of `tint` operation with weighting function. + [#3338](https://github.com/lovell/sharp/issues/3338) + [@jcupitt](https://github.com/jcupitt) + +* Ensure all `Error` objects contain a `stack` property. + [#3653](https://github.com/lovell/sharp/issues/3653) + +* Make `compression` option of `heif` mandatory to help reduce HEIF vs HEIC confusion. + [#3740](https://github.com/lovell/sharp/issues/3740) + +* Ensure correct interpretation of 16-bit raw input. + [#3808](https://github.com/lovell/sharp/issues/3808) + +* Add support for `miniswhite` when using TIFF output. + [#3812](https://github.com/lovell/sharp/pull/3812) + [@dnsbty](https://github.com/dnsbty) + +* TypeScript: add missing definition for `withMetadata` boolean. + [#3823](https://github.com/lovell/sharp/pull/3823) + [@uhthomas](https://github.com/uhthomas) + +* Add more fine-grained control over output metadata. + [#3824](https://github.com/lovell/sharp/issues/3824) + +* Ensure multi-page extract remains sequential. + [#3837](https://github.com/lovell/sharp/issues/3837) diff --git a/docs/src/content/docs/changelog/v0.33.1.md b/docs/src/content/docs/changelog/v0.33.1.md new file mode 100644 index 000000000..9a280c82f --- /dev/null +++ b/docs/src/content/docs/changelog/v0.33.1.md @@ -0,0 +1,14 @@ +--- +title: v0.33.1 - 17th December 2023 +slug: changelog/v0.33.1 +--- + +* Add support for Yarn Plug'n'Play filesystem layout. + [#3888](https://github.com/lovell/sharp/issues/3888) + +* Emit warning when attempting to use invalid ICC profiles. + [#3895](https://github.com/lovell/sharp/issues/3895) + +* Ensure `VIPS_NOVECTOR` environment variable is respected. + [#3897](https://github.com/lovell/sharp/pull/3897) + [@icetee](https://github.com/icetee) diff --git a/docs/src/content/docs/changelog/v0.33.2.md b/docs/src/content/docs/changelog/v0.33.2.md new file mode 100644 index 000000000..7d88c0adf --- /dev/null +++ b/docs/src/content/docs/changelog/v0.33.2.md @@ -0,0 +1,16 @@ +--- +title: v0.33.2 - 12th January 2024 +slug: changelog/v0.33.2 +--- + +* Upgrade to libvips v8.15.1 for upstream bug fixes. + +* TypeScript: add definition for `keepMetadata`. + [#3914](https://github.com/lovell/sharp/pull/3914) + [@abhi0498](https://github.com/abhi0498) + +* Ensure `extend` operation stays sequential when copying (regression in 0.32.0). + [#3928](https://github.com/lovell/sharp/issues/3928) + +* Improve error handling for unsupported multi-page rotation. + [#3940](https://github.com/lovell/sharp/issues/3940) diff --git a/docs/src/content/docs/changelog/v0.33.3.md b/docs/src/content/docs/changelog/v0.33.3.md new file mode 100644 index 000000000..17e6639fe --- /dev/null +++ b/docs/src/content/docs/changelog/v0.33.3.md @@ -0,0 +1,21 @@ +--- +title: v0.33.3 - 23rd March 2024 +slug: changelog/v0.33.3 +--- + +* Upgrade to libvips v8.15.2 for upstream bug fixes. + +* Ensure `keepIccProfile` retains P3 and CMYK input profiles. + [#3906](https://github.com/lovell/sharp/issues/3906) + [#4008](https://github.com/lovell/sharp/issues/4008) + +* Ensure `text.wrap` property can accept `word-char` as value. + [#4028](https://github.com/lovell/sharp/pull/4028) + [@yolopunk](https://github.com/yolopunk) + +* Ensure `clone` takes a deep copy of existing options. + [#4029](https://github.com/lovell/sharp/issues/4029) + +* Add `bitdepth` option to `heif` output (prebuilt binaries support 8-bit only). + [#4036](https://github.com/lovell/sharp/pull/4036) + [@mertalev](https://github.com/mertalev) diff --git a/docs/src/content/docs/changelog/v0.33.4.md b/docs/src/content/docs/changelog/v0.33.4.md new file mode 100644 index 000000000..9c4500f91 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.33.4.md @@ -0,0 +1,32 @@ +--- +title: v0.33.4 - 16th May 2024 +slug: changelog/v0.33.4 +--- + +* Remove experimental status from `pipelineColourspace`. + +* Reduce default concurrency when musl thread over-subscription detected. + +* TypeScript: add missing definitions for `OverlayOptions`. + [#4048](https://github.com/lovell/sharp/pull/4048) + [@ike-gg](https://github.com/ike-gg) + +* Install: add advanced option to force use of a globally-installed libvips. + [#4060](https://github.com/lovell/sharp/issues/4060) + +* Expose `bilinear` resizing kernel (and interpolator). + [#4061](https://github.com/lovell/sharp/issues/4061) + +* Ensure `extend` operation stays sequential for multi-page TIFF (regression in 0.32.0). + [#4069](https://github.com/lovell/sharp/issues/4069) + +* Tighten validation of constructor `text` integer properties. + [#4071](https://github.com/lovell/sharp/issues/4071) + +* Simplify internal StaySequential logic. + [#4074](https://github.com/lovell/sharp/pull/4074) + [@kleisauke](https://github.com/kleisauke) + +* Ensure negate operation occurs after profile conversion. + [#4096](https://github.com/lovell/sharp/pull/4096) + [@adriaanmeuris](https://github.com/adriaanmeuris) diff --git a/docs/src/content/docs/changelog/v0.33.5.md b/docs/src/content/docs/changelog/v0.33.5.md new file mode 100644 index 000000000..7fb99baa2 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.33.5.md @@ -0,0 +1,39 @@ +--- +title: v0.33.5 - 16th August 2024 +slug: changelog/v0.33.5 +--- + +* Upgrade to libvips v8.15.3 for upstream bug fixes. + +* Add `pageHeight` and `pages` to response of multi-page output. + [#3411](https://github.com/lovell/sharp/issues/3411) + +* Ensure option to force use of a globally-installed libvips works correctly. + [#4111](https://github.com/lovell/sharp/pull/4111) + [@project0](https://github.com/project0) + +* Minimise use of `engines` property to improve yarn v1 support. + [#4130](https://github.com/lovell/sharp/issues/4130) + +* Ensure `sharp.format.heif` includes only AVIF when using prebuilt binaries. + [#4132](https://github.com/lovell/sharp/issues/4132) + +* Add support to recomb operation for 4x4 matrices. + [#4147](https://github.com/lovell/sharp/pull/4147) + [@ton11797](https://github.com/ton11797) + +* Expose PNG text chunks as `comments` metadata. + [#4157](https://github.com/lovell/sharp/pull/4157) + [@nkeynes](https://github.com/nkeynes) + +* Expose optional `precision` and `minAmplitude` parameters of `blur` operation. + [#4168](https://github.com/lovell/sharp/pull/4168) + [#4172](https://github.com/lovell/sharp/pull/4172) + [@marcosc90](https://github.com/marcosc90) + +* Ensure `keepIccProfile` avoids colour transformation where possible. + [#4186](https://github.com/lovell/sharp/issues/4186) + +* TypeScript: `chromaSubsampling` metadata is optional. + [#4191](https://github.com/lovell/sharp/pull/4191) + [@DavidVaness](https://github.com/DavidVaness) diff --git a/docs/src/content/docs/changelog/v0.34.0.md b/docs/src/content/docs/changelog/v0.34.0.md new file mode 100644 index 000000000..4fc83109e --- /dev/null +++ b/docs/src/content/docs/changelog/v0.34.0.md @@ -0,0 +1,52 @@ +--- +title: v0.34.0 - 4th April 2025 +slug: changelog/v0.34.0 +--- + +* Breaking: Support array of input images to be joined or animated. + [#1580](https://github.com/lovell/sharp/issues/1580) + +* Breaking: Ensure `removeAlpha` removes all alpha channels. + [#2266](https://github.com/lovell/sharp/issues/2266) + +* Breaking: Non-animated GIF output defaults to no-loop instead of loop-forever. + [#3394](https://github.com/lovell/sharp/issues/3394) + +* Breaking: Support `info.size` on wide-character systems via upgrade to C++17. + [#3943](https://github.com/lovell/sharp/issues/3943) + +* Breaking: Ensure `background` metadata can be parsed by `color` package. + [#4090](https://github.com/lovell/sharp/issues/4090) + +* Add `isPalette` and `bitsPerSample` to metadata, deprecate `paletteBitDepth`. + +* Expose WebP `smartDeblock` output option. + +* Prevent use of linux-x64 binaries with v1 microarchitecture. + +* Add `autoOrient` operation and constructor option. + [#4151](https://github.com/lovell/sharp/pull/4151) + [@happycollision](https://github.com/happycollision) + +* TypeScript: Ensure channel counts use the correct range. + [#4197](https://github.com/lovell/sharp/pull/4197) + [@DavidVaness](https://github.com/DavidVaness) + +* Improve support for ppc64le architecture. + [#4203](https://github.com/lovell/sharp/pull/4203) + [@sumitd2](https://github.com/sumitd2) + +* Add `pdfBackground` constructor property. + [#4207](https://github.com/lovell/sharp/pull/4207) + [@calebmer](https://github.com/calebmer) + +* Expose erode and dilate operations. + [#4243](https://github.com/lovell/sharp/pull/4243) + [@qpincon](https://github.com/qpincon) + +* Add support for RGBE images. Requires libvips compiled with radiance support. + [#4316](https://github.com/lovell/sharp/pull/4316) + [@florentzabera](https://github.com/florentzabera) + +* Allow wide-gamut HEIF output at higher bitdepths. + [#4344](https://github.com/lovell/sharp/issues/4344) diff --git a/docs/src/content/docs/changelog/v0.34.1.md b/docs/src/content/docs/changelog/v0.34.1.md new file mode 100644 index 000000000..a5ae94dcf --- /dev/null +++ b/docs/src/content/docs/changelog/v0.34.1.md @@ -0,0 +1,8 @@ +--- +title: v0.34.1 - 7th April 2025 +slug: changelog/v0.34.1 +--- + +* TypeScript: Ensure new `autoOrient` property is optional. + [#4362](https://github.com/lovell/sharp/pull/4362) + [@styfle](https://github.com/styfle) diff --git a/docs/src/content/docs/changelog/v0.34.2.md b/docs/src/content/docs/changelog/v0.34.2.md new file mode 100644 index 000000000..2292304ea --- /dev/null +++ b/docs/src/content/docs/changelog/v0.34.2.md @@ -0,0 +1,28 @@ +--- +title: v0.34.2 - 20th May 2025 +slug: changelog/v0.34.2 +--- + +* Ensure animated GIF to WebP conversion retains loop (regression in 0.34.0). + [#3394](https://github.com/lovell/sharp/issues/3394) + +* Ensure `pdfBackground` constructor property is used. + [#4207](https://github.com/lovell/sharp/pull/4207) + [#4398](https://github.com/lovell/sharp/issues/4398) + +* Add experimental support for prebuilt Windows ARM64 binaries. + [#4375](https://github.com/lovell/sharp/pull/4375) + [@hans00](https://github.com/hans00) + +* Ensure resizing with a `fit` of `contain` supports multiple alpha channels. + [#4382](https://github.com/lovell/sharp/issues/4382) + +* TypeScript: Ensure `metadata` response more closely matches reality. + [#4383](https://github.com/lovell/sharp/issues/4383) + +* TypeScript: Ensure `smartDeblock` property is included in WebP definition. + [#4387](https://github.com/lovell/sharp/pull/4387) + [@Stephen-X](https://github.com/Stephen-X) + +* Ensure support for wide-character filenames on Windows (regression in 0.34.0). + [#4391](https://github.com/lovell/sharp/issues/4391) diff --git a/docs/src/content/docs/changelog/v0.34.3.md b/docs/src/content/docs/changelog/v0.34.3.md new file mode 100644 index 000000000..1f3016741 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.34.3.md @@ -0,0 +1,33 @@ +--- +title: v0.34.3 - 10th July 2025 +slug: changelog/v0.34.3 +--- + +* Upgrade to libvips v8.17.1 for upstream bug fixes. + +* Add "Magic Kernel Sharp" (no relation) to resizing kernels. + +* Deprecate top-level, format-specific constructor parameters, e.g. `subifd` becomes `tiff.subifd`. + +* Expose `stylesheet` and `highBitdepth` SVG input parameters. + +* Expose `keepDuplicateFrames` GIF output parameter. + +* Add support for RAW digital camera image input. Requires libvips compiled with libraw support. + +* Provide XMP metadata as a string, as well as a Buffer, where possible. + +* Add `pageHeight` option to `create` and `raw` input for animated images. + [#3236](https://github.com/lovell/sharp/issues/3236) + +* Expose JPEG 2000 `oneshot` decoder option. + [#4262](https://github.com/lovell/sharp/pull/4262) + [@mbklein](https://github.com/mbklein) + +* Support composite operation with non-sRGB pipeline colourspace. + [#4412](https://github.com/lovell/sharp/pull/4412) + [@kleisauke](https://github.com/kleisauke) + +* Add `keepXmp` and `withXmp` for control over output XMP metadata. + [#4416](https://github.com/lovell/sharp/pull/4416) + [@tpatel](https://github.com/tpatel) diff --git a/docs/src/content/docs/changelog/v0.34.4.md b/docs/src/content/docs/changelog/v0.34.4.md new file mode 100644 index 000000000..83c95ce43 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.34.4.md @@ -0,0 +1,28 @@ +--- +title: v0.34.4 - 17th September 2025 +slug: changelog/v0.34.4 +--- + +* Upgrade to libvips v8.17.2 for upstream bug fixes. + +* Ensure TIFF `subifd` and OpenSlide `level` input options are respected (regression in 0.34.3). + +* Ensure `autoOrient` occurs before non-90 angle rotation. + [#4425](https://github.com/lovell/sharp/issues/4425) + +* Ensure `autoOrient` removes existing metadata after shrink-on-load. + [#4431](https://github.com/lovell/sharp/issues/4431) + +* TypeScript: Ensure `KernelEnum` includes `linear`. + [#4441](https://github.com/lovell/sharp/pull/4441) + [@BayanBennett](https://github.com/BayanBennett) + +* Ensure `unlimited` flag is passed upstream when reading TIFF images. + [#4446](https://github.com/lovell/sharp/issues/4446) + +* Support Electron memory cage when reading XMP metadata (regression in 0.34.3). + [#4451](https://github.com/lovell/sharp/issues/4451) + +* Add sharp-libvips rpath for yarn v5 support. + [#4452](https://github.com/lovell/sharp/pull/4452) + [@arcanis](https://github.com/arcanis) diff --git a/docs/src/content/docs/changelog/v0.34.5.md b/docs/src/content/docs/changelog/v0.34.5.md new file mode 100644 index 000000000..d8321baa4 --- /dev/null +++ b/docs/src/content/docs/changelog/v0.34.5.md @@ -0,0 +1,21 @@ +--- +title: v0.34.5 - 6th November 2025 +slug: changelog/v0.34.5 +--- + +* Upgrade to libvips v8.17.3 for upstream bug fixes. + +* Add experimental support for prebuilt Linux RISC-V 64-bit binaries. + +* Support building from source with npm v12+, deprecate `--build-from-source` flag. + [#4458](https://github.com/lovell/sharp/issues/4458) + +* Add support for BigTIFF output. + [#4459](https://github.com/lovell/sharp/pull/4459) + [@throwbi](https://github.com/throwbi) + +* Improve error messaging when only warnings issued. + [#4465](https://github.com/lovell/sharp/issues/4465) + +* Simplify ICC processing when retaining input profiles. + [#4468](https://github.com/lovell/sharp/issues/4468) diff --git a/docs/README.md b/docs/src/content/docs/index.md similarity index 73% rename from docs/README.md rename to docs/src/content/docs/index.md index 52ae9b316..3ba704f43 100644 --- a/docs/README.md +++ b/docs/src/content/docs/index.md @@ -1,10 +1,16 @@ -# sharp +--- +title: "High performance Node.js image processing" +--- -<img src="https://cdn.jsdelivr.net/gh/lovell/sharp@master/docs/image/sharp-logo.svg" width="160" height="160" alt="sharp logo" align="right"> +<img src="/sharp-logo.svg" width="160" height="160" alt="sharp logo" align="right"> -The typical use case for this high speed Node.js module +The typical use case for this high speed Node-API module is to convert large images in common formats to -smaller, web-friendly JPEG, PNG, AVIF and WebP images of varying dimensions. +smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions. + +It can be used with all JavaScript runtimes +that provide support for Node-API v9, including +Node.js >= 18.17.0, Deno and Bun. Resizing an image is typically 4x-5x faster than using the quickest ImageMagick and GraphicsMagick settings @@ -16,14 +22,18 @@ 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+ +Most modern macOS, Windows and Linux systems do not require any additional install or runtime dependencies. -### Formats +```sh frame="none" +npm install sharp +``` + +## Formats -This module supports reading JPEG, PNG, WebP, AVIF, TIFF, GIF and SVG images. +This module supports reading JPEG, PNG, WebP, GIF, AVIF, TIFF and SVG images. -Output images can be in JPEG, PNG, WebP, AVIF and TIFF formats as well as uncompressed raw pixel data. +Output images can be in JPEG, PNG, WebP, GIF, AVIF and TIFF formats as well as uncompressed raw pixel data. Streams, Buffer objects and the filesystem can be used for input and output. @@ -33,7 +43,7 @@ Deep Zoom image pyramids can be generated, suitable for use with "slippy map" tile viewers like [OpenSeadragon](https://github.com/openseadragon/openseadragon). -### Fast +## Fast This module is powered by the blazingly fast [libvips](https://github.com/libvips/libvips) image processing library, @@ -48,7 +58,7 @@ taking full advantage of multiple CPU cores and L1/L2/L3 cache. Everything remains non-blocking thanks to _libuv_, no child processes are spawned and Promises/async/await are supported. -### Optimal +## Optimal The features of `mozjpeg` and `pngquant` can be used to optimise the file size of JPEG and PNG images respectively, @@ -63,14 +73,18 @@ PNG filtering is disabled by default, which for diagrams and line art often produces the same result as [pngcrush](https://pmt.sourceforge.io/pngcrush/). -### Contributing +The file size of animated GIF output is optimised +without having to use separate command line tools such as +[gifsicle](https://www.lcdf.org/gifsicle/). + +## Contributing -A [guide for contributors](https://github.com/lovell/sharp/blob/master/.github/CONTRIBUTING.md) +A [guide for contributors](https://github.com/lovell/sharp/blob/main/.github/CONTRIBUTING.md) covers reporting bugs, requesting features and submitting code changes. -### Licensing +## Licensing -Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors. +Copyright 2013 Lovell Fuller and others. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/docs/src/content/docs/install.md b/docs/src/content/docs/install.md new file mode 100644 index 000000000..083db5ccd --- /dev/null +++ b/docs/src/content/docs/install.md @@ -0,0 +1,361 @@ +--- +title: Installation +--- + +Works with your choice of JavaScript package manager. + +:::caution +Please ensure your package manager is configured to install optional dependencies +::: + +If a package manager lockfile must support multiple platforms, +please see the [cross-platform](#cross-platform) section +to help decide which package manager is appropriate. + +```sh frame="none" +npm install sharp +``` + +```sh frame="none" +pnpm add sharp +``` + +When using `pnpm`, add `sharp` to +[ignoredBuiltDependencies](https://pnpm.io/settings#ignoredbuiltdependencies) +to silence warnings. + +```sh frame="none" +yarn add sharp +``` + +```sh frame="none" +bun add sharp +``` + +```sh frame="none" +deno add --quiet npm:sharp +deno run --allow-env --allow-ffi --allow-read --allow-sys ... +``` + +## Prerequisites + +* Node-API v9 compatible runtime e.g. Node.js ^18.17.0 or >=20.3.0. + +## Prebuilt binaries + +Ready-compiled sharp and libvips binaries are provided for use on the most common platforms: + +* macOS x64 (>= 10.15) +* macOS ARM64 +* Linux ARM (glibc >= 2.31) +* Linux ARM64 (glibc >= 2.26, musl >= 1.2.2) +* Linux RISC-V 64-bit (glibc >= 2.41) +* Linux ppc64 (glibc >= 2.36) +* Linux s390x (glibc >= 2.36) +* Linux x64 (glibc >= 2.26, musl >= 1.2.2, CPU with SSE4.2) +* Windows x64 +* Windows x86 +* Windows ARM64 (experimental, CPU with ARMv8.4 required for all features) + +This provides support for the +JPEG, PNG, WebP, AVIF (limited to 8-bit depth), TIFF, GIF and SVG (input) image formats. + +## Cross-platform + +At install time, package managers will automatically select prebuilt binaries +for the current OS platform and CPU architecture, where available. + +Some package managers support multiple platforms and architectures +within the same installation tree and/or using the same lockfile. + +### npm v10+ + +:::caution +npm `package-lock.json` files shared by multiple platforms can cause installation problems due to [npm bug #4828](https://github.com/npm/cli/issues/4828) +::: + +Provides limited support via `--os`, `--cpu` and `--libc` flags. + +To support macOS with Intel x64 and ARM64 CPUs: +```sh frame="none" +npm install --cpu=x64 --os=darwin sharp +npm install --cpu=arm64 --os=darwin sharp +``` + +When the cross-target is Linux, the C standard library must be specified. + +To support glibc (e.g. Debian) and musl (e.g. Alpine) Linux with Intel x64 CPUs: +```sh frame="none" +npm install --cpu=x64 --os=linux --libc=glibc sharp +npm install --cpu=x64 --os=linux --libc=musl sharp +``` + +### yarn v3+ + +Use the [supportedArchitectures](https://yarnpkg.com/configuration/yarnrc#supportedArchitectures) configuration. + +### pnpm v8+ + +Use the [supportedArchitectures](https://pnpm.io/settings#supportedarchitectures) configuration. + +## Custom libvips + +To use a custom, globally-installed version of libvips instead of the provided binaries, +make sure it is at least the version listed under `config.libvips` in the `package.json` file +and that it can be located using `pkg-config --modversion vips-cpp`. + +For help compiling libvips and its dependencies, please see +[building libvips from source](https://www.libvips.org/install.html#building-libvips-from-source). + +The use of a globally-installed libvips is unsupported on Windows +and on macOS when running Node.js under Rosetta. + +## Building from source + +This module will be compiled from source when: + +* a globally-installed libvips is detected, or +* using `npm explore sharp -- npm run build`, or +* using the deprecated `npm run --build-from-source` at `npm install` time. + +The logic to detect a globally-installed libvips can be skipped by setting the +`SHARP_IGNORE_GLOBAL_LIBVIPS` (never try to use it) or +`SHARP_FORCE_GLOBAL_LIBVIPS` (always try to use it, even when missing or outdated) +environment variables. + +Building from source requires: + +* C++17 compiler +* [node-addon-api](https://www.npmjs.com/package/node-addon-api) version 7+ +* [node-gyp](https://github.com/nodejs/node-gyp#installation) version 9+ and its dependencies + +There is an install-time check for these dependencies. +If `node-addon-api` or `node-gyp` cannot be found, try adding them via: + +```sh frame="none" +npm install --save node-addon-api node-gyp +``` + +When using `pnpm`, you may need to add `sharp` to +[onlyBuiltDependencies](https://pnpm.io/settings#onlybuiltdependencies) +to ensure the installation script can be run. + +For cross-compiling, the `--platform`, `--arch` and `--libc` npm flags +(or the `npm_config_platform`, `npm_config_arch` and `npm_config_libc` environment variables) +can be used to configure the target environment. + +## WebAssembly + +Experimental support is provided for runtime environments that provide +multi-threaded Wasm via Workers. + +Use in web browsers is unsupported. + +Native text rendering is unsupported. + +[Tile-based output](/api-output#tile) is unsupported. + +```sh frame="none" +npm install --cpu=wasm32 sharp +``` + +## FreeBSD + +The `vips` package must be installed before `npm install` is run, +as well as the additional [building from source](#building-from-source) dependencies. + +```sh frame="none" +pkg install -y pkgconf vips +``` + +```sh frame="none" +cd /usr/ports/graphics/vips/ && make install clean +``` + +## Linux memory allocator + +The default memory allocator on most glibc-based Linux systems +(e.g. Debian, Red Hat) is unsuitable for long-running, multi-threaded +processes that involve lots of small memory allocations. + +For this reason, by default, sharp will limit the use of thread-based +[concurrency](/api-utility#concurrency) when the glibc allocator is +detected at runtime. + +To help avoid fragmentation and improve performance on these systems, +the use of an alternative memory allocator such as +[jemalloc](https://github.com/jemalloc/jemalloc) is recommended. + +Those using musl-based Linux (e.g. Alpine) and non-Linux systems are +unaffected. + +## AWS Lambda + +The `node_modules` directory of the +[deployment package](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html) +must include binaries for either the linux-x64 or linux-arm64 platforms +depending on the chosen architecture. + +When building your deployment package on a machine that differs from the target architecture, +see the [cross-platform](#cross-platform) section to help decide which package manager is appropriate +and how to configure it. + +Some package managers use symbolic links +but AWS Lambda does not support these within deployment packages. + +To get the best performance select the largest memory available. +A 1536 MB function provides ~12x more CPU time than a 128 MB function. + +When integrating with AWS API Gateway, ensure it is configured with the relevant +[binary media types](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html). + +## Bundlers + +### webpack + +Ensure sharp is excluded from bundling via the +[externals](https://webpack.js.org/configuration/externals/) +configuration. + +```js frame="none" +externals: { + 'sharp': 'commonjs sharp' +} +``` + +### esbuild + +Ensure sharp is excluded from bundling via the +[external](https://esbuild.github.io/api/#external) +configuration. + +```js frame="none" +buildSync({ + entryPoints: ['app.js'], + bundle: true, + platform: 'node', + external: ['sharp'], +}) +``` + +```sh frame="none" +esbuild app.js --bundle --platform=node --external:sharp +``` + +For `serverless-esbuild`, ensure platform-specific binaries are installed +via the `serverless.yml` configuration. + +```yaml frame="none" +custom: + esbuild: + external: + - sharp + packagerOptions: + scripts: + - npm install --os=linux --cpu=x64 sharp +``` + +### electron + +#### electron-builder + +Ensure `sharp` is unpacked from the ASAR archive file using the +[asarUnpack](https://www.electron.build/app-builder-lib.interface.platformspecificbuildoptions#asarunpack) +option. + +```json frame="none" +{ + "build": { + "asar": true, + "asarUnpack": [ + "**/node_modules/sharp/**/*", + "**/node_modules/@img/**/*" + ] + } +} +``` + +#### electron-forge + +Ensure `sharp` is unpacked from the ASAR archive file using the +[unpack](https://js.electronforge.io/interfaces/_electron_forge_maker_squirrel.InternalOptions.Options.html#asar) +option. + +```json frame="none" +{ + "packagerConfig": { + "asar": { + "unpack": "**/node_modules/{sharp,@img}/**/*" + } + } +} +``` + +When using `electron-forge` with [Webpack](#webpack), +you may also need to add +[forge-externals-plugin](https://www.npmjs.com/package/@timfish/forge-externals-plugin). + +### vite + +Ensure `sharp` is excluded from bundling via the +[build.rollupOptions](https://vitejs.dev/config/build-options.html) +configuration. + +```js frame="none" +import { defineConfig } from 'vite'; + +export default defineConfig({ + build: { + rollupOptions: { + external: [ + "sharp" + ] + } + } +}); +``` + +## TypeScript + +TypeScript definitions are published as part of +the `sharp` package from v0.32.0. + +Previously these were available via the `@types/sharp` package, +which is now deprecated. + +When using Typescript, please ensure `devDependencies` includes +the `@types/node` package. + +## Fonts + +When creating text images or rendering SVG images that contain text elements, +`fontconfig` is used to find the relevant fonts. + +On Windows and macOS systems, all system fonts are available for use. + +On macOS systems using Homebrew, you may need to set the +`PANGOCAIRO_BACKEND` environment variable to a value of `fontconfig` +to ensure it is used for font discovery instead of Core Text. + +On Linux systems, fonts that include the relevant +[`fontconfig` configuration](https://www.freedesktop.org/software/fontconfig/fontconfig-user.html) +when installed via package manager are available for use. + +If `fontconfig` configuration is not found, the following error will occur: +``` +Fontconfig error: Cannot load default config file +``` + +In serverless environments where there is no control over font packages, +use the `FONTCONFIG_PATH` environment variable to point to a custom location. + +Embedded SVG fonts are unsupported. + +## Known conflicts + +### Canvas and Windows + +If both `canvas` and `sharp` modules are used in the same Windows process, the following error may occur: +``` +The specified procedure could not be found. +``` diff --git a/docs/src/content/docs/performance.md b/docs/src/content/docs/performance.md new file mode 100644 index 000000000..19aead63d --- /dev/null +++ b/docs/src/content/docs/performance.md @@ -0,0 +1,138 @@ +--- +title: Performance +--- + +## Parallelism and concurrency + +Node.js uses a libuv-managed thread pool when processing asynchronous calls to native modules such as sharp. + +The maximum number of images that sharp can process in parallel is controlled by libuv's +[`UV_THREADPOOL_SIZE`](https://nodejs.org/api/cli.html#uv_threadpool_sizesize) +environment variable, which defaults to 4. + +When using more than 4 physical CPU cores, set this environment variable +before the Node.js process starts to increase the thread pool size. + +```sh frame="none" +export UV_THREADPOOL_SIZE="$(lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l)" +``` + +libvips uses a shared thread pool to avoid the overhead of spawning new threads. +The size of this thread pool will grow on demand and shrink when idle. + +The default number of threads used to concurrently process each image is the same as the number of CPU cores, +except when using glibc-based Linux without jemalloc, where the default is `1` to help reduce memory fragmentation. + +Use [`sharp.concurrency()`](/api-utility/#concurrency) to manage the number of threads per image. + +To reduce memory fragmentation when using the default Linux glibc memory allocator, set the +[`MALLOC_ARENA_MAX`](https://sourceware.org/glibc/manual/latest/html_node/Memory-Allocation-Tunables.html) +environment variable before the Node.js process starts to reduce the number of memory pools. + +```sh frame="none" +export MALLOC_ARENA_MAX="2" +``` + +## Benchmark + +A test to benchmark the performance of this module relative to alternatives. + +Greater libvips performance can be expected with caching enabled (default) +and using 8+ core machines, especially those with larger L1/L2 CPU caches. + +The I/O limits of the relevant (de)compression library will generally determine maximum throughput. + +### Contenders + +- [jimp](https://www.npmjs.com/package/jimp) v1.6.0 - Image processing in pure JavaScript. +- [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.25.1 - Fully featured wrapper around GraphicsMagick's `gm` command line utility, but "_has been sunset_". +- sharp v0.34.3 / libvips v8.17.0 - Caching within libvips disabled to ensure a fair comparison. + +### Environment + +#### AMD64 + +- AWS EC2 us-west-2 [c7a.xlarge](https://aws.amazon.com/ec2/instance-types/c7a/) (4x AMD EPYC 9R14) +- Ubuntu 25.04 +- Node.js 24.3.0 + +#### ARM64 + +- AWS EC2 us-west-2 [c8g.xlarge](https://aws.amazon.com/ec2/instance-types/c8g/) (4x ARM Graviton4) +- Ubuntu 25.04 +- Node.js 24.3.0 + +### Task: JPEG + +Decompress a 2725x2225 JPEG image, +resize to 720x588 using Lanczos 3 resampling (where available), +then compress to JPEG at a "quality" setting of 80. + +Note: jimp does not support Lanczos 3, bicubic resampling used instead. + +#### Results: JPEG (AMD64) + +| Package | I/O | Ops/sec | Speed-up | +| :---------- | :----- | ------: | -------: | +| jimp | buffer | 2.40 | 1.0 | +| jimp | file | 2.60 | 1.1 | +| imagemagick | file | 9.70 | 4.0 | +| gm | buffer | 11.60 | 4.8 | +| gm | file | 11.72 | 4.9 | +| sharp | stream | 59.40 | 24.8 | +| sharp | file | 62.67 | 26.1 | +| sharp | buffer | 64.42 | 26.8 | + +#### Results: JPEG (ARM64) + +| Package | I/O | Ops/sec | Speed-up | +| :---------- | :----- | ------: | -------: | +| jimp | buffer | 2.24 | 1.0 | +| jimp | file | 2.47 | 1.1 | +| imagemagick | file | 10.42 | 4.7 | +| gm | buffer | 12.80 | 5.7 | +| gm | file | 12.88 | 5.7 | +| sharp | stream | 45.58 | 20.3 | +| sharp | file | 47.99 | 21.4 | +| sharp | buffer | 49.20 | 22.0 | + +### Task: PNG + +Decompress a 2048x1536 RGBA PNG image, +premultiply the alpha channel, +resize to 720x540 using Lanczos 3 resampling (where available), +unpremultiply then compress as PNG with a "default" zlib compression level of 6 +and without adaptive filtering. + +Note: jimp does not support premultiply/unpremultiply. + +#### Results: PNG (AMD64) + +| Package | I/O | Ops/sec | Speed-up | +| :---------- | :----- | ------: | -------: | +| imagemagick | file | 6.06 | 1.0 | +| gm | file | 8.44 | 1.4 | +| jimp | buffer | 10.98 | 1.8 | +| sharp | file | 28.26 | 4.7 | +| sharp | buffer | 28.70 | 4.7 | + +#### Results: PNG (ARM64) + +| Package | I/O | Ops/sec | Speed-up | +| :---------- | :----- | ------: | -------: | +| imagemagick | file | 7.09 | 1.0 | +| gm | file | 8.93 | 1.3 | +| jimp | buffer | 10.28 | 1.5 | +| sharp | file | 23.81 | 3.4 | +| sharp | buffer | 24.19 | 3.4 | + +## Running the benchmark test + +Requires Docker. + +```sh frame="none" +git clone https://github.com/lovell/sharp.git +cd sharp/test/bench +./run-with-docker.sh +``` diff --git a/docs/src/styles/custom.css b/docs/src/styles/custom.css new file mode 100644 index 000000000..197ccf567 --- /dev/null +++ b/docs/src/styles/custom.css @@ -0,0 +1,45 @@ +@view-transition { + navigation: auto; +} + +:root { + --sl-content-width: 60rem; + --sl-color-accent-low: #072d00; + --sl-color-accent: #247f00; + --sl-color-accent-high: #aad7a0; + --sl-color-white: #ffffff; + --sl-color-gray-1: #eaf0e8; + --sl-color-gray-2: #c5cdc3; + --sl-color-gray-3: #99a796; + --sl-color-gray-4: #4f5c4d; + --sl-color-gray-5: #303c2d; + --sl-color-gray-6: #1f2a1c; + --sl-color-black: #151a13; +} + +:root[data-theme="light"] { + --sl-color-accent-low: #c0e2b8; + --sl-color-accent: #165800; + --sl-color-accent-high: #0d3e00; + --sl-color-white: #151a13; + --sl-color-gray-1: #1f2a1c; + --sl-color-gray-2: #303c2d; + --sl-color-gray-3: #4f5c4d; + --sl-color-gray-4: #82907f; + --sl-color-gray-5: #bdc4bb; + --sl-color-gray-6: #eaf0e8; + --sl-color-gray-7: #f4f7f3; + --sl-color-black: #ffffff; +} + +blockquote { + background-color: var(--sl-color-gray-6); + padding: 1rem; +} + +.site-title::after { + content: "High performance Node.js image processing"; + color: var(--sl-color-text); + font-size: var(--sl-text-sm); + padding-top: 0.3rem; +} diff --git a/docs/tsconfig.json b/docs/tsconfig.json new file mode 100644 index 000000000..8bf91d3bb --- /dev/null +++ b/docs/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "astro/tsconfigs/strict", + "include": [".astro/types.d.ts", "**/*"], + "exclude": ["dist"] +} diff --git a/install/build.js b/install/build.js new file mode 100644 index 000000000..2ca224586 --- /dev/null +++ b/install/build.js @@ -0,0 +1,38 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { + useGlobalLibvips, + globalLibvipsVersion, + log, + spawnRebuild, +} = require('../lib/libvips'); + +log('Attempting to build from source via node-gyp'); +log('See https://sharp.pixelplumbing.com/install#building-from-source'); + +try { + const addonApi = require('node-addon-api'); + log(`Found node-addon-api ${addonApi.version || ''}`); +} catch (_err) { + log('Please add node-addon-api to your dependencies'); + process.exit(1); +} +try { + const gyp = require('node-gyp'); + log(`Found node-gyp ${gyp().version}`); +} catch (_err) { + log('Please add node-gyp to your dependencies'); + process.exit(1); +} + +if (useGlobalLibvips(log)) { + log(`Detected globally-installed libvips v${globalLibvipsVersion()}`); +} + +const status = spawnRebuild(); +if (status !== 0) { + process.exit(status); +} diff --git a/install/check.js b/install/check.js new file mode 100644 index 000000000..1cfb7d32e --- /dev/null +++ b/install/check.js @@ -0,0 +1,14 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +try { + const { useGlobalLibvips } = require('../lib/libvips'); + if (useGlobalLibvips() || process.env.npm_config_build_from_source) { + process.exit(1); + } +} catch (err) { + const summary = err.message.split(/\n/).slice(0, 1); + console.log(`sharp: skipping install check: ${summary}`); +} diff --git a/install/dll-copy.js b/install/dll-copy.js deleted file mode 100644 index 41449ddd2..000000000 --- a/install/dll-copy.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -const libvips = require('../lib/libvips'); - -const minimumLibvipsVersion = libvips.minimumLibvipsVersion; - -const platform = process.env.npm_config_platform || process.platform; -if (platform === 'win32') { - const buildDir = path.join(__dirname, '..', 'build'); - const buildReleaseDir = path.join(buildDir, 'Release'); - libvips.log(`Creating ${buildReleaseDir}`); - try { - libvips.mkdirSync(buildDir); - libvips.mkdirSync(buildReleaseDir); - } catch (err) {} - const vendorLibDir = path.join(__dirname, '..', 'vendor', minimumLibvipsVersion, 'lib'); - libvips.log(`Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`); - try { - fs - .readdirSync(vendorLibDir) - .filter(function (filename) { - return /\.dll$/.test(filename); - }) - .forEach(function (filename) { - fs.copyFileSync( - path.join(vendorLibDir, filename), - path.join(buildReleaseDir, filename) - ); - }); - } catch (err) { - libvips.log(err); - process.exit(1); - } -} diff --git a/install/libvips.js b/install/libvips.js deleted file mode 100644 index 35f68fd10..000000000 --- a/install/libvips.js +++ /dev/null @@ -1,168 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const os = require('os'); -const path = require('path'); -const stream = require('stream'); -const zlib = require('zlib'); - -const detectLibc = require('detect-libc'); -const semver = require('semver'); -const simpleGet = require('simple-get'); -const tarFs = require('tar-fs'); - -const agent = require('../lib/agent'); -const libvips = require('../lib/libvips'); -const platform = require('../lib/platform'); - -const minimumGlibcVersionByArch = { - arm: '2.28', - arm64: '2.29', - x64: '2.17' -}; - -const hasSharpPrebuild = [ - 'darwin-x64', - 'linux-arm64', - 'linux-x64', - 'linuxmusl-x64', - 'linuxmusl-arm64', - 'win32-ia32', - 'win32-x64' -]; - -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) { - libvips.log(err); - if (err.code === 'EACCES') { - libvips.log('Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag'); - } - libvips.log('Attempting to build from source via node-gyp but this may fail due to the above error'); - libvips.log('Please see https://sharp.pixelplumbing.com/install for required dependencies'); - process.exit(1); -}; - -const extractTarball = function (tarPath, platformAndArch) { - const vendorPath = path.join(__dirname, '..', 'vendor'); - libvips.mkdirSync(vendorPath); - const versionedVendorPath = path.join(vendorPath, minimumLibvipsVersion); - libvips.mkdirSync(versionedVendorPath); - - const ignoreVendorInclude = hasSharpPrebuild.includes(platformAndArch) && !process.env.npm_config_build_from_source; - const ignore = function (name) { - return ignoreVendorInclude && name.includes('include/'); - }; - - stream.pipeline( - fs.createReadStream(tarPath), - supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(), - tarFs.extract(versionedVendorPath, { ignore }), - function (err) { - if (err) { - if (/unexpected end of file/.test(err.message)) { - fail(new Error(`Please delete ${tarPath} as it is not a valid tarball`)); - } - fail(err); - } - } - ); -}; - -try { - const useGlobalLibvips = libvips.useGlobalLibvips(); - - if (useGlobalLibvips) { - const globalLibvipsVersion = libvips.globalLibvipsVersion(); - libvips.log(`Detected globally-installed libvips v${globalLibvipsVersion}`); - libvips.log('Building from source via node-gyp'); - process.exit(1); - } else if (libvips.hasVendoredLibvips()) { - libvips.log(`Using existing vendored libvips v${minimumLibvipsVersion}`); - } else { - // Is this arch/platform supported? - const arch = process.env.npm_config_arch || process.arch; - const platformAndArch = platform(); - 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}`); - } - if (detectLibc.family === detectLibc.GLIBC && detectLibc.version) { - if (semver.lt(`${detectLibc.version}.0`, `${minimumGlibcVersionByArch[arch]}.0`)) { - throw new Error(`Use with glibc ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`); - } - } - if (detectLibc.family === detectLibc.MUSL && detectLibc.version) { - if (semver.lt(detectLibc.version, '1.1.24')) { - 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.' + extension; - const tarPathCache = path.join(libvips.cachePath(), tarFilename); - if (fs.existsSync(tarPathCache)) { - libvips.log(`Using cached ${tarPathCache}`); - extractTarball(tarPathCache, platformAndArch); - } else { - const url = distBaseUrl + tarFilename; - libvips.log(`Downloading ${url}`); - simpleGet({ url: url, agent: agent() }, function (err, response) { - if (err) { - fail(err); - } else if (response.statusCode === 404) { - fail(new Error(`Prebuilt libvips ${minimumLibvipsVersion} binaries are not yet available for ${platformAndArch}`)); - } else if (response.statusCode !== 200) { - fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`)); - } else { - const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`); - const tmpFileStream = fs.createWriteStream(tarPathTemp); - response - .on('error', function (err) { - tmpFileStream.destroy(err); - }) - .on('close', function () { - if (!response.complete) { - tmpFileStream.destroy(new Error('Download incomplete (connection was terminated)')); - } - }) - .pipe(tmpFileStream); - tmpFileStream - .on('error', function (err) { - // Clean up temporary file - fs.unlinkSync(tarPathTemp); - fail(err); - }) - .on('close', function () { - try { - // Attempt to rename - fs.renameSync(tarPathTemp, tarPathCache); - } catch (err) { - // Fall back to copy and unlink - fs.copyFileSync(tarPathTemp, tarPathCache); - fs.unlinkSync(tarPathTemp); - } - extractTarball(tarPathCache); - }); - } - }); - } - } -} catch (err) { - fail(err); -} diff --git a/lib/agent.js b/lib/agent.js deleted file mode 100644 index 7ddb8fca6..000000000 --- a/lib/agent.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; - -const url = require('url'); -const tunnelAgent = require('tunnel-agent'); - -const is = require('./is'); - -const proxies = [ - 'HTTPS_PROXY', - 'https_proxy', - 'HTTP_PROXY', - 'http_proxy', - 'npm_config_https_proxy', - 'npm_config_proxy' -]; - -function env (key) { - return process.env[key]; -} - -module.exports = function () { - try { - const proxy = new url.URL(proxies.map(env).find(is.string)); - const tunnel = proxy.protocol === 'https:' - ? tunnelAgent.httpsOverHttps - : tunnelAgent.httpsOverHttp; - const proxyAuth = proxy.username && proxy.password - ? `${proxy.username}:${proxy.password}` - : null; - return tunnel({ - proxy: { - port: Number(proxy.port), - host: proxy.hostname, - proxyAuth - } - }); - } catch (err) { - return null; - } -}; diff --git a/lib/channel.js b/lib/channel.js index 1ca2fec20..3c6c0b439 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -1,4 +1,7 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const is = require('./is'); @@ -13,7 +16,9 @@ const bool = { }; /** - * Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel. + * Remove alpha channels, if any. This is a no-op if the image does not have an alpha channel. + * + * See also {@link /api-operation/#flatten flatten}. * * @example * sharp('rgba.png') @@ -30,35 +35,59 @@ function removeAlpha () { } /** - * Ensure alpha channel, if missing. The added alpha channel will be fully opaque. This is a no-op if the image already has an alpha channel. + * Ensure the output image has an alpha transparency channel. + * If missing, the added alpha channel will have the specified + * transparency level, defaulting to fully-opaque (1). + * This is a no-op if the image already has an alpha channel. * * @since 0.21.2 * * @example - * sharp('rgb.jpg') + * // rgba.png will be a 4 channel image with a fully-opaque alpha channel + * await sharp('rgb.jpg') * .ensureAlpha() - * .toFile('rgba.png', function(err, info) { - * // rgba.png is a 4 channel image with a fully opaque alpha channel - * }); + * .toFile('rgba.png') * + * @example + * // rgba is a 4 channel image with a fully-transparent alpha channel + * const rgba = await sharp(rgb) + * .ensureAlpha(0) + * .toBuffer(); + * + * @param {number} [alpha=1] - alpha transparency level (0=fully-transparent, 1=fully-opaque) * @returns {Sharp} + * @throws {Error} Invalid alpha transparency level */ -function ensureAlpha () { - this.options.ensureAlpha = true; +function ensureAlpha (alpha) { + if (is.defined(alpha)) { + if (is.number(alpha) && is.inRange(alpha, 0, 1)) { + this.options.ensureAlpha = alpha; + } else { + throw is.invalidParameterError('alpha', 'number between 0 and 1', alpha); + } + } else { + this.options.ensureAlpha = 1; + } return this; } /** * Extract a single channel from a multi-channel image. * + * The output colourspace will be either `b-w` (8-bit) or `grey16` (16-bit). + * * @example - * sharp(input) + * // green.jpg is a greyscale image containing the green channel of the input + * await sharp(input) * .extractChannel('green') - * .toColourspace('b-w') - * .toFile('green.jpg', function(err, info) { - * // info.channels === 1 - * // green.jpg is a greyscale image containing the green channel of the input - * }); + * .toFile('green.jpg'); + * + * @example + * // red1 is the red value of the first pixel, red2 the second pixel etc. + * const [red1, red2, ...] = await sharp(input) + * .extractChannel(0) + * .raw() + * .toBuffer(); * * @param {number|string} channel - zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`. * @returns {Sharp} @@ -131,9 +160,10 @@ function bandbool (boolOp) { /** * Decorate the Sharp prototype with channel-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Object.assign(Sharp.prototype, { // Public instance functions removeAlpha, diff --git a/lib/colour.js b/lib/colour.js index c4516c5a1..e61c248a8 100644 --- a/lib/colour.js +++ b/lib/colour.js @@ -1,6 +1,9 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const color = require('color'); +const color = require('@img/colour'); const is = require('./is'); /** @@ -16,27 +19,34 @@ const colourspace = { }; /** - * Tint the image using the provided chroma while preserving the image luminance. + * Tint the image using the provided colour. * An alpha channel may be present and will be unchanged by the operation. * - * @param {string|Object} rgb - parsed by the [color](https://www.npmjs.org/package/color) module to extract chroma values. + * @example + * const output = await sharp(input) + * .tint({ r: 255, g: 240, b: 16 }) + * .toBuffer(); + * + * @param {string|Object} tint - Parsed by the [color](https://www.npmjs.org/package/color) module. * @returns {Sharp} * @throws {Error} Invalid parameter */ -function tint (rgb) { - const colour = color(rgb); - this.options.tintA = colour.a(); - this.options.tintB = colour.b(); +function tint (tint) { + this._setBackgroundColourOption('tint', tint); return this; } /** * Convert to 8-bit greyscale; 256 shades of grey. * This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use `gamma()` with `greyscale()` for the best results. - * By default the output image will be web-friendly sRGB and contain three (identical) color channels. + * By default the output image will be web-friendly sRGB and contain three (identical) colour channels. * This may be overridden by other sharp operations such as `toColourspace('b-w')`, - * which will produce an output image containing one color channel. + * which will produce an output image containing one colour channel. * An alpha channel may be present, and will be unchanged by the operation. + * + * @example + * const output = await sharp(input).greyscale().toBuffer(); + * * @param {Boolean} [greyscale=true] * @returns {Sharp} */ @@ -54,6 +64,44 @@ function grayscale (grayscale) { return this.greyscale(grayscale); } +/** + * Set the pipeline colourspace. + * + * The input image will be converted to the provided colourspace at the start of the pipeline. + * All operations will use this colourspace before converting to the output colourspace, + * as defined by {@link #tocolourspace toColourspace}. + * + * @since 0.29.0 + * + * @example + * // Run pipeline in 16 bits per channel RGB while converting final result to 8 bits per channel sRGB. + * await sharp(input) + * .pipelineColourspace('rgb16') + * .toColourspace('srgb') + * .toFile('16bpc-pipeline-to-8bpc-output.png') + * + * @param {string} [colourspace] - pipeline colourspace e.g. `rgb16`, `scrgb`, `lab`, `grey16` [...](https://www.libvips.org/API/current/enum.Interpretation.html) + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function pipelineColourspace (colourspace) { + if (!is.string(colourspace)) { + throw is.invalidParameterError('colourspace', 'string', colourspace); + } + this.options.colourspacePipeline = colourspace; + return this; +} + +/** + * Alternative spelling of `pipelineColourspace`. + * @param {string} [colorspace] - pipeline colorspace. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function pipelineColorspace (colorspace) { + return this.pipelineColourspace(colorspace); +} + /** * Set the output colourspace. * By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. @@ -64,7 +112,7 @@ function grayscale (grayscale) { * .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) + * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html) * @returns {Sharp} * @throws {Error} Invalid parameters */ @@ -86,6 +134,29 @@ function toColorspace (colorspace) { return this.toColourspace(colorspace); } +/** + * Create a RGBA colour array from a given value. + * @private + * @param {string|Object} value + * @throws {Error} Invalid value + */ +function _getBackgroundColourOption (value) { + if ( + is.object(value) || + (is.string(value) && value.length >= 3 && value.length <= 200) + ) { + const colour = color(value); + return [ + colour.red(), + colour.green(), + colour.blue(), + Math.round(colour.alpha() * 255) + ]; + } else { + throw is.invalidParameterError('background', 'object or string', value); + } +} + /** * Update a colour attribute of the this.options Object. * @private @@ -95,33 +166,27 @@ function toColorspace (colorspace) { */ function _setBackgroundColourOption (key, value) { if (is.defined(value)) { - if (is.object(value) || is.string(value)) { - const colour = color(value); - this.options[key] = [ - colour.red(), - colour.green(), - colour.blue(), - Math.round(colour.alpha() * 255) - ]; - } else { - throw is.invalidParameterError('background', 'object or string', value); - } + this.options[key] = _getBackgroundColourOption(value); } } /** * Decorate the Sharp prototype with colour-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Object.assign(Sharp.prototype, { // Public tint, greyscale, grayscale, + pipelineColourspace, + pipelineColorspace, toColourspace, toColorspace, // Private + _getBackgroundColourOption, _setBackgroundColourOption }); // Class attributes diff --git a/lib/composite.js b/lib/composite.js index 88ab83cbf..1c3e5e629 100644 --- a/lib/composite.js +++ b/lib/composite.js @@ -1,4 +1,7 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const is = require('./is'); @@ -43,6 +46,9 @@ const blend = { * The images to composite must be the same size or smaller than the processed image. * If both `top` and `left` options are provided, they take precedence over `gravity`. * + * Other operations in the same processing pipeline (e.g. resize, rotate, flip, + * flop, extract) will always be applied to the input image before composition. + * * The `blend` option can be one of `clear`, `source`, `over`, `in`, `out`, `atop`, * `dest`, `dest-over`, `dest-in`, `dest-out`, `dest-atop`, * `xor`, `add`, `saturate`, `multiply`, `screen`, `overlay`, `darken`, `lighten`, @@ -50,12 +56,27 @@ const blend = { * `hard-light`, `soft-light`, `difference`, `exclusion`. * * More information about blend modes can be found at - * https://libvips.github.io/libvips/API/current/libvips-conversion.html#VipsBlendMode + * https://www.libvips.org/API/current/enum.BlendMode.html * and https://www.cairographics.org/operators/ * * @since 0.22.0 * * @example + * await sharp(background) + * .composite([ + * { input: layer1, gravity: 'northwest' }, + * { input: layer2, gravity: 'southeast' }, + * ]) + * .toFile('combined.png'); + * + * @example + * const output = await sharp('input.gif', { animated: true }) + * .composite([ + * { input: 'overlay.png', tile: true, blend: 'saturate' } + * ]) + * .toBuffer(); + * + * @example * sharp('input.png') * .rotate(180) * .resize(300) @@ -78,17 +99,32 @@ const blend = { * @param {Number} [images[].input.create.height] * @param {Number} [images[].input.create.channels] - 3-4 * @param {String|Object} [images[].input.create.background] - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. + * @param {Object} [images[].input.text] - describes a new text image to be created. + * @param {string} [images[].input.text.text] - text to render as a UTF-8 string. It can contain Pango markup, for example `<i>Le</i>Monde`. + * @param {string} [images[].input.text.font] - font name to render with. + * @param {string} [images[].input.text.fontfile] - absolute filesystem path to a font file that can be used by `font`. + * @param {number} [images[].input.text.width=0] - integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. + * @param {number} [images[].input.text.height=0] - integral number of pixels high. When defined, `dpi` will be ignored and the text will automatically fit the pixel resolution defined by `width` and `height`. Will be ignored if `width` is not specified or set to 0. + * @param {string} [images[].input.text.align='left'] - text alignment (`'left'`, `'centre'`, `'center'`, `'right'`). + * @param {boolean} [images[].input.text.justify=false] - set this to true to apply justification to the text. + * @param {number} [images[].input.text.dpi=72] - the resolution (size) at which to render the text. Does not take effect if `height` is specified. + * @param {boolean} [images[].input.text.rgba=false] - set this to true to enable RGBA output. This is useful for colour emoji rendering, or support for Pango markup features like `<span foreground="red">Red!</span>`. + * @param {number} [images[].input.text.spacing=0] - text line height in points. Will use the font line height if none is specified. + * @param {Boolean} [images[].autoOrient=false] - set to true to use EXIF orientation data, if present, to orient the image. * @param {String} [images[].blend='over'] - how to blend this image with the image below. * @param {String} [images[].gravity='centre'] - gravity at which to place the overlay. * @param {Number} [images[].top] - the pixel offset from the top edge. * @param {Number} [images[].left] - the pixel offset from the left edge. * @param {Boolean} [images[].tile=false] - set to true to repeat the overlay image across the entire image with the given `gravity`. - * @param {Boolean} [images[].premultiplied=false] - set to true to avoid premultipling the image below. Equivalent to the `--premultiplied` vips option. + * @param {Boolean} [images[].premultiplied=false] - set to true to avoid premultiplying the image below. Equivalent to the `--premultiplied` vips option. * @param {Number} [images[].density=72] - number representing the DPI for vector overlay image. * @param {Object} [images[].raw] - describes overlay when using raw pixel data. * @param {Number} [images[].raw.width] * @param {Number} [images[].raw.height] * @param {Number} [images[].raw.channels] + * @param {boolean} [images[].animated=false] - Set to `true` to read all frames/pages of an animated image. + * @param {string} [images[].failOn='warning'] - @see {@link /api-constructor/ constructor parameters} + * @param {number|boolean} [images[].limitInputPixels=268402689] - @see {@link /api-constructor/ constructor parameters} * @returns {Sharp} * @throws {Error} Invalid parameters */ @@ -160,7 +196,6 @@ function composite (images) { throw is.invalidParameterError('premultiplied', 'boolean', image.premultiplied); } } - return composite; }); return this; @@ -168,9 +203,10 @@ function composite (images) { /** * Decorate the Sharp prototype with composite-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Sharp.prototype.composite = composite; Sharp.blend = blend; }; diff --git a/lib/constructor.js b/lib/constructor.js index 214f85a61..9aac8105c 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -1,50 +1,35 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const util = require('util'); -const stream = require('stream'); +const util = require('node:util'); +const stream = require('node:stream'); const is = require('./is'); -require('./libvips').hasVendoredLibvips(); - -/* istanbul ignore next */ -try { - require('../build/Release/sharp.node'); -} catch (err) { - // Bail early if bindings aren't available - const help = ['', 'Something went wrong installing the "sharp" module', '', err.message, '']; - if (/NODE_MODULE_VERSION/.test(err.message)) { - help.push('- Ensure the version of Node.js used at install time matches that used at runtime'); - } else if (/invalid ELF header/.test(err.message)) { - help.push(`- Ensure "${process.platform}" is used at install time as well as runtime`); - } else if (/dylib/.test(err.message) && /Incompatible library version/.test(err.message)) { - help.push('- Run "brew update && brew upgrade vips"'); - } else { - help.push( - '- Remove the "node_modules/sharp" directory then run', - ' "npm install --ignore-scripts=false --verbose sharp" and look for errors' - ); - } - help.push( - '- Consult the installation documentation at https://sharp.pixelplumbing.com/install', - '- Search for this error at https://github.com/lovell/sharp/issues', '' - ); - const error = help.join('\n'); - throw new Error(error); -} +require('./sharp'); // Use NODE_DEBUG=sharp to enable libvips warnings const debuglog = util.debuglog('sharp'); +const queueListener = (queueLength) => { + Sharp.queue.emit('change', queueLength); +}; + /** * Constructor factory to create an instance of `sharp`, to which further methods are chained. * - * JPEG, PNG, WebP, AVIF or TIFF format image data can be streamed out from this object. + * JPEG, PNG, WebP, GIF, 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. * * Implements the [stream.Duplex](http://nodejs.org/api/stream.html#stream_class_stream_duplex) class. * + * When loading more than one page/frame of an animated image, + * these are combined as a vertically-stacked "toilet roll" image + * where the overall height is the `pageHeight` multiplied by the number of `pages`. + * * @constructs Sharp * * @emits Sharp#info @@ -59,19 +44,21 @@ const debuglog = util.debuglog('sharp'); * }); * * @example - * // Read image data from readableStream, + * // Read image data from remote URL, * // resize to 300 pixels wide, * // emit an 'info' event with calculated dimensions * // and finally write image data to writableStream - * var transformer = sharp() + * const { body } = fetch('https://...'); + * const readableStream = Readable.fromWeb(body); + * const transformer = sharp() * .resize(300) - * .on('info', function(info) { - * console.log('Image height is ' + info.height); + * .on('info', ({ height }) => { + * console.log(`Image height is ${height}`); * }); * readableStream.pipe(transformer).pipe(writableStream); * * @example - * // Create a blank 300x200 PNG image of semi-transluent red pixels + * // Create a blank 300x200 PNG image of semi-translucent red pixels * sharp({ * create: { * width: 300, @@ -117,40 +104,112 @@ const debuglog = util.debuglog('sharp'); * } * }).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 + * @example + * // Generate an image from text + * await sharp({ + * text: { + * text: 'Hello, world!', + * width: 400, // max width + * height: 300 // max height + * } + * }).toFile('text_bw.png'); + * + * @example + * // Generate an rgba image from text using pango markup and font + * await sharp({ + * text: { + * text: '<span foreground="red">Red!</span><span background="cyan">blue</span>', + * font: 'sans', + * rgba: true, + * dpi: 300 + * } + * }).toFile('text_rgba.png'); + * + * @example + * // Join four input images as a 2x2 grid with a 4 pixel gutter + * const data = await sharp( + * [image1, image2, image3, image4], + * { join: { across: 2, shim: 4 } } + * ).toBuffer(); + * + * @example + * // Generate a two-frame animated image from emoji + * const images = ['😀', '😛'].map(text => ({ + * text: { text, width: 64, height: 64, channels: 4, rgba: true } + * })); + * await sharp(images, { join: { animated: true } }).toFile('out.gif'); + * + * @param {(Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|string|Array)} [input] - if present, can be + * a Buffer / ArrayBuffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image data, or + * a TypedArray containing raw pixel image data, or * a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file. + * An array of inputs can be provided, and these will be joined together. * 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. + * @param {string} [options.failOn='warning'] - When to abort processing of invalid pixel data, one of (in order of sensitivity, least to most): 'none', 'truncated', 'error', 'warning'. Higher levels imply lower levels. Invalid metadata will always abort. * @param {number|boolean} [options.limitInputPixels=268402689] - 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). - * @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 {boolean} [options.unlimited=false] - Set this to `true` to remove safety features that help prevent memory exhaustion (JPEG, PNG, SVG, HEIF). + * @param {boolean} [options.autoOrient=false] - Set this to `true` to rotate/flip the image to match EXIF `Orientation`, if any. + * @param {boolean} [options.sequentialRead=true] - Set this to `false` to use random access rather than sequential read. Some operations will do this automatically. * @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 {number} [options.ignoreIcc=false] - should the embedded ICC profile, if any, be ignored. + * @param {number} [options.pages=1] - Number of pages to extract for multi-page input (GIF, WebP, TIFF), use -1 for all pages. + * @param {number} [options.page=0] - Page number to start extracting from for multi-page input (GIF, WebP, TIFF), zero based. + * @param {boolean} [options.animated=false] - Set to `true` to read all frames/pages of an animated image (GIF, WebP, TIFF), 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] - 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 {boolean} [options.raw.premultiplied] - specifies that the raw input has already been premultiplied, set to `true` + * to avoid sharp premultiplying the image. (optional, default `false`) + * @param {number} [options.raw.pageHeight] - The pixel height of each page/frame for animated images, must be an integral factor of `raw.height`. * @param {Object} [options.create] - describes a new image to be created. * @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 {number} [options.create.pageHeight] - The pixel height of each page/frame for animated images, must be an integral factor of `create.height`. * @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. + * @param {number} [options.create.noise.mean=128] - Mean value of pixels in the generated noise. + * @param {number} [options.create.noise.sigma=30] - Standard deviation of pixel values in the generated noise. + * @param {Object} [options.text] - describes a new text image to be created. + * @param {string} [options.text.text] - text to render as a UTF-8 string. It can contain Pango markup, for example `<i>Le</i>Monde`. + * @param {string} [options.text.font] - font name to render with. + * @param {string} [options.text.fontfile] - absolute filesystem path to a font file that can be used by `font`. + * @param {number} [options.text.width=0] - Integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. + * @param {number} [options.text.height=0] - Maximum integral number of pixels high. When defined, `dpi` will be ignored and the text will automatically fit the pixel resolution defined by `width` and `height`. Will be ignored if `width` is not specified or set to 0. + * @param {string} [options.text.align='left'] - Alignment style for multi-line text (`'left'`, `'centre'`, `'center'`, `'right'`). + * @param {boolean} [options.text.justify=false] - set this to true to apply justification to the text. + * @param {number} [options.text.dpi=72] - the resolution (size) at which to render the text. Does not take effect if `height` is specified. + * @param {boolean} [options.text.rgba=false] - set this to true to enable RGBA output. This is useful for colour emoji rendering, or support for pango markup features like `<span foreground="red">Red!</span>`. + * @param {number} [options.text.spacing=0] - text line height in points. Will use the font line height if none is specified. + * @param {string} [options.text.wrap='word'] - word wrapping style when width is provided, one of: 'word', 'char', 'word-char' (prefer word, fallback to char) or 'none'. + * @param {Object} [options.join] - describes how an array of input images should be joined. + * @param {number} [options.join.across=1] - number of images to join horizontally. + * @param {boolean} [options.join.animated=false] - set this to `true` to join the images as an animated image. + * @param {number} [options.join.shim=0] - number of pixels to insert between joined images. + * @param {string|Object} [options.join.background] - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. + * @param {string} [options.join.halign='left'] - horizontal alignment style for images joined horizontally (`'left'`, `'centre'`, `'center'`, `'right'`). + * @param {string} [options.join.valign='top'] - vertical alignment style for images joined vertically (`'top'`, `'centre'`, `'center'`, `'bottom'`). + * @param {Object} [options.tiff] - Describes TIFF specific options. + * @param {number} [options.tiff.subifd=-1] - Sub Image File Directory to extract for OME-TIFF, defaults to main image. + * @param {Object} [options.svg] - Describes SVG specific options. + * @param {string} [options.svg.stylesheet] - Custom CSS for SVG input, applied with a User Origin during the CSS cascade. + * @param {boolean} [options.svg.highBitdepth=false] - Set to `true` to render SVG input at 32-bits per channel (128-bit) instead of 8-bits per channel (32-bit) RGBA. + * @param {Object} [options.pdf] - Describes PDF specific options. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick. + * @param {string|Object} [options.pdf.background] - Background colour to use when PDF is partially transparent. Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. + * @param {Object} [options.openSlide] - Describes OpenSlide specific options. Requires the use of a globally-installed libvips compiled with support for OpenSlide. + * @param {number} [options.openSlide.level=0] - Level to extract from a multi-level input, zero based. + * @param {Object} [options.jp2] - Describes JPEG 2000 specific options. Requires the use of a globally-installed libvips compiled with support for OpenJPEG. + * @param {boolean} [options.jp2.oneshot=false] - Set to `true` to decode tiled JPEG 2000 images in a single operation, improving compatibility. * @returns {Sharp} * @throws {Error} Invalid parameters */ const Sharp = function (input, options) { + // biome-ignore lint/complexity/noArguments: constructor factory if (arguments.length === 1 && !is.defined(input)) { throw new Error('Invalid input'); } @@ -173,11 +232,11 @@ const Sharp = function (input, options) { canvas: 'crop', position: 0, resizeBackground: [0, 0, 0, 255], - useExifOrientation: false, angle: 0, rotationAngle: 0, rotationBackground: [0, 0, 0, 255], - rotateBeforePreExtract: false, + rotateBefore: false, + orientBefore: false, flip: false, flop: false, extendTop: 0, @@ -185,7 +244,9 @@ const Sharp = function (input, options) { extendLeft: 0, extendRight: 0, extendBackground: [0, 0, 0, 255], + extendWith: 'background', withoutEnlargement: false, + withoutReduction: false, affineMatrix: [], affineBackground: [0, 0, 0, 255], affineIdx: 0, @@ -196,42 +257,65 @@ const Sharp = function (input, options) { kernel: 'lanczos3', fastShrinkOnLoad: true, // operations - tintA: 128, - tintB: 128, + tint: [-1, 0, 0, 0], flatten: false, flattenBackground: [0, 0, 0], + unflatten: false, negate: false, + negateAlpha: true, medianSize: 0, blurSigma: 0, + precision: 'integer', + minAmpl: 0.2, sharpenSigma: 0, - sharpenFlat: 1, - sharpenJagged: 2, + sharpenM1: 1, + sharpenM2: 2, + sharpenX1: 2, + sharpenY2: 10, + sharpenY3: 20, threshold: 0, thresholdGrayscale: true, - trimThreshold: 0, + trimBackground: [], + trimThreshold: -1, + trimLineArt: false, + dilateWidth: 0, + erodeWidth: 0, gamma: 0, gammaOut: 0, greyscale: false, normalise: false, + normaliseLower: 1, + normaliseUpper: 99, + claheWidth: 0, + claheHeight: 0, + claheMaxSlope: 3, brightness: 1, saturation: 1, hue: 0, + lightness: 0, booleanBufferIn: null, booleanFileIn: '', joinChannelIn: [], extractChannel: -1, removeAlpha: false, - ensureAlpha: false, + ensureAlpha: -1, colourspace: 'srgb', + colourspacePipeline: 'last', composite: [], // output fileOut: '', formatOut: 'input', streamOut: false, - withMetadata: false, + keepMetadata: 0, withMetadataOrientation: -1, - withMetadataIcc: '', + withMetadataDensity: 0, + withIccProfile: '', + withExif: {}, + withExifMerge: true, + withXmp: '', resolveWithObject: false, + loop: -1, + delay: [], // output format jpegQuality: 80, jpegProgressive: false, @@ -246,29 +330,56 @@ const Sharp = function (input, options) { pngAdaptiveFiltering: false, pngPalette: false, pngQuality: 100, - pngColours: 256, + pngEffort: 7, + pngBitdepth: 8, pngDither: 1, + jp2Quality: 80, + jp2TileHeight: 512, + jp2TileWidth: 512, + jp2Lossless: false, + jp2ChromaSubsampling: '4:4:4', webpQuality: 80, webpAlphaQuality: 100, webpLossless: false, webpNearLossless: false, webpSmartSubsample: false, - webpReductionEffort: 4, + webpSmartDeblock: false, + webpPreset: 'default', + webpEffort: 4, + webpMinSize: false, + webpMixed: false, + gifBitdepth: 8, + gifEffort: 7, + gifDither: 1, + gifInterFrameMaxError: 0, + gifInterPaletteMaxError: 3, + gifKeepDuplicateFrames: false, + gifReuse: true, + gifProgressive: false, tiffQuality: 80, tiffCompression: 'jpeg', + tiffBigtiff: false, tiffPredictor: 'horizontal', tiffPyramid: false, + tiffMiniswhite: false, tiffBitdepth: 8, tiffTile: false, tiffTileHeight: 256, tiffTileWidth: 256, tiffXres: 1.0, tiffYres: 1.0, + tiffResolutionUnit: 'inch', heifQuality: 50, heifLossless: false, heifCompression: 'av1', - heifSpeed: 5, - heifChromaSubsampling: '4:2:0', + heifEffort: 4, + heifChromaSubsampling: '4:4:4', + heifBitdepth: 8, + jxlDistance: 1, + jxlDecodingTier: 0, + jxlEffort: 7, + jxlLossless: false, + rawDepth: 'uchar', tileSize: 256, tileOverlap: 0, tileContainer: 'fs', @@ -280,22 +391,24 @@ const Sharp = function (input, options) { tileBackground: [255, 255, 255, 255], tileCentre: false, tileId: 'https://example.com/iiif', - linearA: 1, - linearB: 0, + tileBasename: '', + timeoutSeconds: 0, + linearA: [], + linearB: [], + pdfBackground: [255, 255, 255, 255], // Function to notify of libvips warnings debuglog: warning => { this.emit('warning', warning); debuglog(warning); }, // Function to notify of queue length changes - queueListener: function (queueLength) { - Sharp.queue.emit('change', queueLength); - } + queueListener }; this.options.input = this._createInputDescriptor(input, options, { allowStream: true }); return this; }; -util.inherits(Sharp, stream.Duplex); +Object.setPrototypeOf(Sharp.prototype, stream.Duplex.prototype); +Object.setPrototypeOf(Sharp, stream.Duplex); /** * Take a "snapshot" of the Sharp instance, returning a new instance. @@ -315,9 +428,7 @@ util.inherits(Sharp, stream.Duplex); * // Using Promises to know when the pipeline is complete * const fs = require("fs"); * const got = require("got"); - * const sharpStream = sharp({ - * failOnError: false - * }); + * const sharpStream = sharp({ failOn: 'none' }); * * const promises = []; * @@ -344,7 +455,7 @@ util.inherits(Sharp, stream.Duplex); * .toFile("optimized-500.webp") * ); * - * // https://github.com/sindresorhus/got#gotstreamurl-options + * // https://github.com/sindresorhus/got/blob/main/documentation/3-streams.md * got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream); * * Promise.all(promises) @@ -363,13 +474,16 @@ util.inherits(Sharp, stream.Duplex); function clone () { // Clone existing options const clone = this.constructor.call(); - clone.options = Object.assign({}, this.options); + const { debuglog, queueListener, ...options } = this.options; + clone.options = structuredClone(options); + clone.options.debuglog = debuglog; + clone.options.queueListener = queueListener; // Pass 'finish' event to clone for Stream-based input if (this._isStreamInput()) { this.on('finish', () => { // Clone inherits input data this._flattenBufferIn(); - clone.options.bufferIn = this.options.bufferIn; + clone.options.input.buffer = this.options.input.buffer; clone.emit('finish'); }); } @@ -379,6 +493,7 @@ Object.assign(Sharp.prototype, { clone }); /** * Export constructor. + * @module Sharp * @private */ module.exports = Sharp; diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 000000000..13714c947 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,1971 @@ +/** + * Copyright 2017 François Nguyen and others. + * + * Billy Kwok <https://github.com/billykwok> + * Bradley Odell <https://github.com/BTOdell> + * Espen Hovlandsdal <https://github.com/rexxars> + * Floris de Bijl <https://github.com/Fdebijl> + * François Nguyen <https://github.com/phurytw> + * Jamie Woodbury <https://github.com/JamieWoodbury> + * Wooseop Kim <https://github.com/wooseopkim> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +// SPDX-License-Identifier: MIT + +/// <reference types="node" /> + +import type { Duplex } from 'node:stream'; + +//#region Constructor functions + +/** + * Creates a sharp instance from an image + * @param input Buffer containing JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data, or String containing the path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file. + * @param options Object with optional attributes. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ +declare function sharp(options?: sharp.SharpOptions): sharp.Sharp; +declare function sharp( + input?: sharp.SharpInput | Array<sharp.SharpInput>, + options?: sharp.SharpOptions, +): sharp.Sharp; + +declare namespace sharp { + /** Object containing nested boolean values representing the available input and output formats/methods. */ + const format: FormatEnum; + + /** An Object containing the version numbers of sharp, libvips and its dependencies. */ + const versions: { + aom?: string | undefined; + archive?: string | undefined; + cairo?: string | undefined; + cgif?: string | undefined; + exif?: string | undefined; + expat?: string | undefined; + ffi?: string | undefined; + fontconfig?: string | undefined; + freetype?: string | undefined; + fribidi?: string | undefined; + glib?: string | undefined; + harfbuzz?: string | undefined; + heif?: string | undefined; + highway?: string | undefined; + imagequant?: string | undefined; + lcms?: string | undefined; + mozjpeg?: string | undefined; + pango?: string | undefined; + pixman?: string | undefined; + png?: string | undefined; + "proxy-libintl"?: string | undefined; + rsvg?: string | undefined; + sharp: string; + spng?: string | undefined; + tiff?: string | undefined; + vips: string; + webp?: string | undefined; + xml?: string | undefined; + "zlib-ng"?: string | undefined; + }; + + /** An Object containing the available interpolators and their proper values */ + const interpolators: Interpolators; + + /** An EventEmitter that emits a change event when a task is either queued, waiting for libuv to provide a worker thread, complete */ + const queue: NodeJS.EventEmitter; + + //#endregion + + //#region Utility functions + + /** + * Gets or, when options are provided, sets the limits of libvips' operation cache. + * Existing entries in the cache will be trimmed after any change in limits. + * This method always returns cache statistics, useful for determining how much working memory is required for a particular task. + * @param options Object with the following attributes, or Boolean where true uses default cache settings and false removes all caching (optional, default true) + * @returns The cache results. + */ + function cache(options?: boolean | CacheOptions): CacheResult; + + /** + * Gets or sets the number of threads libvips' should create to process each image. + * The default value is the number of CPU cores. A value of 0 will reset to this default. + * The maximum number of images that can be processed in parallel is limited by libuv's UV_THREADPOOL_SIZE environment variable. + * @param concurrency The new concurrency value. + * @returns The current concurrency value. + */ + function concurrency(concurrency?: number): number; + + /** + * Provides access to internal task counters. + * @returns Object containing task counters + */ + function counters(): SharpCounters; + + /** + * Get and set use of SIMD vector unit instructions. Requires libvips to have been compiled with highway support. + * Improves the performance of resize, blur and sharpen operations by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM NEON. + * @param enable enable or disable use of SIMD vector unit instructions + * @returns true if usage of SIMD vector unit instructions is enabled + */ + function simd(enable?: boolean): boolean; + + /** + * Block libvips operations at runtime. + * + * This is in addition to the `VIPS_BLOCK_UNTRUSTED` environment variable, + * which when set will block all "untrusted" operations. + * + * @since 0.32.4 + * + * @example <caption>Block all TIFF input.</caption> + * sharp.block({ + * operation: ['VipsForeignLoadTiff'] + * }); + * + * @param {Object} options + * @param {Array<string>} options.operation - List of libvips low-level operation names to block. + */ + function block(options: { operation: string[] }): void; + + /** + * Unblock libvips operations at runtime. + * + * This is useful for defining a list of allowed operations. + * + * @since 0.32.4 + * + * @example <caption>Block all input except WebP from the filesystem.</caption> + * sharp.block({ + * operation: ['VipsForeignLoad'] + * }); + * sharp.unblock({ + * operation: ['VipsForeignLoadWebpFile'] + * }); + * + * @example <caption>Block all input except JPEG and PNG from a Buffer or Stream.</caption> + * sharp.block({ + * operation: ['VipsForeignLoad'] + * }); + * sharp.unblock({ + * operation: ['VipsForeignLoadJpegBuffer', 'VipsForeignLoadPngBuffer'] + * }); + * + * @param {Object} options + * @param {Array<string>} options.operation - List of libvips low-level operation names to unblock. + */ + function unblock(options: { operation: string[] }): void; + + //#endregion + + const gravity: GravityEnum; + const strategy: StrategyEnum; + const kernel: KernelEnum; + const fit: FitEnum; + const bool: BoolEnum; + + interface Sharp extends Duplex { + //#region Channel functions + + /** + * Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel. + * @returns A sharp instance that can be used to chain operations + */ + removeAlpha(): Sharp; + + /** + * Ensure alpha channel, if missing. The added alpha channel will be fully opaque. This is a no-op if the image already has an alpha channel. + * @param alpha transparency level (0=fully-transparent, 1=fully-opaque) (optional, default 1). + * @returns A sharp instance that can be used to chain operations + */ + ensureAlpha(alpha?: number): Sharp; + + /** + * Extract a single channel from a multi-channel image. + * @param channel zero-indexed channel/band number to extract, or red, green, blue or alpha. + * @throws {Error} Invalid channel + * @returns A sharp instance that can be used to chain operations + */ + extractChannel(channel: 0 | 1 | 2 | 3 | 'red' | 'green' | 'blue' | 'alpha'): Sharp; + + /** + * Join one or more channels to the image. The meaning of the added channels depends on the output colourspace, set with toColourspace(). + * By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. 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. + * 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 images one or more images (file paths, Buffers). + * @param options image options, see sharp() constructor. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + joinChannel(images: string | Buffer | ArrayLike<string | Buffer>, options?: SharpOptions): Sharp; + + /** + * Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image. + * @param boolOp one of "and", "or" or "eor" to perform that bitwise operation, like the C logic operators &, | and ^ respectively. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + bandbool(boolOp: keyof BoolEnum): Sharp; + + //#endregion + + //#region Color functions + + /** + * Tint the image using the provided colour. + * An alpha channel may be present and will be unchanged by the operation. + * @param tint Parsed by the color module. + * @returns A sharp instance that can be used to chain operations + */ + tint(tint: Colour | Color): Sharp; + + /** + * Convert to 8-bit greyscale; 256 shades of grey. + * This is a linear operation. + * If the input image is in a non-linear colour space such as sRGB, use gamma() with greyscale() for the best results. + * By default the output image will be web-friendly sRGB and contain three (identical) colour channels. + * This may be overridden by other sharp operations such as toColourspace('b-w'), which will produce an output image containing one colour channel. + * An alpha channel may be present, and will be unchanged by the operation. + * @param greyscale true to enable and false to disable (defaults to true) + * @returns A sharp instance that can be used to chain operations + */ + greyscale(greyscale?: boolean): Sharp; + + /** + * Alternative spelling of greyscale(). + * @param grayscale true to enable and false to disable (defaults to true) + * @returns A sharp instance that can be used to chain operations + */ + grayscale(grayscale?: boolean): Sharp; + + /** + * Set the pipeline colourspace. + * The input image will be converted to the provided colourspace at the start of the pipeline. + * All operations will use this colourspace before converting to the output colourspace, as defined by toColourspace. + * This feature is experimental and has not yet been fully-tested with all operations. + * + * @param colourspace pipeline colourspace e.g. rgb16, scrgb, lab, grey16 ... + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + pipelineColourspace(colourspace?: string): Sharp; + + /** + * Alternative spelling of pipelineColourspace + * @param colorspace pipeline colourspace e.g. rgb16, scrgb, lab, grey16 ... + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + pipelineColorspace(colorspace?: string): Sharp; + + /** + * Set the output colourspace. + * By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. + * @param colourspace output colourspace e.g. srgb, rgb, cmyk, lab, b-w ... + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + toColourspace(colourspace?: string): Sharp; + + /** + * Alternative spelling of toColourspace(). + * @param colorspace output colorspace e.g. srgb, rgb, cmyk, lab, b-w ... + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + toColorspace(colorspace: string): Sharp; + + //#endregion + + //#region Composite functions + + /** + * Composite image(s) over the processed (resized, extracted etc.) image. + * + * The images to composite must be the same size or smaller than the processed image. + * If both `top` and `left` options are provided, they take precedence over `gravity`. + * @param images - Ordered list of images to composite + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + composite(images: OverlayOptions[]): Sharp; + + //#endregion + + //#region Input functions + + /** + * Take a "snapshot" of the Sharp instance, returning a new instance. + * Cloned instances inherit the input of their parent instance. + * This allows multiple output Streams and therefore multiple processing pipelines to share a single input Stream. + * @returns A sharp instance that can be used to chain operations + */ + clone(): Sharp; + + /** + * Fast access to (uncached) image metadata without decoding any compressed image data. + * @returns A sharp instance that can be used to chain operations + */ + metadata(callback: (err: Error, metadata: Metadata) => void): Sharp; + + /** + * Fast access to (uncached) image metadata without decoding any compressed image data. + * @returns A promise that resolves with a metadata object + */ + metadata(): Promise<Metadata>; + + /** + * Keep all metadata (EXIF, ICC, XMP, IPTC) from the input image in the output image. + * @returns A sharp instance that can be used to chain operations + */ + keepMetadata(): Sharp; + + /** + * Access to pixel-derived image statistics for every channel in the image. + * @returns A sharp instance that can be used to chain operations + */ + stats(callback: (err: Error, stats: Stats) => void): Sharp; + + /** + * Access to pixel-derived image statistics for every channel in the image. + * @returns A promise that resolves with a stats object + */ + stats(): Promise<Stats>; + + //#endregion + + //#region Operation functions + + /** + * Rotate the output image by either an explicit angle + * or auto-orient based on the EXIF `Orientation` tag. + * + * If an angle is provided, it is converted to a valid positive degree rotation. + * For example, `-450` will produce a 270 degree rotation. + * + * When rotating by an angle other than a multiple of 90, + * the background colour can be provided with the `background` option. + * + * If no angle is provided, it is determined from the EXIF data. + * Mirroring is supported and may infer the use of a flip operation. + * + * The use of `rotate` without an angle will remove the EXIF `Orientation` tag, if any. + * + * Only one rotation can occur per pipeline (aside from an initial call without + * arguments to orient via EXIF data). Previous calls to `rotate` in the same + * pipeline will be ignored. + * + * Multi-page images can only be rotated by 180 degrees. + * + * Method order is important when rotating, resizing and/or extracting regions, + * for example `.rotate(x).extract(y)` will produce a different result to `.extract(y).rotate(x)`. + * + * @example + * const pipeline = sharp() + * .rotate() + * .resize(null, 200) + * .toBuffer(function (err, outputBuffer, info) { + * // outputBuffer contains 200px high JPEG image data, + * // auto-rotated using EXIF Orientation tag + * // info.width and info.height contain the dimensions of the resized image + * }); + * readableStream.pipe(pipeline); + * + * @example + * const rotateThenResize = await sharp(input) + * .rotate(90) + * .resize({ width: 16, height: 8, fit: 'fill' }) + * .toBuffer(); + * const resizeThenRotate = await sharp(input) + * .resize({ width: 16, height: 8, fit: 'fill' }) + * .rotate(90) + * .toBuffer(); + * + * @param {number} [angle=auto] angle of rotation. + * @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. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ + rotate(angle?: number, options?: RotateOptions): Sharp; + + /** + * Alias for calling `rotate()` with no arguments, which orients the image based + * on EXIF orientsion. + * + * This operation is aliased to emphasize its purpose, helping to remove any + * confusion between rotation and orientation. + * + * @example + * const output = await sharp(input).autoOrient().toBuffer(); + * + * @returns {Sharp} + */ + autoOrient(): Sharp + + /** + * Flip the image about the vertical Y axis. This always occurs after rotation, if any. + * The use of flip implies the removal of the EXIF Orientation tag, if any. + * @param flip true to enable and false to disable (defaults to true) + * @returns A sharp instance that can be used to chain operations + */ + flip(flip?: boolean): Sharp; + + /** + * Flop the image about the horizontal X axis. This always occurs after rotation, if any. + * The use of flop implies the removal of the EXIF Orientation tag, if any. + * @param flop true to enable and false to disable (defaults to true) + * @returns A sharp instance that can be used to chain operations + */ + flop(flop?: boolean): Sharp; + + /** + * 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 colour with the `background` option. + * A particular interpolator may also be specified. Set the `interpolator` option to an attribute of the `sharp.interpolators` Object e.g. `sharp.interpolators.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. + * + * @param matrix Affine transformation matrix, may either by a array of length four or a 2x2 matrix array + * @param options if present, is an Object with optional attributes. + * + * @returns A sharp instance that can be used to chain operations + */ + affine(matrix: [number, number, number, number] | Matrix2x2, options?: AffineOptions): Sharp; + + /** + * Sharpen the image. + * When used without parameters, performs a fast, mild sharpen of the output image. + * When a sigma is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space. + * Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available. + * @param options if present, is an Object with optional attributes + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + sharpen(options?: SharpenOptions): Sharp; + + /** + * Sharpen the image. + * When used without parameters, performs a fast, mild sharpen of the output image. + * When a sigma is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space. + * Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available. + * @param sigma the sigma of the Gaussian mask, where sigma = 1 + radius / 2. + * @param flat the level of sharpening to apply to "flat" areas. (optional, default 1.0) + * @param jagged the level of sharpening to apply to "jagged" areas. (optional, default 2.0) + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + * + * @deprecated Use the object parameter `sharpen({sigma, m1, m2, x1, y2, y3})` instead + */ + sharpen(sigma?: number, flat?: number, jagged?: number): Sharp; + + /** + * Apply median filter. When used without parameters the default window is 3x3. + * @param size square mask size: size x size (optional, default 3) + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + median(size?: number): Sharp; + + /** + * Blur the image. + * When used without parameters, performs a fast, mild blur of the output image. + * When a sigma is provided, performs a slower, more accurate Gaussian blur. + * When a boolean sigma is provided, ether blur mild or disable blur + * @param sigma a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where sigma = 1 + radius / 2. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + blur(sigma?: number | boolean | BlurOptions): Sharp; + + /** + * Expand foreground objects using the dilate morphological operator. + * @param {Number} [width=1] dilation width in pixels. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + dilate(width?: number): Sharp; + + /** + * Shrink foreground objects using the erode morphological operator. + * @param {Number} [width=1] erosion width in pixels. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + erode(width?: number): Sharp; + + /** + * Merge alpha transparency channel, if any, with background. + * @param flatten true to enable and false to disable (defaults to true) + * @returns A sharp instance that can be used to chain operations + */ + flatten(flatten?: boolean | FlattenOptions): Sharp; + + /** + * Ensure the image has an alpha channel with all white pixel values made fully transparent. + * Existing alpha channel values for non-white pixels remain unchanged. + * @returns A sharp instance that can be used to chain operations + */ + unflatten(): Sharp; + + /** + * Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of 1/gamma then increasing the encoding (brighten) post-resize at a factor of gamma. + * This can improve the perceived brightness of a resized image in non-linear colour spaces. + * JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation when applying a gamma correction. + * Supply a second argument to use a different output gamma value, otherwise the first value is used in both cases. + * @param gamma value between 1.0 and 3.0. (optional, default 2.2) + * @param gammaOut value between 1.0 and 3.0. (optional, defaults to same as gamma) + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + gamma(gamma?: number, gammaOut?: number): Sharp; + + /** + * Produce the "negative" of the image. + * @param negate true to enable and false to disable, or an object of options (defaults to true) + * @returns A sharp instance that can be used to chain operations + */ + negate(negate?: boolean | NegateOptions): Sharp; + + /** + * Enhance output image contrast by stretching its luminance to cover a full dynamic range. + * + * Uses a histogram-based approach, taking a default range of 1% to 99% to reduce sensitivity to noise at the extremes. + * + * Luminance values below the `lower` percentile will be underexposed by clipping to zero. + * Luminance values above the `upper` percentile will be overexposed by clipping to the max pixel value. + * + * @param normalise options + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + normalise(normalise?: NormaliseOptions): Sharp; + + /** + * Alternative spelling of normalise. + * @param normalize options + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + normalize(normalize?: NormaliseOptions): Sharp; + + /** + * Perform contrast limiting adaptive histogram equalization (CLAHE) + * + * This will, in general, enhance the clarity of the image by bringing out + * darker details. Please read more about CLAHE here: + * https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE + * + * @param options clahe options + */ + clahe(options: ClaheOptions): Sharp; + + /** + * Convolve the image with the specified kernel. + * @param kernel the specified kernel + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + convolve(kernel: Kernel): Sharp; + + /** + * Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0. + * @param threshold a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default 128) + * @param options threshold options + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + threshold(threshold?: number, options?: ThresholdOptions): Sharp; + + /** + * Perform a bitwise boolean operation with operand image. + * This operation creates an output image where each pixel is the result of the selected bitwise boolean operation between the corresponding pixels of the input images. + * @param operand Buffer containing image data or String containing the path to an image file. + * @param operator one of "and", "or" or "eor" to perform that bitwise operation, like the C logic operators &, | and ^ respectively. + * @param options describes operand when using raw pixel data. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + boolean(operand: string | Buffer, operator: keyof BoolEnum, options?: { raw: Raw }): Sharp; + + /** + * Apply the linear formula a * input + b to the image (levels adjustment) + * @param a multiplier (optional, default 1.0) + * @param b offset (optional, default 0.0) + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + linear(a?: number | number[] | null, b?: number | number[]): Sharp; + + /** + * Recomb the image with the specified matrix. + * @param inputMatrix 3x3 Recombination matrix or 4x4 Recombination matrix + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + recomb(inputMatrix: Matrix3x3 | Matrix4x4): Sharp; + + /** + * Transforms the image using brightness, saturation, hue rotation and lightness. + * Brightness and lightness both operate on luminance, with the difference being that brightness is multiplicative whereas lightness is additive. + * @param options describes the modulation + * @returns A sharp instance that can be used to chain operations + */ + modulate(options?: { + brightness?: number | undefined; + saturation?: number | undefined; + hue?: number | undefined; + lightness?: number | undefined; + }): Sharp; + + //#endregion + + //#region Output functions + + /** + * 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, AVIF, TIFF, DZI, and libvips' V format supported. + * Note that raw pixel data is only supported for buffer output. + * @param fileOut The path to write the image data to. + * @param callback Callback function called on completion with two arguments (err, info). info contains the output image format, size (bytes), width, height and channels. + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + toFile(fileOut: string, callback: (err: Error, info: OutputInfo) => void): Sharp; + + /** + * Write output image data to a file. + * @param fileOut The path to write the image data to. + * @throws {Error} Invalid parameters + * @returns A promise that fulfills with an object containing information on the resulting file + */ + toFile(fileOut: string): Promise<OutputInfo>; + + /** + * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported. + * By default, the format will match the input image, except SVG input which becomes PNG output. + * @param callback Callback function called on completion with three arguments (err, buffer, info). + * @returns A sharp instance that can be used to chain operations + */ + toBuffer(callback: (err: Error, buffer: Buffer, info: OutputInfo) => void): Sharp; + + /** + * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported. + * By default, the format will match the input image, except SVG input which becomes PNG output. + * @param options resolve options + * @param options.resolveWithObject Resolve the Promise with an Object containing data and info properties instead of resolving only with data. + * @returns A promise that resolves with the Buffer data. + */ + toBuffer(options?: { resolveWithObject: false }): Promise<Buffer>; + + /** + * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported. + * By default, the format will match the input image, except SVG input which becomes PNG output. + * @param options resolve options + * @param options.resolveWithObject Resolve the Promise with an Object containing data and info properties instead of resolving only with data. + * @returns A promise that resolves with an object containing the Buffer data and an info object containing the output image format, size (bytes), width, height and channels + */ + toBuffer(options: { resolveWithObject: true }): Promise<{ data: Buffer; info: OutputInfo }>; + + /** + * Keep all EXIF metadata from the input image in the output image. + * EXIF metadata is unsupported for TIFF output. + * @returns A sharp instance that can be used to chain operations + */ + keepExif(): Sharp; + + /** + * Set EXIF metadata in the output image, ignoring any EXIF in the input image. + * @param {Exif} exif Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. + * @returns A sharp instance that can be used to chain operations + * @throws {Error} Invalid parameters + */ + withExif(exif: Exif): Sharp; + + /** + * Update EXIF metadata from the input image in the output image. + * @param {Exif} exif Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. + * @returns A sharp instance that can be used to chain operations + * @throws {Error} Invalid parameters + */ + withExifMerge(exif: Exif): Sharp; + + /** + * Keep ICC profile from the input image in the output image where possible. + * @returns A sharp instance that can be used to chain operations + */ + keepIccProfile(): Sharp; + + /** + * Transform using an ICC profile and attach to the output image. + * @param {string} icc - Absolute filesystem path to output ICC profile or built-in profile name (srgb, p3, cmyk). + * @returns A sharp instance that can be used to chain operations + * @throws {Error} Invalid parameters + */ + withIccProfile(icc: string, options?: WithIccProfileOptions): Sharp; + + /** + * Keep all XMP metadata from the input image in the output image. + * @returns A sharp instance that can be used to chain operations + */ + keepXmp(): Sharp; + + /** + * Set XMP metadata in the output image. + * @param {string} xmp - String containing XMP metadata to be embedded in the output image. + * @returns A sharp instance that can be used to chain operations + * @throws {Error} Invalid parameters + */ + withXmp(xmp: string): Sharp; + + /** + * Include all metadata (EXIF, XMP, IPTC) from the input image in the output image. + * The default behaviour, when withMetadata is not used, is to strip all metadata and convert to the device-independent sRGB colour space. + * This will also convert to and add a web-friendly sRGB ICC profile. + * @param withMetadata + * @throws {Error} Invalid parameters. + */ + withMetadata(withMetadata?: WriteableMetadata): Sharp; + + /** + * Use these JPEG options for output image. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + jpeg(options?: JpegOptions): Sharp; + + /** + * Use these JP2 (JPEG 2000) options for output image. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + jp2(options?: Jp2Options): Sharp; + + /** + * Use these JPEG-XL (JXL) options for output image. + * This feature is experimental, please do not use in production systems. + * Requires libvips compiled with support for libjxl. + * The prebuilt binaries do not include this. + * Image metadata (EXIF, XMP) is unsupported. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + jxl(options?: JxlOptions): Sharp; + + /** + * Use these PNG options for output image. + * PNG output is always full colour at 8 or 16 bits per pixel. + * Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + png(options?: PngOptions): Sharp; + + /** + * Use these WebP options for output image. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + webp(options?: WebpOptions): Sharp; + + /** + * Use these GIF options for output image. + * Requires libvips compiled with support for ImageMagick or GraphicsMagick. The prebuilt binaries do not include this - see installing a custom libvips. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + gif(options?: GifOptions): Sharp; + + /** + * Use these AVIF options for output image. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + avif(options?: AvifOptions): Sharp; + + /** + * 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. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + heif(options?: HeifOptions): Sharp; + + /** + * Use these TIFF options for output image. + * @param options Output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + tiff(options?: TiffOptions): Sharp; + + /** + * Force output to be raw, uncompressed uint8 pixel data. + * @param options Raw output options. + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + raw(options?: RawOptions): Sharp; + + /** + * Force output to a given format. + * @param format a String or an Object with an 'id' attribute + * @param options output options + * @throws {Error} Unsupported format or options + * @returns A sharp instance that can be used to chain operations + */ + toFormat( + format: keyof FormatEnum | AvailableFormatInfo, + options?: + | OutputOptions + | JpegOptions + | PngOptions + | WebpOptions + | AvifOptions + | HeifOptions + | JxlOptions + | GifOptions + | Jp2Options + | RawOptions + | TiffOptions, + ): Sharp; + + /** + * Use tile-based deep zoom (image pyramid) output. + * Set the format and options for tile images via the toFormat, jpeg, png or webp functions. + * Use a .zip or .szi file extension with toFile to write to a compressed archive file format. + * @param tile tile options + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + tile(tile?: TileOptions): Sharp; + + /** + * Set a timeout for processing, in seconds. Use a value of zero to continue processing indefinitely, the default behaviour. + * The clock starts when libvips opens an input image for processing. Time spent waiting for a libuv thread to become available is not included. + * @param options Object with a `seconds` attribute between 0 and 3600 (number) + * @throws {Error} Invalid options + * @returns A sharp instance that can be used to chain operations + */ + timeout(options: TimeoutOptions): Sharp; + + //#endregion + + //#region Resize functions + + /** + * Resize image to width, height or width x height. + * + * When both a width and height are provided, the possible methods by which the image should fit these are: + * - cover: Crop to cover both provided dimensions (the default). + * - contain: Embed within both provided dimensions. + * - fill: Ignore the aspect ratio of the input and stretch to both provided dimensions. + * - inside: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified. + * - outside: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified. + * Some of these values are based on the object-fit CSS property. + * + * When using a fit of cover or contain, the default position is centre. Other options are: + * - sharp.position: top, right top, right, right bottom, bottom, left bottom, left, left top. + * - sharp.gravity: north, northeast, east, southeast, south, southwest, west, northwest, center or centre. + * - sharp.strategy: cover only, dynamically crop using either the entropy or attention strategy. Some of these values are based on the object-position CSS property. + * + * The experimental strategy-based approach resizes so one dimension is at its target length then repeatedly ranks edge regions, + * discarding the edge with the lowest score based on the selected strategy. + * - entropy: focus on the region with the highest Shannon entropy. + * - attention: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones. + * + * Possible interpolation kernels are: + * - nearest: Use nearest neighbour interpolation. + * - cubic: Use a Catmull-Rom spline. + * - lanczos2: Use a Lanczos kernel with a=2. + * - lanczos3: Use a Lanczos kernel with a=3 (the default). + * + * @param width pixels wide the resultant image should be. Use null or undefined to auto-scale the width to match the height. + * @param height pixels high the resultant image should be. Use null or undefined to auto-scale the height to match the width. + * @param options resize options + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + resize(widthOrOptions?: number | ResizeOptions | null, height?: number | null, options?: ResizeOptions): Sharp; + + /** + * Shorthand for resize(null, null, options); + * + * @param options resize options + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + resize(options: ResizeOptions): Sharp; + + /** + * Extend / pad / extrude one or more edges of the image with either + * the provided background colour or pixels derived from the image. + * This operation will always occur after resizing and extraction, if any. + * @param extend single pixel count to add to all edges or an Object with per-edge counts + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + extend(extend: number | ExtendOptions): Sharp; + + /** + * Extract a region of the image. + * - Use extract() before resize() for pre-resize extraction. + * - Use extract() after resize() for post-resize extraction. + * - Use extract() before and after for both. + * + * @param region The region to extract + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + extract(region: Region): Sharp; + + /** + * Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel. + * Images with an alpha channel will use the combined bounding box of alpha and non-alpha channels. + * The info response Object will contain trimOffsetLeft and trimOffsetTop properties. + * @param options trim options + * @throws {Error} Invalid parameters + * @returns A sharp instance that can be used to chain operations + */ + trim(options?: TrimOptions): Sharp; + + //#endregion + } + + type SharpInput = Buffer + | ArrayBuffer + | Uint8Array + | Uint8ClampedArray + | Int8Array + | Uint16Array + | Int16Array + | Uint32Array + | Int32Array + | Float32Array + | Float64Array + | string; + + interface SharpOptions { + /** + * Auto-orient based on the EXIF `Orientation` tag, if present. + * Mirroring is supported and may infer the use of a flip operation. + * + * Using this option will remove the EXIF `Orientation` tag, if any. + */ + autoOrient?: boolean | undefined; + /** + * When to abort processing of invalid pixel data, one of (in order of sensitivity): + * 'none' (least), 'truncated', 'error' or 'warning' (most), highers level imply lower levels, invalid metadata will always abort. (optional, default 'warning') + */ + failOn?: FailOnOptions | undefined; + /** + * 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) + * + * @deprecated Use `failOn` instead + */ + failOnError?: boolean | undefined; + /** + * 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) + */ + limitInputPixels?: number | boolean | undefined; + /** Set this to true to remove safety features that help prevent memory exhaustion (SVG, PNG). (optional, default false) */ + unlimited?: boolean | undefined; + /** Set this to false to use random access rather than sequential read. Some operations will do this automatically. */ + sequentialRead?: boolean | undefined; + /** Number representing the DPI for vector images in the range 1 to 100000. (optional, default 72) */ + density?: number | undefined; + /** Should the embedded ICC profile, if any, be ignored. */ + ignoreIcc?: boolean | undefined; + /** Number of pages to extract for multi-page input (GIF, TIFF, PDF), use -1 for all pages */ + pages?: number | undefined; + /** Page number to start extracting from for multi-page input (GIF, TIFF, PDF), zero based. (optional, default 0) */ + page?: number | undefined; + /** TIFF specific input options */ + tiff?: TiffInputOptions | undefined; + /** SVG specific input options */ + svg?: SvgInputOptions | undefined; + /** PDF specific input options */ + pdf?: PdfInputOptions | undefined; + /** OpenSlide specific input options */ + openSlide?: OpenSlideInputOptions | undefined; + /** JPEG 2000 specific input options */ + jp2?: Jp2InputOptions | undefined; + /** @deprecated Use {@link SharpOptions.tiff} instead */ + subifd?: number | undefined; + /** @deprecated Use {@link SharpOptions.pdf} instead */ + pdfBackground?: Colour | Color | undefined; + /** @deprecated Use {@link SharpOptions.openSlide} instead */ + level?: number | undefined; + /** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default false) */ + animated?: boolean | undefined; + /** Describes raw pixel input image data. See raw() for pixel ordering. */ + raw?: CreateRaw | undefined; + /** Describes a new image to be created. */ + create?: Create | undefined; + /** Describes a new text image to be created. */ + text?: CreateText | undefined; + /** Describes how array of input images should be joined. */ + join?: Join | undefined; + } + + interface CacheOptions { + /** Is the maximum memory in MB to use for this cache (optional, default 50) */ + memory?: number | undefined; + /** Is the maximum number of files to hold open (optional, default 20) */ + files?: number | undefined; + /** Is the maximum number of operations to cache (optional, default 100) */ + items?: number | undefined; + } + + interface TimeoutOptions { + /** Number of seconds after which processing will be stopped (default 0, eg disabled) */ + seconds: number; + } + + interface SharpCounters { + /** The number of tasks this module has queued waiting for libuv to provide a worker thread from its pool. */ + queue: number; + /** The number of resize tasks currently being processed. */ + process: number; + } + + interface Raw { + width: number; + height: number; + channels: Channels; + } + + interface CreateRaw extends Raw { + /** Specifies that the raw input has already been premultiplied, set to true to avoid sharp premultiplying the image. (optional, default false) */ + premultiplied?: boolean | undefined; + /** The height of each page/frame for animated images, must be an integral factor of the overall image height. */ + pageHeight?: number | undefined; + } + + type CreateChannels = 3 | 4; + + interface Create { + /** Number of pixels wide. */ + width: number; + /** Number of pixels high. */ + height: number; + /** Number of bands, 3 for RGB, 4 for RGBA */ + channels: CreateChannels; + /** Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. */ + background: Colour | Color; + /** Describes a noise to be created. */ + noise?: Noise | undefined; + /** The height of each page/frame for animated images, must be an integral factor of the overall image height. */ + pageHeight?: number | undefined; + + } + + interface CreateText { + /** Text to render as a UTF-8 string. It can contain Pango markup, for example `<i>Le</i>Monde`. */ + text: string; + /** Font name to render with. */ + font?: string; + /** Absolute filesystem path to a font file that can be used by `font`. */ + fontfile?: string; + /** Integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. (optional, default `0`) */ + width?: number; + /** + * Integral number of pixels high. When defined, `dpi` will be ignored and the text will automatically fit the pixel resolution + * defined by `width` and `height`. Will be ignored if `width` is not specified or set to 0. (optional, default `0`) + */ + height?: number; + /** Text alignment ('left', 'centre', 'center', 'right'). (optional, default 'left') */ + align?: TextAlign; + /** Set this to true to apply justification to the text. (optional, default `false`) */ + justify?: boolean; + /** The resolution (size) at which to render the text. Does not take effect if `height` is specified. (optional, default `72`) */ + dpi?: number; + /** + * Set this to true to enable RGBA output. This is useful for colour emoji rendering, + * or support for pango markup features like `<span foreground="red">Red!</span>`. (optional, default `false`) + */ + rgba?: boolean; + /** Text line height in points. Will use the font line height if none is specified. (optional, default `0`) */ + spacing?: number; + /** Word wrapping style when width is provided, one of: 'word', 'char', 'word-char' (prefer word, fallback to char) or 'none' */ + wrap?: TextWrap; + } + + interface Join { + /** Number of images per row. */ + across?: number | undefined; + /** Treat input as frames of an animated image. */ + animated?: boolean | undefined; + /** Space between images, in pixels. */ + shim?: number | undefined; + /** Background colour. */ + background?: Colour | Color | undefined; + /** Horizontal alignment. */ + halign?: HorizontalAlignment | undefined; + /** Vertical alignment. */ + valign?: VerticalAlignment | undefined; + } + + interface TiffInputOptions { + /** Sub Image File Directory to extract, defaults to main image. Use -1 for all subifds. */ + subifd?: number | undefined; + } + + interface SvgInputOptions { + /** Custom CSS for SVG input, applied with a User Origin during the CSS cascade. */ + stylesheet?: string | undefined; + /** Set to `true` to render SVG input at 32-bits per channel (128-bit) instead of 8-bits per channel (32-bit) RGBA. */ + highBitdepth?: boolean | undefined; + } + + interface PdfInputOptions { + /** Background colour to use when PDF is partially transparent. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick. */ + background?: Colour | Color | undefined; + } + + interface OpenSlideInputOptions { + /** Level to extract from a multi-level input, zero based. (optional, default 0) */ + level?: number | undefined; + } + + interface Jp2InputOptions { + /** Set to `true` to load JPEG 2000 images using [oneshot mode](https://github.com/libvips/libvips/issues/4205) */ + oneshot?: boolean | undefined; + } + + interface ExifDir { + [k: string]: string; + } + + interface Exif { + 'IFD0'?: ExifDir; + 'IFD1'?: ExifDir; + 'IFD2'?: ExifDir; + 'IFD3'?: ExifDir; + } + + type HeifCompression = 'av1' | 'hevc'; + + type Unit = 'inch' | 'cm'; + + interface WriteableMetadata { + /** Number of pixels per inch (DPI) */ + density?: number | undefined; + /** Value between 1 and 8, used to update the EXIF Orientation tag. */ + orientation?: number | undefined; + /** + * Filesystem path to output ICC profile, defaults to sRGB. + * @deprecated Use `withIccProfile()` instead. + */ + icc?: string | undefined; + /** + * Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. + * @deprecated Use `withExif()` or `withExifMerge()` instead. + */ + exif?: Exif | undefined; + } + + interface Metadata { + /** Number value of the EXIF Orientation header, if present */ + orientation?: number | undefined; + /** Name of decoder used to decompress image data e.g. jpeg, png, webp, gif, svg */ + format: keyof FormatEnum; + /** Total size of image in bytes, for Stream and Buffer input only */ + size?: number | undefined; + /** Number of pixels wide (EXIF orientation is not taken into consideration) */ + width: number; + /** Number of pixels high (EXIF orientation is not taken into consideration) */ + height: number; + /** Any changed metadata after the image orientation is applied. */ + autoOrient: { + /** Number of pixels wide (EXIF orientation is taken into consideration) */ + width: number; + /** Number of pixels high (EXIF orientation is taken into consideration) */ + height: number; + }; + /** Name of colour space interpretation */ + space: keyof ColourspaceEnum; + /** Number of bands e.g. 3 for sRGB, 4 for CMYK */ + channels: Channels; + /** Name of pixel depth format e.g. uchar, char, ushort, float ... */ + depth: keyof DepthEnum; + /** Number of pixels per inch (DPI), if present */ + density?: number | undefined; + /** String containing JPEG chroma subsampling, 4:2:0 or 4:4:4 for RGB, 4:2:0:4 or 4:4:4:4 for CMYK */ + chromaSubsampling?: string | undefined; + /** Boolean indicating whether the image is interlaced using a progressive scan */ + isProgressive: boolean; + /** Boolean indicating whether the image is palette-based (GIF, PNG). */ + isPalette: boolean; + /** Number of bits per sample for each channel (GIF, PNG). */ + bitsPerSample?: number | undefined; + /** Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP */ + pages?: number | undefined; + /** Number of pixels high each page in a multi-page image will be. */ + pageHeight?: number | undefined; + /** Number of times to loop an animated image, zero refers to a continuous loop. */ + loop?: number | undefined; + /** Delay in ms between each page in an animated image, provided as an array of integers. */ + delay?: number[] | undefined; + /** Number of the primary page in a HEIF image */ + pagePrimary?: number | undefined; + /** Boolean indicating the presence of an embedded ICC profile */ + hasProfile: boolean; + /** Boolean indicating the presence of an alpha transparency channel */ + hasAlpha: boolean; + /** Buffer containing raw EXIF data, if present */ + exif?: Buffer | undefined; + /** Buffer containing raw ICC profile data, if present */ + icc?: Buffer | undefined; + /** Buffer containing raw IPTC data, if present */ + iptc?: Buffer | undefined; + /** Buffer containing raw XMP data, if present */ + xmp?: Buffer | undefined; + /** String containing XMP data, if valid UTF-8 */ + xmpAsString?: string | undefined; + /** Buffer containing raw TIFFTAG_PHOTOSHOP data, if present */ + tifftagPhotoshop?: Buffer | undefined; + /** The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) */ + compression?: HeifCompression | undefined; + /** Default background colour, if present, for PNG (bKGD) and GIF images */ + background?: { r: number; g: number; b: number } | { gray: number }; + /** Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide */ + levels?: LevelMetadata[] | undefined; + /** Number of Sub Image File Directories in an OME-TIFF image */ + subifds?: number | undefined; + /** The unit of resolution (density) */ + resolutionUnit?: Unit | undefined; + /** String containing format for images loaded via *magick */ + formatMagick?: string | undefined; + /** Array of keyword/text pairs representing PNG text blocks, if present. */ + comments?: CommentsMetadata[] | undefined; + } + + interface LevelMetadata { + width: number; + height: number; + } + + interface CommentsMetadata { + keyword: string; + text: string; + } + + interface Stats { + /** Array of channel statistics for each channel in the image. */ + channels: ChannelStats[]; + /** Value to identify if the image is opaque or transparent, based on the presence and use of alpha channel */ + isOpaque: boolean; + /** Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental) */ + entropy: number; + /** Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any (experimental) */ + sharpness: number; + /** Object containing most dominant sRGB colour based on a 4096-bin 3D histogram (experimental) */ + dominant: { r: number; g: number; b: number }; + } + + interface ChannelStats { + /** minimum value in the channel */ + min: number; + /** maximum value in the channel */ + max: number; + /** sum of all values in a channel */ + sum: number; + /** sum of squared values in a channel */ + squaresSum: number; + /** mean of the values in a channel */ + mean: number; + /** standard deviation for the values in a channel */ + stdev: number; + /** x-coordinate of one of the pixel where the minimum lies */ + minX: number; + /** y-coordinate of one of the pixel where the minimum lies */ + minY: number; + /** x-coordinate of one of the pixel where the maximum lies */ + maxX: number; + /** y-coordinate of one of the pixel where the maximum lies */ + maxY: number; + } + + interface OutputOptions { + /** Force format output, otherwise attempt to use input format (optional, default true) */ + force?: boolean | undefined; + } + + interface WithIccProfileOptions { + /** Should the ICC profile be included in the output image metadata? (optional, default true) */ + attach?: boolean | undefined; + } + + interface JpegOptions extends OutputOptions { + /** Quality, integer 1-100 (optional, default 80) */ + quality?: number | undefined; + /** Use progressive (interlace) scan (optional, default false) */ + progressive?: boolean | undefined; + /** Set to '4:4:4' to prevent chroma subsampling when quality <= 90 (optional, default '4:2:0') */ + chromaSubsampling?: string | undefined; + /** Apply trellis quantisation (optional, default false) */ + trellisQuantisation?: boolean | undefined; + /** Apply overshoot deringing (optional, default false) */ + overshootDeringing?: boolean | undefined; + /** Optimise progressive scans, forces progressive (optional, default false) */ + optimiseScans?: boolean | undefined; + /** Alternative spelling of optimiseScans (optional, default false) */ + optimizeScans?: boolean | undefined; + /** Optimise Huffman coding tables (optional, default true) */ + optimiseCoding?: boolean | undefined; + /** Alternative spelling of optimiseCoding (optional, default true) */ + optimizeCoding?: boolean | undefined; + /** Quantization table to use, integer 0-8 (optional, default 0) */ + quantisationTable?: number | undefined; + /** Alternative spelling of quantisationTable (optional, default 0) */ + quantizationTable?: number | undefined; + /** Use mozjpeg defaults (optional, default false) */ + mozjpeg?: boolean | undefined; + } + + interface Jp2Options extends OutputOptions { + /** Quality, integer 1-100 (optional, default 80) */ + quality?: number; + /** Use lossless compression mode (optional, default false) */ + lossless?: boolean; + /** Horizontal tile size (optional, default 512) */ + tileWidth?: number; + /** Vertical tile size (optional, default 512) */ + tileHeight?: number; + /** Set to '4:2:0' to enable chroma subsampling (optional, default '4:4:4') */ + chromaSubsampling?: '4:4:4' | '4:2:0'; + } + + interface JxlOptions extends OutputOptions { + /** Maximum encoding error, between 0 (highest quality) and 15 (lowest quality) (optional, default 1.0) */ + distance?: number; + /** Calculate distance based on JPEG-like quality, between 1 and 100, overrides distance if specified */ + quality?: number; + /** Target decode speed tier, between 0 (highest quality) and 4 (lowest quality) (optional, default 0) */ + decodingTier?: number; + /** Use lossless compression (optional, default false) */ + lossless?: boolean; + /** CPU effort, between 3 (fastest) and 9 (slowest) (optional, default 7) */ + effort?: number | undefined; + } + + interface WebpOptions extends OutputOptions, AnimationOptions { + /** Quality, integer 1-100 (optional, default 80) */ + quality?: number | undefined; + /** Quality of alpha layer, number from 0-100 (optional, default 100) */ + alphaQuality?: number | undefined; + /** Use lossless compression mode (optional, default false) */ + lossless?: boolean | undefined; + /** Use near_lossless compression mode (optional, default false) */ + nearLossless?: boolean | undefined; + /** Use high quality chroma subsampling (optional, default false) */ + smartSubsample?: boolean | undefined; + /** Auto-adjust the deblocking filter, slow but can improve low contrast edges (optional, default false) */ + smartDeblock?: boolean | undefined; + /** Level of CPU effort to reduce file size, integer 0-6 (optional, default 4) */ + effort?: number | undefined; + /** Prevent use of animation key frames to minimise file size (slow) (optional, default false) */ + minSize?: boolean; + /** Allow mixture of lossy and lossless animation frames (slow) (optional, default false) */ + mixed?: boolean; + /** Preset options: one of default, photo, picture, drawing, icon, text (optional, default 'default') */ + preset?: keyof PresetEnum | undefined; + } + + interface AvifOptions extends OutputOptions { + /** quality, integer 1-100 (optional, default 50) */ + quality?: number | undefined; + /** use lossless compression (optional, default false) */ + lossless?: boolean | undefined; + /** Level of CPU effort to reduce file size, between 0 (fastest) and 9 (slowest) (optional, default 4) */ + effort?: number | undefined; + /** set to '4:2:0' to use chroma subsampling, requires libvips v8.11.0 (optional, default '4:4:4') */ + chromaSubsampling?: string | undefined; + /** Set bitdepth to 8, 10 or 12 bit (optional, default 8) */ + bitdepth?: 8 | 10 | 12 | undefined; + } + + interface HeifOptions extends OutputOptions { + /** quality, integer 1-100 (optional, default 50) */ + quality?: number | undefined; + /** compression format: av1, hevc (optional, default 'av1') */ + compression?: HeifCompression | undefined; + /** use lossless compression (optional, default false) */ + lossless?: boolean | undefined; + /** Level of CPU effort to reduce file size, between 0 (fastest) and 9 (slowest) (optional, default 4) */ + effort?: number | undefined; + /** set to '4:2:0' to use chroma subsampling (optional, default '4:4:4') */ + chromaSubsampling?: string | undefined; + /** Set bitdepth to 8, 10 or 12 bit (optional, default 8) */ + bitdepth?: 8 | 10 | 12 | undefined; + } + + interface GifOptions extends OutputOptions, AnimationOptions { + /** Re-use existing palette, otherwise generate new (slow) */ + reuse?: boolean | undefined; + /** Use progressive (interlace) scan */ + progressive?: boolean | undefined; + /** Maximum number of palette entries, including transparency, between 2 and 256 (optional, default 256) */ + colours?: number | undefined; + /** Alternative spelling of "colours". Maximum number of palette entries, including transparency, between 2 and 256 (optional, default 256) */ + colors?: number | undefined; + /** Level of CPU effort to reduce file size, between 1 (fastest) and 10 (slowest) (optional, default 7) */ + effort?: number | undefined; + /** Level of Floyd-Steinberg error diffusion, between 0 (least) and 1 (most) (optional, default 1.0) */ + dither?: number | undefined; + /** Maximum inter-frame error for transparency, between 0 (lossless) and 32 (optional, default 0) */ + interFrameMaxError?: number | undefined; + /** Maximum inter-palette error for palette reuse, between 0 and 256 (optional, default 3) */ + interPaletteMaxError?: number | undefined; + /** Keep duplicate frames in the output instead of combining them (optional, default false) */ + keepDuplicateFrames?: boolean | undefined; + } + + interface TiffOptions extends OutputOptions { + /** Quality, integer 1-100 (optional, default 80) */ + quality?: number | undefined; + /** Compression options: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k (optional, default 'jpeg') */ + compression?: string | undefined; + /** Use BigTIFF variant (has no effect when compression is none) (optional, default false) */ + bigtiff?: boolean | undefined; + /** Compression predictor options: none, horizontal, float (optional, default 'horizontal') */ + predictor?: string | undefined; + /** Write an image pyramid (optional, default false) */ + pyramid?: boolean | undefined; + /** Write a tiled tiff (optional, default false) */ + tile?: boolean | undefined; + /** Horizontal tile size (optional, default 256) */ + tileWidth?: number | undefined; + /** Vertical tile size (optional, default 256) */ + tileHeight?: number | undefined; + /** Horizontal resolution in pixels/mm (optional, default 1.0) */ + xres?: number | undefined; + /** Vertical resolution in pixels/mm (optional, default 1.0) */ + yres?: number | undefined; + /** Reduce bitdepth to 1, 2 or 4 bit (optional, default 8) */ + bitdepth?: 1 | 2 | 4 | 8 | undefined; + /** Write 1-bit images as miniswhite (optional, default false) */ + miniswhite?: boolean | undefined; + /** Resolution unit options: inch, cm (optional, default 'inch') */ + resolutionUnit?: Unit | undefined; + } + + interface PngOptions extends OutputOptions { + /** Use progressive (interlace) scan (optional, default false) */ + progressive?: boolean | undefined; + /** zlib compression level, 0-9 (optional, default 6) */ + compressionLevel?: number | undefined; + /** Use adaptive row filtering (optional, default false) */ + adaptiveFiltering?: boolean | undefined; + /** Use the lowest number of colours needed to achieve given quality (optional, default `100`) */ + quality?: number | undefined; + /** Level of CPU effort to reduce file size, between 1 (fastest) and 10 (slowest), sets palette to true (optional, default 7) */ + effort?: number | undefined; + /** Quantise to a palette-based image with alpha transparency support (optional, default false) */ + palette?: boolean | undefined; + /** Maximum number of palette entries (optional, default 256) */ + colours?: number | undefined; + /** Alternative Spelling of "colours". Maximum number of palette entries (optional, default 256) */ + colors?: number | undefined; + /** Level of Floyd-Steinberg error diffusion (optional, default 1.0) */ + dither?: number | undefined; + } + + interface RotateOptions { + /** parsed by the color module to extract values for red, green, blue and alpha. (optional, default "#000000") */ + background?: Colour | Color | undefined; + } + + type Precision = 'integer' | 'float' | 'approximate'; + + interface BlurOptions { + /** A value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2` */ + sigma: number; + /** A value between 0.001 and 1. A smaller value will generate a larger, more accurate mask. */ + minAmplitude?: number; + /** How accurate the operation should be, one of: integer, float, approximate. (optional, default "integer") */ + precision?: Precision | undefined; + } + + interface FlattenOptions { + /** background colour, parsed by the color module, defaults to black. (optional, default {r:0,g:0,b:0}) */ + background?: Colour | Color | undefined; + } + + interface NegateOptions { + /** whether or not to negate any alpha channel. (optional, default true) */ + alpha?: boolean | undefined; + } + + interface NormaliseOptions { + /** Percentile below which luminance values will be underexposed. */ + lower?: number | undefined; + /** Percentile above which luminance values will be overexposed. */ + upper?: number | undefined; + } + + interface ResizeOptions { + /** Alternative means of specifying width. If both are present this takes priority. */ + width?: number | undefined; + /** Alternative means of specifying height. If both are present this takes priority. */ + height?: number | undefined; + /** How the image should be resized to fit both provided dimensions, one of cover, contain, fill, inside or outside. (optional, default 'cover') */ + fit?: keyof FitEnum | undefined; + /** Position, gravity or strategy to use when fit is cover or contain. (optional, default 'centre') */ + position?: number | string | undefined; + /** Background colour when using a fit of contain, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */ + background?: Colour | Color | undefined; + /** The kernel to use for image reduction. (optional, default 'lanczos3') */ + kernel?: keyof KernelEnum | undefined; + /** Do not enlarge if the width or height are already less than the specified dimensions, equivalent to GraphicsMagick's > geometry option. (optional, default false) */ + withoutEnlargement?: boolean | undefined; + /** Do not reduce if the width or height are already greater than the specified dimensions, equivalent to GraphicsMagick's < geometry option. (optional, default false) */ + withoutReduction?: boolean | undefined; + /** Take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default true) */ + fastShrinkOnLoad?: boolean | undefined; + } + + interface Region { + /** zero-indexed offset from left edge */ + left: number; + /** zero-indexed offset from top edge */ + top: number; + /** dimension of extracted image */ + width: number; + /** dimension of extracted image */ + height: number; + } + + interface Noise { + /** type of generated noise, currently only gaussian is supported. */ + type: 'gaussian'; + /** mean of pixels in generated noise. */ + mean?: number | undefined; + /** standard deviation of pixels in generated noise. */ + sigma?: number | undefined; + } + + type ExtendWith = 'background' | 'copy' | 'repeat' | 'mirror'; + + interface ExtendOptions { + /** single pixel count to top edge (optional, default 0) */ + top?: number | undefined; + /** single pixel count to left edge (optional, default 0) */ + left?: number | undefined; + /** single pixel count to bottom edge (optional, default 0) */ + bottom?: number | undefined; + /** single pixel count to right edge (optional, default 0) */ + right?: number | undefined; + /** background colour, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */ + background?: Colour | Color | undefined; + /** how the extension is done, one of: "background", "copy", "repeat", "mirror" (optional, default `'background'`) */ + extendWith?: ExtendWith | undefined; + } + + interface TrimOptions { + /** Background colour, parsed by the color module, defaults to that of the top-left pixel. (optional) */ + background?: Colour | Color | undefined; + /** Allowed difference from the above colour, a positive number. (optional, default 10) */ + threshold?: number | undefined; + /** Does the input more closely resemble line art (e.g. vector) rather than being photographic? (optional, default false) */ + lineArt?: boolean | undefined; + } + + interface RawOptions { + depth?: keyof DepthEnum; + } + + /** 1 for grayscale, 2 for grayscale + alpha, 3 for sRGB, 4 for CMYK or RGBA */ + type Channels = 1 | 2 | 3 | 4; + + interface RGBA { + r?: number | undefined; + g?: number | undefined; + b?: number | undefined; + alpha?: number | undefined; + } + + type Colour = string | RGBA; + type Color = Colour; + + interface Kernel { + /** width of the kernel in pixels. */ + width: number; + /** height of the kernel in pixels. */ + height: number; + /** Array of length width*height containing the kernel values. */ + kernel: ArrayLike<number>; + /** the scale of the kernel in pixels. (optional, default sum) */ + scale?: number | undefined; + /** the offset of the kernel in pixels. (optional, default 0) */ + offset?: number | undefined; + } + + interface ClaheOptions { + /** width of the region */ + width: number; + /** height of the region */ + height: number; + /** max slope of the cumulative contrast. A value of 0 disables contrast limiting. Valid values are integers in the range 0-100 (inclusive) (optional, default 3) */ + maxSlope?: number | undefined; + } + + interface ThresholdOptions { + /** convert to single channel greyscale. (optional, default true) */ + greyscale?: boolean | undefined; + /** alternative spelling for greyscale. (optional, default true) */ + grayscale?: boolean | undefined; + } + + interface OverlayOptions extends SharpOptions { + /** Buffer containing image data, String containing the path to an image file, or Create object */ + input?: string | Buffer | { create: Create } | { text: CreateText } | { raw: CreateRaw } | undefined; + /** how to blend this image with the image below. (optional, default `'over'`) */ + blend?: Blend | undefined; + /** gravity at which to place the overlay. (optional, default 'centre') */ + gravity?: Gravity | undefined; + /** the pixel offset from the top edge. */ + top?: number | undefined; + /** the pixel offset from the left edge. */ + left?: number | undefined; + /** set to true to repeat the overlay image across the entire image with the given gravity. (optional, default false) */ + tile?: boolean | undefined; + /** Set to true to avoid premultipling the image below. Equivalent to the --premultiplied vips option. */ + premultiplied?: boolean | undefined; + /** number representing the DPI for vector overlay image. (optional, default 72)*/ + density?: number | undefined; + /** Set to true to read all frames/pages of an animated image. (optional, default false) */ + animated?: boolean | undefined; + /** see sharp() constructor, (optional, default 'warning') */ + failOn?: FailOnOptions | undefined; + /** see sharp() constructor, (optional, default 268402689) */ + limitInputPixels?: number | boolean | undefined; + /** see sharp() constructor, (optional, default false) */ + autoOrient?: boolean | undefined; + } + + interface TileOptions { + /** Tile size in pixels, a value between 1 and 8192. (optional, default 256) */ + size?: number | undefined; + /** Tile overlap in pixels, a value between 0 and 8192. (optional, default 0) */ + overlap?: number | undefined; + /** Tile angle of rotation, must be a multiple of 90. (optional, default 0) */ + angle?: number | undefined; + /** background colour, parsed by the color module, defaults to white without transparency. (optional, default {r:255,g:255,b:255,alpha:1}) */ + background?: string | RGBA | undefined; + /** How deep to make the pyramid, possible values are "onepixel", "onetile" or "one" (default based on layout) */ + depth?: string | undefined; + /** Threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images */ + skipBlanks?: number | undefined; + /** Tile container, with value fs (filesystem) or zip (compressed file). (optional, default 'fs') */ + container?: TileContainer | undefined; + /** Filesystem layout, possible values are dz, iiif, iiif3, zoomify or google. (optional, default 'dz') */ + layout?: TileLayout | undefined; + /** Centre image in tile. (optional, default false) */ + centre?: boolean | undefined; + /** Alternative spelling of centre. (optional, default false) */ + center?: boolean | undefined; + /** When layout is iiif/iiif3, sets the @id/id attribute of info.json (optional, default 'https://example.com/iiif') */ + id?: string | undefined; + /** The name of the directory within the zip file when container is `zip`. */ + basename?: string | undefined; + } + + interface AnimationOptions { + /** Number of animation iterations, a value between 0 and 65535. Use 0 for infinite animation. (optional, default 0) */ + loop?: number | undefined; + /** delay(s) between animation frames (in milliseconds), each value between 0 and 65535. (optional) */ + delay?: number | number[] | undefined; + } + + interface SharpenOptions { + /** The sigma of the Gaussian mask, where sigma = 1 + radius / 2, between 0.000001 and 10000 */ + sigma: number; + /** The level of sharpening to apply to "flat" areas, between 0 and 1000000 (optional, default 1.0) */ + m1?: number | undefined; + /** The level of sharpening to apply to "jagged" areas, between 0 and 1000000 (optional, default 2.0) */ + m2?: number | undefined; + /** Threshold between "flat" and "jagged", between 0 and 1000000 (optional, default 2.0) */ + x1?: number | undefined; + /** Maximum amount of brightening, between 0 and 1000000 (optional, default 10.0) */ + y2?: number | undefined; + /** Maximum amount of darkening, between 0 and 1000000 (optional, default 20.0) */ + y3?: number | undefined; + } + + interface AffineOptions { + /** Parsed by the color module to extract values for red, green, blue and alpha. (optional, default "#000000") */ + background?: string | object | undefined; + /** Input horizontal offset (optional, default 0) */ + idx?: number | undefined; + /** Input vertical offset (optional, default 0) */ + idy?: number | undefined; + /** Output horizontal offset (optional, default 0) */ + odx?: number | undefined; + /** Output horizontal offset (optional, default 0) */ + ody?: number | undefined; + /** Interpolator (optional, default sharp.interpolators.bicubic) */ + interpolator?: Interpolators[keyof Interpolators] | undefined; + } + + interface OutputInfo { + format: string; + size: number; + width: number; + height: number; + channels: Channels; + /** indicating if premultiplication was used */ + premultiplied: boolean; + /** Only defined when using a crop strategy */ + cropOffsetLeft?: number | undefined; + /** Only defined when using a crop strategy */ + cropOffsetTop?: number | undefined; + /** Only defined when using a trim method */ + trimOffsetLeft?: number | undefined; + /** Only defined when using a trim method */ + trimOffsetTop?: number | undefined; + /** DPI the font was rendered at, only defined when using `text` input */ + textAutofitDpi?: number | undefined; + /** When using the attention crop strategy, the focal point of the cropped region */ + attentionX?: number | undefined; + attentionY?: number | undefined; + /** Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP */ + pages?: number | undefined; + /** Number of pixels high each page in a multi-page image will be. */ + pageHeight?: number | undefined; + } + + interface AvailableFormatInfo { + id: string; + input: { file: boolean; buffer: boolean; stream: boolean; fileSuffix?: string[] }; + output: { file: boolean; buffer: boolean; stream: boolean; alias?: string[] }; + } + + interface FitEnum { + contain: 'contain'; + cover: 'cover'; + fill: 'fill'; + inside: 'inside'; + outside: 'outside'; + } + + interface KernelEnum { + nearest: 'nearest'; + cubic: 'cubic'; + linear: 'linear'; + mitchell: 'mitchell'; + lanczos2: 'lanczos2'; + lanczos3: 'lanczos3'; + mks2013: 'mks2013'; + mks2021: 'mks2021'; + } + + interface PresetEnum { + default: 'default'; + picture: 'picture'; + photo: 'photo'; + drawing: 'drawing'; + icon: 'icon'; + text: 'text'; + } + + interface BoolEnum { + and: 'and'; + or: 'or'; + eor: 'eor'; + } + + interface ColourspaceEnum { + 'b-w': string; + cmc: string; + cmyk: string; + fourier: string; + grey16: string; + histogram: string; + hsv: string; + lab: string; + labq: string; + labs: string; + lch: string; + matrix: string; + multiband: string; + rgb: string; + rgb16: string; + scrgb: string; + srgb: string; + xyz: string; + yxy: string; + } + + interface DepthEnum { + char: string; + complex: string; + double: string; + dpcomplex: string; + float: string; + int: string; + short: string; + uchar: string; + uint: string; + ushort: string; + } + + type FailOnOptions = 'none' | 'truncated' | 'error' | 'warning'; + + type TextAlign = 'left' | 'centre' | 'center' | 'right'; + + type TextWrap = 'word' | 'char' | 'word-char' | 'none'; + + type HorizontalAlignment = 'left' | 'centre' | 'center' | 'right'; + + type VerticalAlignment = 'top' | 'centre' | 'center' | 'bottom'; + + type TileContainer = 'fs' | 'zip'; + + type TileLayout = 'dz' | 'iiif' | 'iiif3' | 'zoomify' | 'google'; + + type Blend = + | 'clear' + | 'source' + | 'over' + | 'in' + | 'out' + | 'atop' + | 'dest' + | 'dest-over' + | 'dest-in' + | 'dest-out' + | 'dest-atop' + | 'xor' + | 'add' + | 'saturate' + | 'multiply' + | 'screen' + | 'overlay' + | 'darken' + | 'lighten' + | 'color-dodge' + | 'colour-dodge' + | 'color-burn' + | 'colour-burn' + | 'hard-light' + | 'soft-light' + | 'difference' + | 'exclusion'; + + type Gravity = number | string; + + interface GravityEnum { + north: number; + northeast: number; + southeast: number; + south: number; + southwest: number; + west: number; + northwest: number; + east: number; + center: number; + centre: number; + } + + interface StrategyEnum { + entropy: number; + attention: number; + } + + interface FormatEnum { + avif: AvailableFormatInfo; + dcraw: AvailableFormatInfo; + dz: AvailableFormatInfo; + exr: AvailableFormatInfo; + fits: AvailableFormatInfo; + gif: AvailableFormatInfo; + heif: AvailableFormatInfo; + input: AvailableFormatInfo; + jpeg: AvailableFormatInfo; + jpg: AvailableFormatInfo; + jp2: AvailableFormatInfo; + jxl: AvailableFormatInfo; + magick: AvailableFormatInfo; + openslide: AvailableFormatInfo; + pdf: AvailableFormatInfo; + png: AvailableFormatInfo; + ppm: AvailableFormatInfo; + rad: AvailableFormatInfo; + raw: AvailableFormatInfo; + svg: AvailableFormatInfo; + tiff: AvailableFormatInfo; + tif: AvailableFormatInfo; + v: AvailableFormatInfo; + webp: AvailableFormatInfo; + } + + interface CacheResult { + memory: { current: number; high: number; max: number }; + files: { current: number; max: number }; + items: { current: number; max: number }; + } + + interface 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/libvips/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/libvips/libvips/blob/master/libvips/resample/vsqbs.cpp#L48). Prevents "staircasing" when enlarging. */ + vertexSplitQuadraticBasisSpline: 'vsqbs'; + } + + type Matrix2x2 = [[number, number], [number, number]]; + type Matrix3x3 = [[number, number, number], [number, number, number], [number, number, number]]; + type Matrix4x4 = [[number, number, number, number], [number, number, number, number], [number, number, number, number], [number, number, number, number]]; +} + +export = sharp; diff --git a/lib/index.js b/lib/index.js index 56fa29423..b80191d71 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,7 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const Sharp = require('./constructor'); require('./input')(Sharp); diff --git a/lib/input.js b/lib/input.js index a81036d66..48388a1d2 100644 --- a/lib/input.js +++ b/lib/input.js @@ -1,17 +1,48 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const color = require('color'); const is = require('./is'); -const sharp = require('../build/Release/sharp.node'); +const sharp = require('./sharp'); + +/** + * Justification alignment + * @member + * @private + */ +const align = { + left: 'low', + top: 'low', + low: 'low', + center: 'centre', + centre: 'centre', + right: 'high', + bottom: 'high', + high: 'high' +}; + +const inputStreamParameters = [ + // Limits and error handling + 'failOn', 'limitInputPixels', 'unlimited', + // Format-generic + 'animated', 'autoOrient', 'density', 'ignoreIcc', 'page', 'pages', 'sequentialRead', + // Format-specific + 'jp2', 'openSlide', 'pdf', 'raw', 'svg', 'tiff', + // Deprecated + 'failOnError', 'openSlideLevel', 'pdfBackground', 'tiffSubifd' +]; /** * Extract input options, if any, from an object. * @private */ function _inputOptionsFromObject (obj) { - 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 } + const params = inputStreamParameters + .filter(p => is.defined(obj[p])) + .map(p => ([p, obj[p]])); + return params.length + ? Object.fromEntries(params) : undefined; } @@ -21,19 +52,32 @@ function _inputOptionsFromObject (obj) { */ function _createInputDescriptor (input, inputOptions, containerOptions) { const inputDescriptor = { - failOnError: true, - limitInputPixels: Math.pow(0x3FFF, 2), - sequentialRead: false + autoOrient: false, + failOn: 'warning', + limitInputPixels: 0x3FFF ** 2, + ignoreIcc: false, + unlimited: false, + sequentialRead: true }; if (is.string(input)) { // filesystem inputDescriptor.file = input; } else if (is.buffer(input)) { // Buffer + if (input.length === 0) { + throw Error('Input Buffer is empty'); + } inputDescriptor.buffer = input; - } else if (is.uint8Array(input)) { - // Uint8Array or Uint8ClampedArray - inputDescriptor.buffer = Buffer.from(input.buffer); + } else if (is.arrayBuffer(input)) { + if (input.byteLength === 0) { + throw Error('Input bit Array is empty'); + } + inputDescriptor.buffer = Buffer.from(input, 0, input.byteLength); + } else if (is.typedArray(input)) { + if (input.length === 0) { + throw Error('Input Bit Array is empty'); + } + inputDescriptor.buffer = Buffer.from(input.buffer, input.byteOffset, input.byteLength); } else if (is.plainObject(input) && !is.defined(inputOptions)) { // Plain Object descriptor, e.g. create inputOptions = input; @@ -44,20 +88,48 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { } else if (!is.defined(input) && !is.defined(inputOptions) && is.object(containerOptions) && containerOptions.allowStream) { // Stream without options inputDescriptor.buffer = []; + } else if (Array.isArray(input)) { + if (input.length > 1) { + // Join images together + if (!this.options.joining) { + this.options.joining = true; + this.options.join = input.map(i => this._createInputDescriptor(i)); + } else { + throw new Error('Recursive join is unsupported'); + } + } else { + throw new Error('Expected at least two images to join'); + } } else { throw new Error(`Unsupported input '${input}' of type ${typeof input}${ is.defined(inputOptions) ? ` when also providing options of type ${typeof inputOptions}` : '' }`); } if (is.object(inputOptions)) { - // Fail on error + // Deprecated: failOnError if (is.defined(inputOptions.failOnError)) { if (is.bool(inputOptions.failOnError)) { - inputDescriptor.failOnError = inputOptions.failOnError; + inputDescriptor.failOn = inputOptions.failOnError ? 'warning' : 'none'; } else { throw is.invalidParameterError('failOnError', 'boolean', inputOptions.failOnError); } } + // failOn + if (is.defined(inputOptions.failOn)) { + if (is.string(inputOptions.failOn) && is.inArray(inputOptions.failOn, ['none', 'truncated', 'error', 'warning'])) { + inputDescriptor.failOn = inputOptions.failOn; + } else { + throw is.invalidParameterError('failOn', 'one of: none, truncated, error, warning', inputOptions.failOn); + } + } + // autoOrient + if (is.defined(inputOptions.autoOrient)) { + if (is.bool(inputOptions.autoOrient)) { + inputDescriptor.autoOrient = inputOptions.autoOrient; + } else { + throw is.invalidParameterError('autoOrient', 'boolean', inputOptions.autoOrient); + } + } // Density if (is.defined(inputOptions.density)) { if (is.inRange(inputOptions.density, 1, 100000)) { @@ -66,16 +138,32 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { throw is.invalidParameterError('density', 'number between 1 and 100000', inputOptions.density); } } + // Ignore embeddded ICC profile + if (is.defined(inputOptions.ignoreIcc)) { + if (is.bool(inputOptions.ignoreIcc)) { + inputDescriptor.ignoreIcc = inputOptions.ignoreIcc; + } else { + throw is.invalidParameterError('ignoreIcc', 'boolean', inputOptions.ignoreIcc); + } + } // limitInputPixels if (is.defined(inputOptions.limitInputPixels)) { if (is.bool(inputOptions.limitInputPixels)) { inputDescriptor.limitInputPixels = inputOptions.limitInputPixels - ? Math.pow(0x3FFF, 2) + ? 0x3FFF ** 2 : 0; - } else if (is.integer(inputOptions.limitInputPixels) && inputOptions.limitInputPixels >= 0) { + } else if (is.integer(inputOptions.limitInputPixels) && is.inRange(inputOptions.limitInputPixels, 0, Number.MAX_SAFE_INTEGER)) { inputDescriptor.limitInputPixels = inputOptions.limitInputPixels; } else { - throw is.invalidParameterError('limitInputPixels', 'integer >= 0', inputOptions.limitInputPixels); + throw is.invalidParameterError('limitInputPixels', 'positive integer', inputOptions.limitInputPixels); + } + } + // unlimited + if (is.defined(inputOptions.unlimited)) { + if (is.bool(inputOptions.unlimited)) { + inputDescriptor.unlimited = inputOptions.unlimited; + } else { + throw is.invalidParameterError('unlimited', 'boolean', inputOptions.unlimited); } } // sequentialRead @@ -97,9 +185,58 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { inputDescriptor.rawWidth = inputOptions.raw.width; inputDescriptor.rawHeight = inputOptions.raw.height; inputDescriptor.rawChannels = inputOptions.raw.channels; + switch (input.constructor) { + case Uint8Array: + case Uint8ClampedArray: + inputDescriptor.rawDepth = 'uchar'; + break; + case Int8Array: + inputDescriptor.rawDepth = 'char'; + break; + case Uint16Array: + inputDescriptor.rawDepth = 'ushort'; + break; + case Int16Array: + inputDescriptor.rawDepth = 'short'; + break; + case Uint32Array: + inputDescriptor.rawDepth = 'uint'; + break; + case Int32Array: + inputDescriptor.rawDepth = 'int'; + break; + case Float32Array: + inputDescriptor.rawDepth = 'float'; + break; + case Float64Array: + inputDescriptor.rawDepth = 'double'; + break; + default: + inputDescriptor.rawDepth = 'uchar'; + break; + } } else { throw new Error('Expected width, height and channels for raw pixel input'); } + inputDescriptor.rawPremultiplied = false; + if (is.defined(inputOptions.raw.premultiplied)) { + if (is.bool(inputOptions.raw.premultiplied)) { + inputDescriptor.rawPremultiplied = inputOptions.raw.premultiplied; + } else { + throw is.invalidParameterError('raw.premultiplied', 'boolean', inputOptions.raw.premultiplied); + } + } + inputDescriptor.rawPageHeight = 0; + if (is.defined(inputOptions.raw.pageHeight)) { + if (is.integer(inputOptions.raw.pageHeight) && inputOptions.raw.pageHeight > 0 && inputOptions.raw.pageHeight <= inputOptions.raw.height) { + if (inputOptions.raw.height % inputOptions.raw.pageHeight !== 0) { + throw new Error(`Expected raw.height ${inputOptions.raw.height} to be a multiple of raw.pageHeight ${inputOptions.raw.pageHeight}`); + } + inputDescriptor.rawPageHeight = inputOptions.raw.pageHeight; + } else { + throw is.invalidParameterError('raw.pageHeight', 'positive integer', inputOptions.raw.pageHeight); + } + } } // Multi-page input (GIF, TIFF, PDF) if (is.defined(inputOptions.animated)) { @@ -123,14 +260,68 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { throw is.invalidParameterError('page', 'integer between 0 and 100000', inputOptions.page); } } - // Multi-level input (OpenSlide) - if (is.defined(inputOptions.level)) { + // OpenSlide specific options + if (is.object(inputOptions.openSlide) && is.defined(inputOptions.openSlide.level)) { + if (is.integer(inputOptions.openSlide.level) && is.inRange(inputOptions.openSlide.level, 0, 256)) { + inputDescriptor.openSlideLevel = inputOptions.openSlide.level; + } else { + throw is.invalidParameterError('openSlide.level', 'integer between 0 and 256', inputOptions.openSlide.level); + } + } else if (is.defined(inputOptions.level)) { + // Deprecated if (is.integer(inputOptions.level) && is.inRange(inputOptions.level, 0, 256)) { - inputDescriptor.level = inputOptions.level; + inputDescriptor.openSlideLevel = inputOptions.level; } else { throw is.invalidParameterError('level', 'integer between 0 and 256', inputOptions.level); } } + // TIFF specific options + if (is.object(inputOptions.tiff) && is.defined(inputOptions.tiff.subifd)) { + if (is.integer(inputOptions.tiff.subifd) && is.inRange(inputOptions.tiff.subifd, -1, 100000)) { + inputDescriptor.tiffSubifd = inputOptions.tiff.subifd; + } else { + throw is.invalidParameterError('tiff.subifd', 'integer between -1 and 100000', inputOptions.tiff.subifd); + } + } else if (is.defined(inputOptions.subifd)) { + // Deprecated + if (is.integer(inputOptions.subifd) && is.inRange(inputOptions.subifd, -1, 100000)) { + inputDescriptor.tiffSubifd = inputOptions.subifd; + } else { + throw is.invalidParameterError('subifd', 'integer between -1 and 100000', inputOptions.subifd); + } + } + // SVG specific options + if (is.object(inputOptions.svg)) { + if (is.defined(inputOptions.svg.stylesheet)) { + if (is.string(inputOptions.svg.stylesheet)) { + inputDescriptor.svgStylesheet = inputOptions.svg.stylesheet; + } else { + throw is.invalidParameterError('svg.stylesheet', 'string', inputOptions.svg.stylesheet); + } + } + if (is.defined(inputOptions.svg.highBitdepth)) { + if (is.bool(inputOptions.svg.highBitdepth)) { + inputDescriptor.svgHighBitdepth = inputOptions.svg.highBitdepth; + } else { + throw is.invalidParameterError('svg.highBitdepth', 'boolean', inputOptions.svg.highBitdepth); + } + } + } + // PDF specific options + if (is.object(inputOptions.pdf) && is.defined(inputOptions.pdf.background)) { + inputDescriptor.pdfBackground = this._getBackgroundColourOption(inputOptions.pdf.background); + } else if (is.defined(inputOptions.pdfBackground)) { + // Deprecated + inputDescriptor.pdfBackground = this._getBackgroundColourOption(inputOptions.pdfBackground); + } + // JPEG 2000 specific options + if (is.object(inputOptions.jp2) && is.defined(inputOptions.jp2.oneshot)) { + if (is.bool(inputOptions.jp2.oneshot)) { + inputDescriptor.jp2Oneshot = inputOptions.jp2.oneshot; + } else { + throw is.invalidParameterError('jp2.oneshot', 'boolean', inputOptions.jp2.oneshot); + } + } // Create new image if (is.defined(inputOptions.create)) { if ( @@ -142,39 +333,50 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { inputDescriptor.createWidth = inputOptions.create.width; inputDescriptor.createHeight = inputOptions.create.height; inputDescriptor.createChannels = inputOptions.create.channels; + inputDescriptor.createPageHeight = 0; + if (is.defined(inputOptions.create.pageHeight)) { + if (is.integer(inputOptions.create.pageHeight) && inputOptions.create.pageHeight > 0 && inputOptions.create.pageHeight <= inputOptions.create.height) { + if (inputOptions.create.height % inputOptions.create.pageHeight !== 0) { + throw new Error(`Expected create.height ${inputOptions.create.height} to be a multiple of create.pageHeight ${inputOptions.create.pageHeight}`); + } + inputDescriptor.createPageHeight = inputOptions.create.pageHeight; + } else { + throw is.invalidParameterError('create.pageHeight', 'positive integer', inputOptions.create.pageHeight); + } + } // 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'])) { + if (inputOptions.create.noise.type !== 'gaussian') { throw new Error('Only gaussian noise is supported at the moment'); } + inputDescriptor.createNoiseType = inputOptions.create.noise.type; 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); + inputDescriptor.createNoiseMean = 128; + if (is.defined(inputOptions.create.noise.mean)) { + 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); + inputDescriptor.createNoiseSigma = 30; + if (is.defined(inputOptions.create.noise.sigma)) { + 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) - ]; + inputDescriptor.createBackground = this._getBackgroundColourOption(inputOptions.create.background); } else { throw new Error('Expected valid noise or background to create a new input image'); } @@ -183,8 +385,135 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { throw new Error('Expected valid width, height and channels to create a new input image'); } } + // Create a new image with text + if (is.defined(inputOptions.text)) { + if (is.object(inputOptions.text) && is.string(inputOptions.text.text)) { + inputDescriptor.textValue = inputOptions.text.text; + if (is.defined(inputOptions.text.height) && is.defined(inputOptions.text.dpi)) { + throw new Error('Expected only one of dpi or height'); + } + if (is.defined(inputOptions.text.font)) { + if (is.string(inputOptions.text.font)) { + inputDescriptor.textFont = inputOptions.text.font; + } else { + throw is.invalidParameterError('text.font', 'string', inputOptions.text.font); + } + } + if (is.defined(inputOptions.text.fontfile)) { + if (is.string(inputOptions.text.fontfile)) { + inputDescriptor.textFontfile = inputOptions.text.fontfile; + } else { + throw is.invalidParameterError('text.fontfile', 'string', inputOptions.text.fontfile); + } + } + if (is.defined(inputOptions.text.width)) { + if (is.integer(inputOptions.text.width) && inputOptions.text.width > 0) { + inputDescriptor.textWidth = inputOptions.text.width; + } else { + throw is.invalidParameterError('text.width', 'positive integer', inputOptions.text.width); + } + } + if (is.defined(inputOptions.text.height)) { + if (is.integer(inputOptions.text.height) && inputOptions.text.height > 0) { + inputDescriptor.textHeight = inputOptions.text.height; + } else { + throw is.invalidParameterError('text.height', 'positive integer', inputOptions.text.height); + } + } + if (is.defined(inputOptions.text.align)) { + if (is.string(inputOptions.text.align) && is.string(this.constructor.align[inputOptions.text.align])) { + inputDescriptor.textAlign = this.constructor.align[inputOptions.text.align]; + } else { + throw is.invalidParameterError('text.align', 'valid alignment', inputOptions.text.align); + } + } + if (is.defined(inputOptions.text.justify)) { + if (is.bool(inputOptions.text.justify)) { + inputDescriptor.textJustify = inputOptions.text.justify; + } else { + throw is.invalidParameterError('text.justify', 'boolean', inputOptions.text.justify); + } + } + if (is.defined(inputOptions.text.dpi)) { + if (is.integer(inputOptions.text.dpi) && is.inRange(inputOptions.text.dpi, 1, 1000000)) { + inputDescriptor.textDpi = inputOptions.text.dpi; + } else { + throw is.invalidParameterError('text.dpi', 'integer between 1 and 1000000', inputOptions.text.dpi); + } + } + if (is.defined(inputOptions.text.rgba)) { + if (is.bool(inputOptions.text.rgba)) { + inputDescriptor.textRgba = inputOptions.text.rgba; + } else { + throw is.invalidParameterError('text.rgba', 'bool', inputOptions.text.rgba); + } + } + if (is.defined(inputOptions.text.spacing)) { + if (is.integer(inputOptions.text.spacing) && is.inRange(inputOptions.text.spacing, -1000000, 1000000)) { + inputDescriptor.textSpacing = inputOptions.text.spacing; + } else { + throw is.invalidParameterError('text.spacing', 'integer between -1000000 and 1000000', inputOptions.text.spacing); + } + } + if (is.defined(inputOptions.text.wrap)) { + if (is.string(inputOptions.text.wrap) && is.inArray(inputOptions.text.wrap, ['word', 'char', 'word-char', 'none'])) { + inputDescriptor.textWrap = inputOptions.text.wrap; + } else { + throw is.invalidParameterError('text.wrap', 'one of: word, char, word-char, none', inputOptions.text.wrap); + } + } + delete inputDescriptor.buffer; + } else { + throw new Error('Expected a valid string to create an image with text.'); + } + } + // Join images together + if (is.defined(inputOptions.join)) { + if (is.defined(this.options.join)) { + if (is.defined(inputOptions.join.animated)) { + if (is.bool(inputOptions.join.animated)) { + inputDescriptor.joinAnimated = inputOptions.join.animated; + } else { + throw is.invalidParameterError('join.animated', 'boolean', inputOptions.join.animated); + } + } + if (is.defined(inputOptions.join.across)) { + if (is.integer(inputOptions.join.across) && is.inRange(inputOptions.join.across, 1, 1000000)) { + inputDescriptor.joinAcross = inputOptions.join.across; + } else { + throw is.invalidParameterError('join.across', 'integer between 1 and 100000', inputOptions.join.across); + } + } + if (is.defined(inputOptions.join.shim)) { + if (is.integer(inputOptions.join.shim) && is.inRange(inputOptions.join.shim, 0, 1000000)) { + inputDescriptor.joinShim = inputOptions.join.shim; + } else { + throw is.invalidParameterError('join.shim', 'integer between 0 and 100000', inputOptions.join.shim); + } + } + if (is.defined(inputOptions.join.background)) { + inputDescriptor.joinBackground = this._getBackgroundColourOption(inputOptions.join.background); + } + if (is.defined(inputOptions.join.halign)) { + if (is.string(inputOptions.join.halign) && is.string(this.constructor.align[inputOptions.join.halign])) { + inputDescriptor.joinHalign = this.constructor.align[inputOptions.join.halign]; + } else { + throw is.invalidParameterError('join.halign', 'valid alignment', inputOptions.join.halign); + } + } + if (is.defined(inputOptions.join.valign)) { + if (is.string(inputOptions.join.valign) && is.string(this.constructor.align[inputOptions.join.valign])) { + inputDescriptor.joinValign = this.constructor.align[inputOptions.join.valign]; + } else { + throw is.invalidParameterError('join.valign', 'valid alignment', inputOptions.join.valign); + } + } + } else { + throw new Error('Expected input to be an array of images to join'); + } + } } else if (is.defined(inputOptions)) { - throw new Error('Invalid input options ' + inputOptions); + throw new Error(`Invalid input options ${inputOptions}`); } return inputDescriptor; } @@ -196,10 +525,8 @@ function _createInputDescriptor (input, inputOptions, containerOptions) { * @param {string} encoding - unused * @param {Function} callback */ -function _write (chunk, encoding, callback) { - /* istanbul ignore else */ +function _write (chunk, _encoding, callback) { if (Array.isArray(this.options.input.buffer)) { - /* istanbul ignore else */ if (is.buffer(chunk)) { if (this.options.input.buffer.length === 0) { this.on('finish', () => { @@ -236,25 +563,39 @@ function _isStreamInput () { } /** - * Fast access to (uncached) image metadata without decoding any compressed image data. + * Fast access to (uncached) image metadata without decoding any compressed pixel data. + * + * This is read from the header of the input image. + * It does not take into consideration any operations to be applied to the output image, + * such as resize or rotate. + * + * Dimensions in the response will respect the `page` and `pages` properties of the + * {@link /api-constructor/ constructor parameters}. + * * A `Promise` is returned when `callback` is not provided. * - * - `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg` + * - `format`: Name of decoder used to parse image e.g. `jpeg`, `png`, `webp`, `gif`, `svg`, `heif`, `tiff` * - `size`: Total size of image in bytes, for Stream and Buffer input only - * - `width`: Number of pixels wide (EXIF orientation is not taken into consideration) - * - `height`: Number of pixels high (EXIF orientation is not taken into consideration) - * - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://libvips.github.io/libvips/API/current/VipsImage.html#VipsInterpretation) + * - `width`: Number of pixels wide (EXIF orientation is not taken into consideration, see example below) + * - `height`: Number of pixels high (EXIF orientation is not taken into consideration, see example below) + * - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html) * - `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK - * - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://libvips.github.io/libvips/API/current/VipsImage.html#VipsBandFormat) + * - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://www.libvips.org/API/current/enum.BandFormat.html) * - `density`: Number of pixels per inch (DPI), if present * - `chromaSubsampling`: String containing JPEG chroma subsampling, `4:2:0` or `4:4:4` for RGB, `4:2:0:4` or `4:4:4:4` for CMYK * - `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan + * - `isPalette`: Boolean indicating whether the image is palette-based (GIF, PNG). + * - `bitsPerSample`: Number of bits per sample for each channel (GIF, PNG, HEIF). * - `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP * - `pageHeight`: Number of pixels high each page in a multi-page image will be. * - `loop`: Number of times to loop an animated image, zero refers to a continuous loop. * - `delay`: Delay in ms between each page in an animated image, provided as an array of integers. * - `pagePrimary`: Number of the primary page in a HEIF image * - `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide + * - `subifds`: Number of Sub Image File Directories in an OME-TIFF image + * - `background`: Default background colour, if present, for PNG (bKGD) and GIF images + * - `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) + * - `resolutionUnit`: The unit of resolution (density), either `inch` or `cm`, if present * - `hasProfile`: Boolean indicating the presence of an embedded ICC profile * - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel * - `orientation`: Number value of the EXIF Orientation header, if present @@ -262,7 +603,13 @@ function _isStreamInput () { * - `icc`: Buffer containing raw [ICC](https://www.npmjs.com/package/icc) profile data, if present * - `iptc`: Buffer containing raw IPTC data, if present * - `xmp`: Buffer containing raw XMP data, if present + * - `xmpAsString`: String containing XMP data, if valid UTF-8. * - `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present + * - `formatMagick`: String containing format for images loaded via *magick + * - `comments`: Array of keyword/text pairs representing PNG text blocks, if present. + * + * @example + * const metadata = await sharp(input).metadata(); * * @example * const image = sharp(inputJpg); @@ -278,39 +625,62 @@ function _isStreamInput () { * // data contains a WebP image half the width and height of the original JPEG * }); * + * @example + * // Get dimensions taking EXIF Orientation into account. + * const { autoOrient } = await sharp(input).metadata(); + * const { width, height } = autoOrient; + * * @param {Function} [callback] - called with the arguments `(err, metadata)` * @returns {Promise<Object>|Sharp} */ function metadata (callback) { + const stack = Error(); if (is.fn(callback)) { if (this._isStreamInput()) { this.on('finish', () => { this._flattenBufferIn(); - sharp.metadata(this.options, callback); + sharp.metadata(this.options, (err, metadata) => { + if (err) { + callback(is.nativeError(err, stack)); + } else { + callback(null, metadata); + } + }); }); } else { - sharp.metadata(this.options, callback); + sharp.metadata(this.options, (err, metadata) => { + if (err) { + callback(is.nativeError(err, stack)); + } else { + callback(null, metadata); + } + }); } return this; } else { if (this._isStreamInput()) { return new Promise((resolve, reject) => { - this.on('finish', () => { + const finished = () => { this._flattenBufferIn(); sharp.metadata(this.options, (err, metadata) => { if (err) { - reject(err); + reject(is.nativeError(err, stack)); } else { resolve(metadata); } }); - }); + }; + if (this.writableFinished) { + finished(); + } else { + this.once('finish', finished); + } }); } else { return new Promise((resolve, reject) => { sharp.metadata(this.options, (err, metadata) => { if (err) { - reject(err); + reject(is.nativeError(err, stack)); } else { resolve(metadata); } @@ -336,9 +706,12 @@ function metadata (callback) { * - `maxX` (x-coordinate of one of the pixel where the maximum lies) * - `maxY` (y-coordinate of one of the pixel where the maximum lies) * - `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque. - * - `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental) - * - `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any (experimental) - * - `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram (experimental) + * - `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any. + * - `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any. + * - `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram. + * + * **Note**: Statistics are derived from the original input image. Any operations performed on the image must first be + * written to a buffer in order to run `stats` on the result (see third example). * * @example * const image = sharp(inputJpg); @@ -352,18 +725,38 @@ function metadata (callback) { * const { entropy, sharpness, dominant } = await sharp(input).stats(); * const { r, g, b } = dominant; * + * @example + * const image = sharp(input); + * // store intermediate result + * const part = await image.extract(region).toBuffer(); + * // create new instance to obtain statistics of extracted region + * const stats = await sharp(part).stats(); + * * @param {Function} [callback] - called with the arguments `(err, stats)` * @returns {Promise<Object>} */ function stats (callback) { + const stack = Error(); if (is.fn(callback)) { if (this._isStreamInput()) { this.on('finish', () => { this._flattenBufferIn(); - sharp.stats(this.options, callback); + sharp.stats(this.options, (err, stats) => { + if (err) { + callback(is.nativeError(err, stack)); + } else { + callback(null, stats); + } + }); }); } else { - sharp.stats(this.options, callback); + sharp.stats(this.options, (err, stats) => { + if (err) { + callback(is.nativeError(err, stack)); + } else { + callback(null, stats); + } + }); } return this; } else { @@ -373,7 +766,7 @@ function stats (callback) { this._flattenBufferIn(); sharp.stats(this.options, (err, stats) => { if (err) { - reject(err); + reject(is.nativeError(err, stack)); } else { resolve(stats); } @@ -384,7 +777,7 @@ function stats (callback) { return new Promise((resolve, reject) => { sharp.stats(this.options, (err, stats) => { if (err) { - reject(err); + reject(is.nativeError(err, stack)); } else { resolve(stats); } @@ -396,9 +789,10 @@ function stats (callback) { /** * Decorate the Sharp prototype with input-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Object.assign(Sharp.prototype, { // Private _inputOptionsFromObject, @@ -410,4 +804,6 @@ module.exports = function (Sharp) { metadata, stats }); + // Class attributes + Sharp.align = align; }; diff --git a/lib/is.js b/lib/is.js index 7662c6e70..3ac9a1a35 100644 --- a/lib/is.js +++ b/lib/is.js @@ -1,101 +1,102 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ /** * Is this value defined and not null? * @private */ -const defined = function (val) { - return typeof val !== 'undefined' && val !== null; -}; +const defined = (val) => typeof val !== 'undefined' && val !== null; /** * Is this value an object? * @private */ -const object = function (val) { - return typeof val === 'object'; -}; +const object = (val) => typeof val === 'object'; /** * Is this value a plain object? * @private */ -const plainObject = function (val) { - return Object.prototype.toString.call(val) === '[object Object]'; -}; +const plainObject = (val) => Object.prototype.toString.call(val) === '[object Object]'; /** * Is this value a function? * @private */ -const fn = function (val) { - return typeof val === 'function'; -}; +const fn = (val) => typeof val === 'function'; /** * Is this value a boolean? * @private */ -const bool = function (val) { - return typeof val === 'boolean'; -}; +const bool = (val) => typeof val === 'boolean'; /** * Is this value a Buffer object? * @private */ -const buffer = function (val) { - return val instanceof Buffer; -}; +const buffer = (val) => val instanceof Buffer; /** - * Is this value a Uint8Array or Uint8ClampedArray object? + * Is this value a typed array object?. E.g. Uint8Array or Uint8ClampedArray? * @private */ -const uint8Array = function (val) { - // allow both since Uint8ClampedArray simply clamps the values between 0-255 - return val instanceof Uint8Array || val instanceof Uint8ClampedArray; +const typedArray = (val) => { + if (defined(val)) { + switch (val.constructor) { + case Uint8Array: + case Uint8ClampedArray: + case Int8Array: + case Uint16Array: + case Int16Array: + case Uint32Array: + case Int32Array: + case Float32Array: + case Float64Array: + return true; + } + } + + return false; }; +/** + * Is this value an ArrayBuffer object? + * @private + */ +const arrayBuffer = (val) => val instanceof ArrayBuffer; + /** * Is this value a non-empty string? * @private */ -const string = function (val) { - return typeof val === 'string' && val.length > 0; -}; +const string = (val) => typeof val === 'string' && val.length > 0; /** * Is this value a real number? * @private */ -const number = function (val) { - return typeof val === 'number' && !Number.isNaN(val); -}; +const number = (val) => typeof val === 'number' && !Number.isNaN(val); /** * Is this value an integer? * @private */ -const integer = function (val) { - return Number.isInteger(val); -}; +const integer = (val) => Number.isInteger(val); /** * Is this value within an inclusive given range? * @private */ -const inRange = function (val, min, max) { - return val >= min && val <= max; -}; +const inRange = (val, min, max) => val >= min && val <= max; /** * Is this value within the elements of an array? * @private */ -const inArray = function (val, list) { - return list.includes(val); -}; +const inArray = (val, list) => list.includes(val); /** * Create an Error with a message relating to an invalid parameter. @@ -106,24 +107,37 @@ const inArray = function (val, list) { * @returns {Error} Containing the formatted message. * @private */ -const invalidParameterError = function (name, expected, actual) { - return new Error( +const invalidParameterError = (name, expected, actual) => new Error( `Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}` ); + +/** + * Ensures an Error from C++ contains a JS stack. + * + * @param {Error} native - Error with message from C++. + * @param {Error} context - Error with stack from JS. + * @returns {Error} Error with message and stack. + * @private + */ +const nativeError = (native, context) => { + context.message = native.message; + return context; }; module.exports = { - defined: defined, - object: object, - plainObject: plainObject, - fn: fn, - bool: bool, - buffer: buffer, - uint8Array: uint8Array, - string: string, - number: number, - integer: integer, - inRange: inRange, - inArray: inArray, - invalidParameterError: invalidParameterError + defined, + object, + plainObject, + fn, + bool, + buffer, + typedArray, + arrayBuffer, + string, + number, + integer, + inRange, + inArray, + invalidParameterError, + nativeError }; diff --git a/lib/libvips.js b/lib/libvips.js index 6ee0554ba..881dc5c13 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -1,52 +1,104 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const os = require('os'); -const path = require('path'); -const spawnSync = require('child_process').spawnSync; -const semver = require('semver'); -const platform = require('./platform'); +const { spawnSync } = require('node:child_process'); +const { createHash } = require('node:crypto'); +const semverCoerce = require('semver/functions/coerce'); +const semverGreaterThanOrEqualTo = require('semver/functions/gte'); +const semverSatisfies = require('semver/functions/satisfies'); +const detectLibc = require('detect-libc'); -const env = process.env; -const minimumLibvipsVersionLabelled = env.npm_package_config_libvips || /* istanbul ignore next */ - require('../package.json').config.libvips; -const minimumLibvipsVersion = semver.coerce(minimumLibvipsVersionLabelled).version; +const { config, engines, optionalDependencies } = require('../package.json'); + +/* node:coverage ignore next */ +const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || config.libvips; +const minimumLibvipsVersion = semverCoerce(minimumLibvipsVersionLabelled).version; + +const prebuiltPlatforms = [ + 'darwin-arm64', 'darwin-x64', + 'linux-arm', 'linux-arm64', 'linux-ppc64', 'linux-riscv64', 'linux-s390x', 'linux-x64', + 'linuxmusl-arm64', 'linuxmusl-x64', + 'win32-arm64', 'win32-ia32', 'win32-x64' +]; const spawnSyncOptions = { encoding: 'utf8', shell: true }; -const mkdirSync = function (dirPath) { +const log = (item) => { + if (item instanceof Error) { + console.error(`sharp: Installation error: ${item.message}`); + } else { + console.log(`sharp: ${item}`); + } +}; + +/* node:coverage ignore next */ +const runtimeLibc = () => detectLibc.isNonGlibcLinuxSync() ? detectLibc.familySync() : ''; + +const runtimePlatformArch = () => `${process.platform}${runtimeLibc()}-${process.arch}`; + +const buildPlatformArch = () => { + /* node:coverage ignore next 3 */ + if (isEmscripten()) { + return 'wasm32'; + } + const { npm_config_arch, npm_config_platform, npm_config_libc } = process.env; + const libc = typeof npm_config_libc === 'string' ? npm_config_libc : runtimeLibc(); + return `${npm_config_platform || process.platform}${libc}-${npm_config_arch || process.arch}`; +}; + +const buildSharpLibvipsIncludeDir = () => { try { - fs.mkdirSync(dirPath); - } catch (err) { - /* istanbul ignore if */ - if (err.code !== 'EEXIST') { - throw err; - } + return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/include`); + } catch { + /* node:coverage ignore next 5 */ + try { + return require('@img/sharp-libvips-dev/include'); + } catch {} } + return ''; }; -const cachePath = function () { - const npmCachePath = env.npm_config_cache || /* istanbul ignore next */ - (env.APPDATA ? path.join(env.APPDATA, 'npm-cache') : path.join(os.homedir(), '.npm')); - mkdirSync(npmCachePath); - const libvipsCachePath = path.join(npmCachePath, '_libvips'); - mkdirSync(libvipsCachePath); - return libvipsCachePath; +const buildSharpLibvipsCPlusPlusDir = () => { + /* node:coverage ignore next 4 */ + try { + return require('@img/sharp-libvips-dev/cplusplus'); + } catch {} + return ''; }; -const log = function (item) { - if (item instanceof Error) { - console.error(`sharp: ${item.message}`); - } else { - console.log(`sharp: ${item}`); +const buildSharpLibvipsLibDir = () => { + try { + return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/lib`); + } catch { + /* node:coverage ignore next 5 */ + try { + return require(`@img/sharp-libvips-${buildPlatformArch()}/lib`); + } catch {} + } + return ''; +}; + +/* node:coverage disable */ + +const isUnsupportedNodeRuntime = () => { + if (process.release?.name === 'node' && process.versions) { + if (!semverSatisfies(process.versions.node, engines.node)) { + return { found: process.versions.node, expected: engines.node }; + } } }; -const isRosetta = function () { - /* istanbul ignore next */ +const isEmscripten = () => { + const { CC } = process.env; + return Boolean(CC?.endsWith('/emcc')); +}; + +const isRosetta = () => { if (process.platform === 'darwin' && process.arch === 'x64') { const translated = spawnSync('sysctl sysctl.proc_translated', spawnSyncOptions).stdout; return (translated || '').trim() === 'sysctl.proc_translated: 1'; @@ -54,68 +106,102 @@ const isRosetta = function () { return false; }; -const globalLibvipsVersion = function () { +/* node:coverage enable */ + +const sha512 = (s) => createHash('sha512').update(s).digest('hex'); + +const yarnLocator = () => { + try { + const identHash = sha512(`imgsharp-libvips-${buildPlatformArch()}`); + const npmVersion = semverCoerce(optionalDependencies[`@img/sharp-libvips-${buildPlatformArch()}`], { + includePrerelease: true + }).version; + return sha512(`${identHash}npm:${npmVersion}`).slice(0, 10); + } catch {} + return ''; +}; + +/* node:coverage disable */ + +const spawnRebuild = () => + spawnSync(`node-gyp rebuild --directory=src ${isEmscripten() ? '--nodedir=emscripten' : ''}`, { + ...spawnSyncOptions, + stdio: 'inherit' + }).status; + +const globalLibvipsVersion = () => { if (process.platform !== 'win32') { - const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout; - /* istanbul ignore next */ + const globalLibvipsVersion = spawnSync('pkg-config --modversion vips-cpp', { + ...spawnSyncOptions, + env: { + ...process.env, + PKG_CONFIG_PATH: pkgConfigPath() + } + }).stdout; return (globalLibvipsVersion || '').trim(); } else { return ''; } }; -const hasVendoredLibvips = function () { - const currentPlatformId = platform(); - const vendorPath = path.join(__dirname, '..', 'vendor', minimumLibvipsVersion); - let vendorPlatformId; - try { - vendorPlatformId = require(path.join(vendorPath, 'platform.json')); - } catch (err) {} - /* istanbul ignore else */ - if (vendorPlatformId) { - /* istanbul ignore else */ - if (currentPlatformId === vendorPlatformId) { - return true; - } else { - throw new Error(`'${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp' directory and run 'npm install' on the '${currentPlatformId}' platform.`); - } - } else { - return false; - } -}; +/* node:coverage enable */ -const pkgConfigPath = function () { +const pkgConfigPath = () => { if (process.platform !== 'win32') { - const brewPkgConfigPath = spawnSync('which brew >/dev/null 2>&1 && eval $(brew --env) && echo $PKG_CONFIG_LIBDIR', spawnSyncOptions).stdout || ''; - return [brewPkgConfigPath.trim(), env.PKG_CONFIG_PATH, '/usr/local/lib/pkgconfig', '/usr/lib/pkgconfig'] - .filter(function (p) { return !!p; }) - .join(':'); + /* node:coverage ignore next 4 */ + const brewPkgConfigPath = spawnSync( + 'which brew >/dev/null 2>&1 && brew environment --plain | grep PKG_CONFIG_LIBDIR | cut -d" " -f2', + spawnSyncOptions + ).stdout || ''; + return [ + brewPkgConfigPath.trim(), + process.env.PKG_CONFIG_PATH, + '/usr/local/lib/pkgconfig', + '/usr/lib/pkgconfig', + '/usr/local/libdata/pkgconfig', + '/usr/libdata/pkgconfig' + ].filter(Boolean).join(':'); } else { return ''; } }; -const useGlobalLibvips = function () { - if (Boolean(env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) { - return false; +const skipSearch = (status, reason, logger) => { + if (logger) { + logger(`Detected ${reason}, skipping search for globally-installed libvips`); + } + return status; +}; + +const useGlobalLibvips = (logger) => { + if (Boolean(process.env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) { + return skipSearch(false, 'SHARP_IGNORE_GLOBAL_LIBVIPS', logger); + } + if (Boolean(process.env.SHARP_FORCE_GLOBAL_LIBVIPS) === true) { + return skipSearch(true, 'SHARP_FORCE_GLOBAL_LIBVIPS', logger); } - /* istanbul ignore next */ + /* node:coverage ignore next 3 */ if (isRosetta()) { - return false; + return skipSearch(false, 'Rosetta', logger); } const globalVipsVersion = globalLibvipsVersion(); - return !!globalVipsVersion && /* istanbul ignore next */ - semver.gte(globalVipsVersion, minimumLibvipsVersion); + /* node:coverage ignore next */ + return !!globalVipsVersion && semverGreaterThanOrEqualTo(globalVipsVersion, minimumLibvipsVersion); }; module.exports = { minimumLibvipsVersion, - minimumLibvipsVersionLabelled, - cachePath, + prebuiltPlatforms, + buildPlatformArch, + buildSharpLibvipsIncludeDir, + buildSharpLibvipsCPlusPlusDir, + buildSharpLibvipsLibDir, + isUnsupportedNodeRuntime, + runtimePlatformArch, log, + yarnLocator, + spawnRebuild, globalLibvipsVersion, - hasVendoredLibvips, pkgConfigPath, - useGlobalLibvips, - mkdirSync + useGlobalLibvips }; diff --git a/lib/operation.js b/lib/operation.js index 2a3b52d30..ebbf54e9c 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -1,36 +1,50 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const color = require('color'); const is = require('./is'); /** - * Rotate the output image by either an explicit angle - * or auto-orient based on the EXIF `Orientation` tag. + * How accurate an operation should be. + * @member + * @private + */ +const vipsPrecision = { + integer: 'integer', + float: 'float', + approximate: 'approximate' +}; + +/** + * Rotate the output image. * - * If an angle is provided, it is converted to a valid positive degree rotation. - * For example, `-450` will produce a 270deg rotation. + * The provided angle is converted to a valid positive degree rotation. + * For example, `-450` will produce a 270 degree rotation. * * When rotating by an angle other than a multiple of 90, * the background colour can be provided with the `background` option. * - * If no angle is provided, it is determined from the EXIF data. - * Mirroring is supported and may infer the use of a flip operation. + * For backwards compatibility, if no angle is provided, `.autoOrient()` will be called. * - * The use of `rotate` implies the removal of the EXIF `Orientation` tag, if any. + * Only one rotation can occur per pipeline (aside from an initial call without + * arguments to orient via EXIF data). Previous calls to `rotate` in the same + * pipeline will be ignored. * - * Method order is important when both rotating and extracting regions, - * for example `rotate(x).extract(y)` will produce a different result to `extract(y).rotate(x)`. + * Multi-page images can only be rotated by 180 degrees. + * + * Method order is important when rotating, resizing and/or extracting regions, + * for example `.rotate(x).extract(y)` will produce a different result to `.extract(y).rotate(x)`. * * @example - * const pipeline = sharp() - * .rotate() - * .resize(null, 200) - * .toBuffer(function (err, outputBuffer, info) { - * // outputBuffer contains 200px high JPEG image data, - * // auto-rotated using EXIF Orientation tag - * // info.width and info.height contain the dimensions of the resized image - * }); - * readableStream.pipe(pipeline); + * const rotateThenResize = await sharp(input) + * .rotate(90) + * .resize({ width: 16, height: 8, fit: 'fill' }) + * .toBuffer(); + * const resizeThenRotate = await sharp(input) + * .resize({ width: 16, height: 8, fit: 'fill' }) + * .rotate(90) + * .toBuffer(); * * @param {number} [angle=auto] angle of rotation. * @param {Object} [options] - if present, is an Object with optional attributes. @@ -40,19 +54,19 @@ const is = require('./is'); */ function rotate (angle, options) { if (!is.defined(angle)) { - this.options.useExifOrientation = true; - } else if (is.integer(angle) && !(angle % 90)) { + return this.autoOrient(); + } + if (this.options.angle || this.options.rotationAngle) { + this.options.debuglog('ignoring previous rotate options'); + this.options.angle = 0; + this.options.rotationAngle = 0; + } + if (is.integer(angle) && !(angle % 90)) { this.options.angle = angle; } else if (is.number(angle)) { this.options.rotationAngle = angle; if (is.object(options) && options.background) { - const backgroundColour = color(options.background); - this.options.rotationBackground = [ - backgroundColour.red(), - backgroundColour.green(), - backgroundColour.blue(), - Math.round(backgroundColour.alpha() * 255) - ]; + this._setBackgroundColourOption('rotationBackground', options.background); } } else { throw is.invalidParameterError('angle', 'numeric', angle); @@ -61,8 +75,42 @@ function rotate (angle, options) { } /** - * Flip the image about the vertical Y axis. This always occurs after rotation, if any. - * The use of `flip` implies the removal of the EXIF `Orientation` tag, if any. + * Auto-orient based on the EXIF `Orientation` tag, then remove the tag. + * Mirroring is supported and may infer the use of a flip operation. + * + * Previous or subsequent use of `rotate(angle)` and either `flip()` or `flop()` + * will logically occur after auto-orientation, regardless of call order. + * + * @example + * const output = await sharp(input).autoOrient().toBuffer(); + * + * @example + * const pipeline = sharp() + * .autoOrient() + * .resize(null, 200) + * .toBuffer(function (err, outputBuffer, info) { + * // outputBuffer contains 200px high JPEG image data, + * // auto-oriented using EXIF Orientation tag + * // info.width and info.height contain the dimensions of the resized image + * }); + * readableStream.pipe(pipeline); + * + * @returns {Sharp} + */ +function autoOrient () { + this.options.input.autoOrient = true; + return this; +} + +/** + * Mirror the image vertically (up-down) about the x-axis. + * This always occurs before rotation, if any. + * + * This operation does not work correctly with multi-page images. + * + * @example + * const output = await sharp(input).flip().toBuffer(); + * * @param {Boolean} [flip=true] * @returns {Sharp} */ @@ -72,8 +120,12 @@ function flip (flip) { } /** - * Flop the image about the horizontal X axis. This always occurs after rotation, if any. - * The use of `flop` implies the removal of the EXIF `Orientation` tag, if any. + * Mirror the image horizontally (left-right) about the y-axis. + * This always occurs before rotation, if any. + * + * @example + * const output = await sharp(input).flop().toBuffer(); + * * @param {Boolean} [flop=true] * @returns {Sharp} */ @@ -86,8 +138,8 @@ function flop (flop) { * 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`. + * By default, new pixels are filled with a black background. You can provide a background colour with the `background` option. + * A particular interpolator may also be specified. Set the `interpolator` option to an attribute of the `sharp.interpolators` Object e.g. `sharp.interpolators.nohalo`. * * In the case of a 2x2 matrix, the transform is: * - X = `matrix[0, 0]` \* (x + `idx`) + `matrix[0, 1]` \* (y + `idy`) + `odx` @@ -104,7 +156,7 @@ function flop (flop) { * const pipeline = sharp() * .affine([[1, 0.3], [0.1, 0.7]], { * background: 'white', - * interpolate: sharp.interpolators.nohalo + * interpolator: sharp.interpolators.nohalo * }) * .toBuffer((err, outputBuffer, info) => { * // outputBuffer contains the transformed image @@ -181,44 +233,113 @@ function affine (matrix, options) { /** * Sharpen the image. + * * When used without parameters, performs a fast, mild sharpen of the output image. + * * When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space. - * Separate control over the level of sharpening in "flat" and "jagged" areas is available. + * Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available. + * + * See {@link https://www.libvips.org/API/current/method.Image.sharpen.html libvips sharpen} operation. + * + * @example + * const data = await sharp(input).sharpen().toBuffer(); + * + * @example + * const data = await sharp(input).sharpen({ sigma: 2 }).toBuffer(); * - * @param {number} [sigma] - the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. - * @param {number} [flat=1.0] - the level of sharpening to apply to "flat" areas. - * @param {number} [jagged=2.0] - the level of sharpening to apply to "jagged" areas. + * @example + * const data = await sharp(input) + * .sharpen({ + * sigma: 2, + * m1: 0, + * m2: 3, + * x1: 3, + * y2: 15, + * y3: 15, + * }) + * .toBuffer(); + * + * @param {Object|number} [options] - if present, is an Object with attributes + * @param {number} [options.sigma] - the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`, between 0.000001 and 10 + * @param {number} [options.m1=1.0] - the level of sharpening to apply to "flat" areas, between 0 and 1000000 + * @param {number} [options.m2=2.0] - the level of sharpening to apply to "jagged" areas, between 0 and 1000000 + * @param {number} [options.x1=2.0] - threshold between "flat" and "jagged", between 0 and 1000000 + * @param {number} [options.y2=10.0] - maximum amount of brightening, between 0 and 1000000 + * @param {number} [options.y3=20.0] - maximum amount of darkening, between 0 and 1000000 + * @param {number} [flat] - (deprecated) see `options.m1`. + * @param {number} [jagged] - (deprecated) see `options.m2`. * @returns {Sharp} * @throws {Error} Invalid parameters */ -function sharpen (sigma, flat, jagged) { - if (!is.defined(sigma)) { +function sharpen (options, flat, jagged) { + if (!is.defined(options)) { // No arguments: default to mild sharpen this.options.sharpenSigma = -1; - } else if (is.bool(sigma)) { - // Boolean argument: apply mild sharpen? - this.options.sharpenSigma = sigma ? -1 : 0; - } else if (is.number(sigma) && is.inRange(sigma, 0.01, 10000)) { - // Numeric argument: specific sigma - this.options.sharpenSigma = sigma; - // Control over flat areas + } else if (is.bool(options)) { + // Deprecated boolean argument: apply mild sharpen? + this.options.sharpenSigma = options ? -1 : 0; + } else if (is.number(options) && is.inRange(options, 0.01, 10000)) { + // Deprecated numeric argument: specific sigma + this.options.sharpenSigma = options; + // Deprecated control over flat areas if (is.defined(flat)) { if (is.number(flat) && is.inRange(flat, 0, 10000)) { - this.options.sharpenFlat = flat; + this.options.sharpenM1 = flat; } else { throw is.invalidParameterError('flat', 'number between 0 and 10000', flat); } } - // Control over jagged areas + // Deprecated control over jagged areas if (is.defined(jagged)) { if (is.number(jagged) && is.inRange(jagged, 0, 10000)) { - this.options.sharpenJagged = jagged; + this.options.sharpenM2 = jagged; } else { throw is.invalidParameterError('jagged', 'number between 0 and 10000', jagged); } } + } else if (is.plainObject(options)) { + if (is.number(options.sigma) && is.inRange(options.sigma, 0.000001, 10)) { + this.options.sharpenSigma = options.sigma; + } else { + throw is.invalidParameterError('options.sigma', 'number between 0.000001 and 10', options.sigma); + } + if (is.defined(options.m1)) { + if (is.number(options.m1) && is.inRange(options.m1, 0, 1000000)) { + this.options.sharpenM1 = options.m1; + } else { + throw is.invalidParameterError('options.m1', 'number between 0 and 1000000', options.m1); + } + } + if (is.defined(options.m2)) { + if (is.number(options.m2) && is.inRange(options.m2, 0, 1000000)) { + this.options.sharpenM2 = options.m2; + } else { + throw is.invalidParameterError('options.m2', 'number between 0 and 1000000', options.m2); + } + } + if (is.defined(options.x1)) { + if (is.number(options.x1) && is.inRange(options.x1, 0, 1000000)) { + this.options.sharpenX1 = options.x1; + } else { + throw is.invalidParameterError('options.x1', 'number between 0 and 1000000', options.x1); + } + } + if (is.defined(options.y2)) { + if (is.number(options.y2) && is.inRange(options.y2, 0, 1000000)) { + this.options.sharpenY2 = options.y2; + } else { + throw is.invalidParameterError('options.y2', 'number between 0 and 1000000', options.y2); + } + } + if (is.defined(options.y3)) { + if (is.number(options.y3) && is.inRange(options.y3, 0, 1000000)) { + this.options.sharpenY3 = options.y3; + } else { + throw is.invalidParameterError('options.y3', 'number between 0 and 1000000', options.y3); + } + } } else { - throw is.invalidParameterError('sigma', 'number between 0.01 and 10000', sigma); + throw is.invalidParameterError('sigma', 'number between 0.01 and 10000', options); } return this; } @@ -226,6 +347,13 @@ function sharpen (sigma, flat, jagged) { /** * Apply median filter. * When used without parameters the default window is 3x3. + * + * @example + * const output = await sharp(input).median().toBuffer(); + * + * @example + * const output = await sharp(input).median(5).toBuffer(); + * * @param {number} [size=3] square mask size: size x size * @returns {Sharp} * @throws {Error} Invalid parameters @@ -245,34 +373,123 @@ function median (size) { /** * Blur the image. - * When used without parameters, performs a fast, mild blur of the output image. + * + * When used without parameters, performs a fast 3x3 box blur (equivalent to a box linear filter). + * * When a `sigma` is provided, performs a slower, more accurate Gaussian blur. - * @param {number} [sigma] a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. + * + * @example + * const boxBlurred = await sharp(input) + * .blur() + * .toBuffer(); + * + * @example + * const gaussianBlurred = await sharp(input) + * .blur(5) + * .toBuffer(); + * + * @param {Object|number|Boolean} [options] + * @param {number} [options.sigma] a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. + * @param {string} [options.precision='integer'] How accurate the operation should be, one of: integer, float, approximate. + * @param {number} [options.minAmplitude=0.2] A value between 0.001 and 1. A smaller value will generate a larger, more accurate mask. * @returns {Sharp} * @throws {Error} Invalid parameters */ -function blur (sigma) { - if (!is.defined(sigma)) { +function blur (options) { + let sigma; + if (is.number(options)) { + sigma = options; + } else if (is.plainObject(options)) { + if (!is.number(options.sigma)) { + throw is.invalidParameterError('options.sigma', 'number between 0.3 and 1000', sigma); + } + sigma = options.sigma; + if ('precision' in options) { + if (is.string(vipsPrecision[options.precision])) { + this.options.precision = vipsPrecision[options.precision]; + } else { + throw is.invalidParameterError('precision', 'one of: integer, float, approximate', options.precision); + } + } + if ('minAmplitude' in options) { + if (is.number(options.minAmplitude) && is.inRange(options.minAmplitude, 0.001, 1)) { + this.options.minAmpl = options.minAmplitude; + } else { + throw is.invalidParameterError('minAmplitude', 'number between 0.001 and 1', options.minAmplitude); + } + } + } + + if (!is.defined(options)) { // No arguments: default to mild blur this.options.blurSigma = -1; - } else if (is.bool(sigma)) { + } else if (is.bool(options)) { // Boolean argument: apply mild blur? - this.options.blurSigma = sigma ? -1 : 0; + this.options.blurSigma = options ? -1 : 0; } else if (is.number(sigma) && is.inRange(sigma, 0.3, 1000)) { // Numeric argument: specific sigma this.options.blurSigma = sigma; } else { throw is.invalidParameterError('sigma', 'number between 0.3 and 1000', sigma); } + + return this; +} + +/** + * Expand foreground objects using the dilate morphological operator. + * + * @example + * const output = await sharp(input) + * .dilate() + * .toBuffer(); + * + * @param {Number} [width=1] dilation width in pixels. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function dilate (width) { + if (!is.defined(width)) { + this.options.dilateWidth = 1; + } else if (is.integer(width) && width > 0) { + this.options.dilateWidth = width; + } else { + throw is.invalidParameterError('dilate', 'positive integer', dilate); + } + return this; +} + +/** + * Shrink foreground objects using the erode morphological operator. + * + * @example + * const output = await sharp(input) + * .erode() + * .toBuffer(); + * + * @param {Number} [width=1] erosion width in pixels. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function erode (width) { + if (!is.defined(width)) { + this.options.erodeWidth = 1; + } else if (is.integer(width) && width > 0) { + this.options.erodeWidth = width; + } else { + throw is.invalidParameterError('erode', 'positive integer', erode); + } return this; } /** * Merge alpha transparency channel, if any, with a background, then remove the alpha channel. * + * See also {@link /api-channel#removealpha removeAlpha}. + * * @example * await sharp(rgbaInput) - * .flatten('#F0A703') + * .flatten({ background: '#F0A703' }) * .toBuffer(); * * @param {Object} [options] @@ -287,6 +504,32 @@ function flatten (options) { return this; } +/** + * Ensure the image has an alpha channel + * with all white pixel values made fully transparent. + * + * Existing alpha channel values for non-white pixels remain unchanged. + * + * This feature is experimental and the API may change. + * + * @since 0.32.1 + * + * @example + * await sharp(rgbInput) + * .unflatten() + * .toBuffer(); + * + * @example + * await sharp(rgbInput) + * .threshold(128, { grayscale: false }) // converter bright pixels to white + * .unflatten() + * .toBuffer(); + */ +function unflatten () { + this.options.unflatten = true; + return this; +} + /** * Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of `1/gamma` * then increasing the encoding (brighten) post-resize at a factor of `gamma`. @@ -323,31 +566,144 @@ function gamma (gamma, gammaOut) { /** * Produce the "negative" of the image. - * @param {Boolean} [negate=true] + * + * @example + * const output = await sharp(input) + * .negate() + * .toBuffer(); + * + * @example + * const output = await sharp(input) + * .negate({ alpha: false }) + * .toBuffer(); + * + * @param {Object} [options] + * @param {Boolean} [options.alpha=true] Whether or not to negate any alpha channel * @returns {Sharp} */ -function negate (negate) { - this.options.negate = is.bool(negate) ? negate : true; +function negate (options) { + this.options.negate = is.bool(options) ? options : true; + if (is.plainObject(options) && 'alpha' in options) { + if (!is.bool(options.alpha)) { + throw is.invalidParameterError('alpha', 'should be boolean value', options.alpha); + } else { + this.options.negateAlpha = options.alpha; + } + } return this; } /** - * Enhance output image contrast by stretching its luminance to cover the full dynamic range. - * @param {Boolean} [normalise=true] + * Enhance output image contrast by stretching its luminance to cover a full dynamic range. + * + * Uses a histogram-based approach, taking a default range of 1% to 99% to reduce sensitivity to noise at the extremes. + * + * Luminance values below the `lower` percentile will be underexposed by clipping to zero. + * Luminance values above the `upper` percentile will be overexposed by clipping to the max pixel value. + * + * @example + * const output = await sharp(input) + * .normalise() + * .toBuffer(); + * + * @example + * const output = await sharp(input) + * .normalise({ lower: 0, upper: 100 }) + * .toBuffer(); + * + * @param {Object} [options] + * @param {number} [options.lower=1] - Percentile below which luminance values will be underexposed. + * @param {number} [options.upper=99] - Percentile above which luminance values will be overexposed. * @returns {Sharp} */ -function normalise (normalise) { - this.options.normalise = is.bool(normalise) ? normalise : true; +function normalise (options) { + if (is.plainObject(options)) { + if (is.defined(options.lower)) { + if (is.number(options.lower) && is.inRange(options.lower, 0, 99)) { + this.options.normaliseLower = options.lower; + } else { + throw is.invalidParameterError('lower', 'number between 0 and 99', options.lower); + } + } + if (is.defined(options.upper)) { + if (is.number(options.upper) && is.inRange(options.upper, 1, 100)) { + this.options.normaliseUpper = options.upper; + } else { + throw is.invalidParameterError('upper', 'number between 1 and 100', options.upper); + } + } + } + if (this.options.normaliseLower >= this.options.normaliseUpper) { + throw is.invalidParameterError('range', 'lower to be less than upper', + `${this.options.normaliseLower} >= ${this.options.normaliseUpper}`); + } + this.options.normalise = true; return this; } /** * Alternative spelling of normalise. - * @param {Boolean} [normalize=true] + * + * @example + * const output = await sharp(input) + * .normalize() + * .toBuffer(); + * + * @param {Object} [options] + * @param {number} [options.lower=1] - Percentile below which luminance values will be underexposed. + * @param {number} [options.upper=99] - Percentile above which luminance values will be overexposed. * @returns {Sharp} */ -function normalize (normalize) { - return this.normalise(normalize); +function normalize (options) { + return this.normalise(options); +} + +/** + * Perform contrast limiting adaptive histogram equalization + * {@link https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE CLAHE}. + * + * This will, in general, enhance the clarity of the image by bringing out darker details. + * + * @since 0.28.3 + * + * @example + * const output = await sharp(input) + * .clahe({ + * width: 3, + * height: 3, + * }) + * .toBuffer(); + * + * @param {Object} options + * @param {number} options.width - Integral width of the search window, in pixels. + * @param {number} options.height - Integral height of the search window, in pixels. + * @param {number} [options.maxSlope=3] - Integral level of brightening, between 0 and 100, where 0 disables contrast limiting. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function clahe (options) { + if (is.plainObject(options)) { + if (is.integer(options.width) && options.width > 0) { + this.options.claheWidth = options.width; + } else { + throw is.invalidParameterError('width', 'integer greater than zero', options.width); + } + if (is.integer(options.height) && options.height > 0) { + this.options.claheHeight = options.height; + } else { + throw is.invalidParameterError('height', 'integer greater than zero', options.height); + } + if (is.defined(options.maxSlope)) { + if (is.integer(options.maxSlope) && is.inRange(options.maxSlope, 0, 100)) { + this.options.claheMaxSlope = options.maxSlope; + } else { + throw is.invalidParameterError('maxSlope', 'integer between 0 and 100', options.maxSlope); + } + } + } else { + throw is.invalidParameterError('options', 'plain object', options); + } + return this; } /** @@ -368,7 +724,7 @@ function normalize (normalize) { * * @param {Object} kernel * @param {number} kernel.width - width of the kernel in pixels. - * @param {number} kernel.height - width of the kernel in pixels. + * @param {number} kernel.height - height of the kernel in pixels. * @param {Array<number>} kernel.kernel - Array of length `width*height` containing the kernel values. * @param {number} [kernel.scale=sum] - the scale of the kernel in pixels. * @param {number} [kernel.offset=0] - the offset of the kernel in pixels. @@ -386,9 +742,7 @@ function convolve (kernel) { } // Default scale is sum of kernel values if (!is.integer(kernel.scale)) { - kernel.scale = kernel.kernel.reduce(function (a, b) { - return a + b; - }, 0); + kernel.scale = kernel.kernel.reduce((a, b) => a + b, 0); } // Clip scale to a minimum value of 1 if (kernel.scale < 1) { @@ -455,32 +809,61 @@ function boolean (operand, operator, options) { } /** - * Apply the linear formula a * input + b to the image (levels adjustment) - * @param {number} [a=1.0] multiplier - * @param {number} [b=0.0] offset + * Apply the linear formula `a` * input + `b` to the image to adjust image levels. + * + * When a single number is provided, it will be used for all image channels. + * When an array of numbers is provided, the array length must match the number of channels. + * + * @example + * await sharp(input) + * .linear(0.5, 2) + * .toBuffer(); + * + * @example + * await sharp(rgbInput) + * .linear( + * [0.25, 0.5, 0.75], + * [150, 100, 50] + * ) + * .toBuffer(); + * + * @param {(number|number[])} [a=[]] multiplier + * @param {(number|number[])} [b=[]] offset * @returns {Sharp} * @throws {Error} Invalid parameters */ function linear (a, b) { + if (!is.defined(a) && is.number(b)) { + a = 1.0; + } else if (is.number(a) && !is.defined(b)) { + b = 0.0; + } if (!is.defined(a)) { - this.options.linearA = 1.0; + this.options.linearA = []; } else if (is.number(a)) { + this.options.linearA = [a]; + } else if (Array.isArray(a) && a.length && a.every(is.number)) { this.options.linearA = a; } else { - throw is.invalidParameterError('a', 'numeric', a); + throw is.invalidParameterError('a', 'number or array of numbers', a); } if (!is.defined(b)) { - this.options.linearB = 0.0; + this.options.linearB = []; } else if (is.number(b)) { + this.options.linearB = [b]; + } else if (Array.isArray(b) && b.length && b.every(is.number)) { this.options.linearB = b; } else { - throw is.invalidParameterError('b', 'numeric', b); + throw is.invalidParameterError('b', 'number or array of numbers', b); + } + if (this.options.linearA.length !== this.options.linearB.length) { + throw new Error('Expected a and b to be arrays of the same length'); } return this; } /** - * Recomb the image with the specified matrix. + * Recombine the image with the specified matrix. * * @since 0.21.1 * @@ -493,59 +876,75 @@ function linear (a, b) { * ]) * .raw() * .toBuffer(function(err, data, info) { - * // data contains the raw pixel data after applying the recomb + * // data contains the raw pixel data after applying the matrix * // With this example input, a sepia filter has been applied * }); * - * @param {Array<Array<number>>} inputMatrix - 3x3 Recombination matrix + * @param {Array<Array<number>>} inputMatrix - 3x3 or 4x4 Recombination matrix * @returns {Sharp} * @throws {Error} Invalid parameters */ function recomb (inputMatrix) { - if (!Array.isArray(inputMatrix) || inputMatrix.length !== 3 || - inputMatrix[0].length !== 3 || - inputMatrix[1].length !== 3 || - inputMatrix[2].length !== 3 - ) { - // must pass in a kernel - throw new Error('Invalid recombination matrix'); + if (!Array.isArray(inputMatrix)) { + throw is.invalidParameterError('inputMatrix', 'array', inputMatrix); + } + if (inputMatrix.length !== 3 && inputMatrix.length !== 4) { + throw is.invalidParameterError('inputMatrix', '3x3 or 4x4 array', inputMatrix.length); } - this.options.recombMatrix = [ - inputMatrix[0][0], inputMatrix[0][1], inputMatrix[0][2], - inputMatrix[1][0], inputMatrix[1][1], inputMatrix[1][2], - inputMatrix[2][0], inputMatrix[2][1], inputMatrix[2][2] - ].map(Number); + const recombMatrix = inputMatrix.flat().map(Number); + if (recombMatrix.length !== 9 && recombMatrix.length !== 16) { + throw is.invalidParameterError('inputMatrix', 'cardinality of 9 or 16', recombMatrix.length); + } + this.options.recombMatrix = recombMatrix; return this; } /** - * Transforms the image using brightness, saturation and hue rotation. + * Transforms the image using brightness, saturation, hue rotation, and lightness. + * Brightness and lightness both operate on luminance, with the difference being that + * brightness is multiplicative whereas lightness is additive. * * @since 0.22.1 * * @example - * sharp(input) + * // increase brightness by a factor of 2 + * const output = await sharp(input) * .modulate({ - * brightness: 2 // increase lightness by a factor of 2 - * }); + * brightness: 2 + * }) + * .toBuffer(); * - * sharp(input) + * @example + * // hue-rotate by 180 degrees + * const output = await sharp(input) * .modulate({ - * hue: 180 // hue-rotate by 180 degrees - * }); + * hue: 180 + * }) + * .toBuffer(); * - * // decreate brightness and saturation while also hue-rotating by 90 degrees - * sharp(input) + * @example + * // increase lightness by +50 + * const output = await sharp(input) + * .modulate({ + * lightness: 50 + * }) + * .toBuffer(); + * + * @example + * // decrease brightness and saturation while also hue-rotating by 90 degrees + * const output = await sharp(input) * .modulate({ * brightness: 0.5, * saturation: 0.5, - * hue: 90 - * }); + * hue: 90, + * }) + * .toBuffer(); * * @param {Object} [options] * @param {number} [options.brightness] Brightness multiplier * @param {number} [options.saturation] Saturation multiplier * @param {number} [options.hue] Degrees for hue rotation + * @param {number} [options.lightness] Lightness addend * @returns {Sharp} */ function modulate (options) { @@ -573,27 +972,40 @@ function modulate (options) { throw is.invalidParameterError('hue', 'number', options.hue); } } + if ('lightness' in options) { + if (is.number(options.lightness)) { + this.options.lightness = options.lightness; + } else { + throw is.invalidParameterError('lightness', 'number', options.lightness); + } + } return this; } /** * Decorate the Sharp prototype with operation-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Object.assign(Sharp.prototype, { + autoOrient, rotate, flip, flop, affine, sharpen, + erode, + dilate, median, blur, flatten, + unflatten, gamma, negate, normalise, normalize, + clahe, convolve, threshold, boolean, diff --git a/lib/output.js b/lib/output.js index 762c43e9a..27a6ac470 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1,7 +1,11 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ +const path = require('node:path'); const is = require('./is'); -const sharp = require('../build/Release/sharp.node'); +const sharp = require('./sharp'); const formats = new Map([ ['heic', 'heif'], @@ -9,24 +13,39 @@ const formats = new Map([ ['avif', 'avif'], ['jpeg', 'jpeg'], ['jpg', 'jpeg'], + ['jpe', 'jpeg'], + ['tile', 'tile'], + ['dz', 'tile'], ['png', 'png'], ['raw', 'raw'], ['tiff', 'tiff'], + ['tif', 'tiff'], ['webp', 'webp'], - ['gif', 'gif'] + ['gif', 'gif'], + ['jp2', 'jp2'], + ['jpx', 'jp2'], + ['j2k', 'jp2'], + ['j2c', 'jp2'], + ['jxl', 'jxl'] ]); -const errMagickSave = new Error('GIF output requires libvips with support for ImageMagick'); +const jp2Regex = /\.(jp[2x]|j2[kc])$/i; + +const errJp2Save = () => new Error('JP2 output requires libvips with support for OpenJPEG'); + +const bitdepthFromColourCount = (colours) => 1 << 31 - Math.clz32(Math.ceil(Math.log2(colours))); /** * 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, AVIF, TIFF, DZI, and libvips' V format supported. + * with JPEG, PNG, WebP, AVIF, TIFF, GIF, 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. - * See {@link withMetadata} for control over this. + * See {@link #withmetadata withMetadata} for control over this. + * + * The caller is responsible for ensuring directory structures and permissions exist. * * A `Promise` is returned when `callback` is not provided. * @@ -45,6 +64,9 @@ const errMagickSave = new Error('GIF output requires libvips with support for Im * `info` contains the output image `format`, `size` (bytes), `width`, `height`, * `channels` and `premultiplied` (indicating if premultiplication was used). * When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. + * When using the attention crop strategy also contains `attentionX` and `attentionY`, the focal point of the cropped region. + * Animated output will also contain `pageHeight` and `pages`. + * May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text. * @returns {Promise<Object>} - when no callback is provided * @throws {Error} Invalid parameters */ @@ -52,10 +74,10 @@ function toFile (fileOut, callback) { let err; if (!is.string(fileOut)) { err = new Error('Missing output file path'); - } else if (this.options.input.file === fileOut) { + } else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(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; + } else if (jp2Regex.test(path.extname(fileOut)) && !this.constructor.format.jp2k.output.file) { + err = errJp2Save(); } if (err) { if (is.fn(callback)) { @@ -65,19 +87,22 @@ function toFile (fileOut, callback) { } } else { this.options.fileOut = fileOut; - return this._pipeline(callback); + const stack = Error(); + return this._pipeline(callback, stack); } return this; } /** * Write output to a Buffer. - * JPEG, PNG, WebP, AVIF, TIFF and raw pixel data output are supported. + * JPEG, PNG, WebP, AVIF, TIFF, GIF 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. + * Use {@link #toformat toFormat} or one of the format-specific functions such as {@link #jpeg jpeg}, {@link #png png} etc. to set the output format. + * + * If no explicit format is set, the output format will match the input image, except SVG input which becomes PNG output. * * By default all metadata will be removed, which includes EXIF-based orientation. - * See {@link withMetadata} for control over this. + * See {@link #withmetadata withMetadata} for control over this. * * `callback`, if present, gets three arguments `(err, data, info)` where: * - `err` is an error, if any. @@ -85,6 +110,8 @@ function toFile (fileOut, callback) { * - `info` contains the output image `format`, `size` (bytes), `width`, `height`, * `channels` and `premultiplied` (indicating if premultiplication was used). * When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. + * Animated output will also contain `pageHeight` and `pages`. + * May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text. * * A `Promise` is returned when `callback` is not provided. * @@ -100,6 +127,7 @@ function toFile (fileOut, callback) { * * @example * sharp(input) + * .png() * .toBuffer({ resolveWithObject: true }) * .then(({ data, info }) => { ... }) * .catch(err => { ... }); @@ -131,31 +159,266 @@ function toBuffer (options, callback) { } else if (this.options.resolveWithObject) { this.options.resolveWithObject = false; } - return this._pipeline(is.fn(options) ? options : callback); + this.options.fileOut = ''; + const stack = Error(); + return this._pipeline(is.fn(options) ? options : callback, stack); +} + +/** + * Keep all EXIF metadata from the input image in the output image. + * + * EXIF metadata is unsupported for TIFF output. + * + * @since 0.33.0 + * + * @example + * const outputWithExif = await sharp(inputWithExif) + * .keepExif() + * .toBuffer(); + * + * @returns {Sharp} + */ +function keepExif () { + this.options.keepMetadata |= 0b00001; + return this; +} + +/** + * Set EXIF metadata in the output image, ignoring any EXIF in the input image. + * + * @since 0.33.0 + * + * @example + * const dataWithExif = await sharp(input) + * .withExif({ + * IFD0: { + * Copyright: 'The National Gallery' + * }, + * IFD3: { + * GPSLatitudeRef: 'N', + * GPSLatitude: '51/1 30/1 3230/100', + * GPSLongitudeRef: 'W', + * GPSLongitude: '0/1 7/1 4366/100' + * } + * }) + * .toBuffer(); + * + * @param {Object<string, Object<string, string>>} exif Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function withExif (exif) { + if (is.object(exif)) { + for (const [ifd, entries] of Object.entries(exif)) { + if (is.object(entries)) { + for (const [k, v] of Object.entries(entries)) { + if (is.string(v)) { + this.options.withExif[`exif-${ifd.toLowerCase()}-${k}`] = v; + } else { + throw is.invalidParameterError(`${ifd}.${k}`, 'string', v); + } + } + } else { + throw is.invalidParameterError(ifd, 'object', entries); + } + } + } else { + throw is.invalidParameterError('exif', 'object', exif); + } + this.options.withExifMerge = false; + return this.keepExif(); +} + +/** + * Update EXIF metadata from the input image in the output image. + * + * @since 0.33.0 + * + * @example + * const dataWithMergedExif = await sharp(inputWithExif) + * .withExifMerge({ + * IFD0: { + * Copyright: 'The National Gallery' + * } + * }) + * .toBuffer(); + * + * @param {Object<string, Object<string, string>>} exif Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function withExifMerge (exif) { + this.withExif(exif); + this.options.withExifMerge = true; + return this; +} + +/** + * Keep ICC profile from the input image in the output image. + * + * When input and output colour spaces differ, use with {@link /api-colour/#tocolourspace toColourspace} and optionally {@link /api-colour/#pipelinecolourspace pipelineColourspace}. + * + * @since 0.33.0 + * + * @example + * const outputWithIccProfile = await sharp(inputWithIccProfile) + * .keepIccProfile() + * .toBuffer(); + * + * @example + * const cmykOutputWithIccProfile = await sharp(cmykInputWithIccProfile) + * .pipelineColourspace('cmyk') + * .toColourspace('cmyk') + * .keepIccProfile() + * .toBuffer(); + * + * @returns {Sharp} + */ +function keepIccProfile () { + this.options.keepMetadata |= 0b01000; + return this; +} + +/** + * Transform using an ICC profile and attach to the output image. + * + * This can either be an absolute filesystem path or + * built-in profile name (`srgb`, `p3`, `cmyk`). + * + * @since 0.33.0 + * + * @example + * const outputWithP3 = await sharp(input) + * .withIccProfile('p3') + * .toBuffer(); + * + * @param {string} icc - Absolute filesystem path to output ICC profile or built-in profile name (srgb, p3, cmyk). + * @param {Object} [options] + * @param {number} [options.attach=true] Should the ICC profile be included in the output image metadata? + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function withIccProfile (icc, options) { + if (is.string(icc)) { + this.options.withIccProfile = icc; + } else { + throw is.invalidParameterError('icc', 'string', icc); + } + this.keepIccProfile(); + if (is.object(options)) { + if (is.defined(options.attach)) { + if (is.bool(options.attach)) { + if (!options.attach) { + this.options.keepMetadata &= ~0b01000; + } + } else { + throw is.invalidParameterError('attach', 'boolean', options.attach); + } + } + } + return this; } /** - * Include all metadata (EXIF, XMP, IPTC) from the input image in the output image. - * This will also convert to and add a web-friendly sRGB ICC profile unless a custom - * output profile is provided. + * Keep XMP metadata from the input image in the output image. + * + * @since 0.34.3 + * + * @example + * const outputWithXmp = await sharp(inputWithXmp) + * .keepXmp() + * .toBuffer(); * - * The default behaviour, when `withMetadata` is not used, is to convert to the device-independent + * @returns {Sharp} + */ +function keepXmp () { + this.options.keepMetadata |= 0b00010; + return this; +} + +/** + * Set XMP metadata in the output image. + * + * Supported by PNG, JPEG, WebP, and TIFF output. + * + * @since 0.34.3 + * + * @example + * const xmpString = ` + * <?xml version="1.0"?> + * <x:xmpmeta xmlns:x="adobe:ns:meta/"> + * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + * <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"> + * <dc:creator><rdf:Seq><rdf:li>John Doe</rdf:li></rdf:Seq></dc:creator> + * </rdf:Description> + * </rdf:RDF> + * </x:xmpmeta>`; + * + * const data = await sharp(input) + * .withXmp(xmpString) + * .toBuffer(); + * + * @param {string} xmp String containing XMP metadata to be embedded in the output image. + * @returns {Sharp} + * @throws {Error} Invalid parameters + */ +function withXmp (xmp) { + if (is.string(xmp) && xmp.length > 0) { + this.options.withXmp = xmp; + this.options.keepMetadata |= 0b00010; + } else { + throw is.invalidParameterError('xmp', 'non-empty string', xmp); + } + return this; +} + +/** + * Keep all metadata (EXIF, ICC, XMP, IPTC) from the input image in the output image. + * + * The default behaviour, when `keepMetadata` is not used, is to convert to the device-independent * sRGB colour space and strip all metadata, including the removal of any ICC profile. * + * @since 0.33.0 + * + * @example + * const outputWithMetadata = await sharp(inputWithMetadata) + * .keepMetadata() + * .toBuffer(); + * + * @returns {Sharp} + */ +function keepMetadata () { + this.options.keepMetadata = 0b11111; + return this; +} + +/** + * Keep most metadata (EXIF, XMP, IPTC) from the input image in the output image. + * + * This will also convert to and add a web-friendly sRGB ICC profile if appropriate. + * + * Allows orientation and density to be set or updated. + * * @example - * sharp('input.jpg') + * const outputSrgbWithMetadata = await sharp(inputRgbWithMetadata) * .withMetadata() - * .toFile('output-with-metadata.jpg') - * .then(info => { ... }); + * .toBuffer(); + * + * @example + * // Set output metadata to 96 DPI + * const data = await sharp(input) + * .withMetadata({ density: 96 }) + * .toBuffer(); * * @param {Object} [options] - * @param {number} [options.orientation] value between 1 and 8, used to update the EXIF `Orientation` tag. - * @param {string} [options.icc] filesystem path to output ICC profile, defaults to sRGB. + * @param {number} [options.orientation] Used to update the EXIF `Orientation` tag, integer between 1 and 8. + * @param {number} [options.density] Number of pixels per inch (DPI). * @returns {Sharp} * @throws {Error} Invalid parameters */ function withMetadata (options) { - this.options.withMetadata = is.bool(options) ? options : true; + this.keepMetadata(); + this.withIccProfile('srgb'); if (is.object(options)) { if (is.defined(options.orientation)) { if (is.integer(options.orientation) && is.inRange(options.orientation, 1, 8)) { @@ -164,13 +427,19 @@ function withMetadata (options) { throw is.invalidParameterError('orientation', 'integer between 1 and 8', options.orientation); } } - if (is.defined(options.icc)) { - if (is.string(options.icc)) { - this.options.withMetadataIcc = options.icc; + if (is.defined(options.density)) { + if (is.number(options.density) && options.density > 0) { + this.options.withMetadataDensity = options.density; } else { - throw is.invalidParameterError('icc', 'string filesystem path to ICC profile', options.icc); + throw is.invalidParameterError('density', 'positive number', options.density); } } + if (is.defined(options.icc)) { + this.withIccProfile(options.icc); + } + if (is.defined(options.exif)) { + this.withExifMerge(options.exif); + } } return this; } @@ -297,10 +566,14 @@ function jpeg (options) { /** * Use these PNG options for output image. * - * By default, PNG output is full colour at 8 or 16 bits per pixel. + * By default, PNG output is full colour at 8 bits per pixel. + * * Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel. * Set `palette` to `true` for slower, indexed PNG output. * + * For 16 bits per pixel output, convert to `rgb16` via + * {@link /api-colour/#tocolourspace toColourspace}. + * * @example * // Convert any input to full colour PNG output * const data = await sharp(input) @@ -313,12 +586,20 @@ function jpeg (options) { * .png({ palette: true }) * .toBuffer(); * + * @example + * // Output 16 bits per pixel RGB(A) + * const data = await sharp(input) + * .toColourspace('rgb16') + * .png() + * .toBuffer(); + * * @param {Object} [options] * @param {boolean} [options.progressive=false] - use progressive (interlace) scan * @param {number} [options.compressionLevel=6] - zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest) * @param {boolean} [options.adaptiveFiltering=false] - use adaptive row filtering * @param {boolean} [options.palette=false] - quantise to a palette-based image with alpha transparency support * @param {number} [options.quality=100] - use the lowest number of colours needed to achieve given quality, sets `palette` to `true` + * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest), sets `palette` to `true` * @param {number} [options.colours=256] - maximum number of palette entries, sets `palette` to `true` * @param {number} [options.colors=256] - alternative spelling of `options.colours`, sets `palette` to `true` * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, sets `palette` to `true` @@ -341,9 +622,17 @@ function png (options) { if (is.defined(options.adaptiveFiltering)) { this._setBooleanOption('pngAdaptiveFiltering', options.adaptiveFiltering); } + const colours = options.colours || options.colors; + if (is.defined(colours)) { + if (is.integer(colours) && is.inRange(colours, 2, 256)) { + this.options.pngBitdepth = bitdepthFromColourCount(colours); + } else { + throw is.invalidParameterError('colours', 'integer between 2 and 256', colours); + } + } if (is.defined(options.palette)) { this._setBooleanOption('pngPalette', options.palette); - } else if (is.defined(options.quality) || is.defined(options.colours || options.colors) || is.defined(options.dither)) { + } else if ([options.quality, options.effort, options.colours, options.colors, options.dither].some(is.defined)) { this._setBooleanOption('pngPalette', true); } if (this.options.pngPalette) { @@ -354,12 +643,11 @@ function png (options) { throw is.invalidParameterError('quality', 'integer between 0 and 100', options.quality); } } - const colours = options.colours || options.colors; - if (is.defined(colours)) { - if (is.integer(colours) && is.inRange(colours, 2, 256)) { - this.options.pngColours = colours; + if (is.defined(options.effort)) { + if (is.integer(options.effort) && is.inRange(options.effort, 1, 10)) { + this.options.pngEffort = options.effort; } else { - throw is.invalidParameterError('colours', 'integer between 2 and 256', colours); + throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort); } } if (is.defined(options.dither)) { @@ -383,99 +671,276 @@ function png (options) { * .webp({ lossless: true }) * .toBuffer(); * + * @example + * // Optimise the file size of an animated WebP + * const outputWebp = await sharp(inputWebp, { animated: true }) + * .webp({ effort: 6 }) + * .toBuffer(); + * * @param {Object} [options] - output options * @param {number} [options.quality=80] - quality, integer 1-100 * @param {number} [options.alphaQuality=100] - quality of alpha layer, integer 0-100 * @param {boolean} [options.lossless=false] - use lossless compression mode * @param {boolean} [options.nearLossless=false] - use near_lossless compression mode * @param {boolean} [options.smartSubsample=false] - use high quality chroma subsampling - * @param {number} [options.reductionEffort=4] - level of CPU effort to reduce file size, integer 0-6 - * @param {number} [options.pageHeight] - page height for animated output + * @param {boolean} [options.smartDeblock=false] - auto-adjust the deblocking filter, can improve low contrast edges (slow) + * @param {string} [options.preset='default'] - named preset for preprocessing/filtering, one of: default, photo, picture, drawing, icon, text + * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 6 (slowest) * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation - * @param {number[]} [options.delay] - list of delays between animation frames (in milliseconds) + * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds) + * @param {boolean} [options.minSize=false] - prevent use of animation key frames to minimise file size (slow) + * @param {boolean} [options.mixed=false] - allow mixture of lossy and lossless animation frames (slow) * @param {boolean} [options.force=true] - force WebP output, otherwise attempt to use input format * @returns {Sharp} * @throws {Error} Invalid options */ function webp (options) { - if (is.object(options) && is.defined(options.quality)) { - if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) { - this.options.webpQuality = options.quality; - } else { - throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality); + if (is.object(options)) { + if (is.defined(options.quality)) { + if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) { + this.options.webpQuality = options.quality; + } else { + throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality); + } } - } - if (is.object(options) && is.defined(options.alphaQuality)) { - if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 0, 100)) { - this.options.webpAlphaQuality = options.alphaQuality; - } else { - throw is.invalidParameterError('alphaQuality', 'integer between 0 and 100', options.alphaQuality); + if (is.defined(options.alphaQuality)) { + if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 0, 100)) { + this.options.webpAlphaQuality = options.alphaQuality; + } else { + throw is.invalidParameterError('alphaQuality', 'integer between 0 and 100', options.alphaQuality); + } } - } - if (is.object(options) && is.defined(options.lossless)) { - this._setBooleanOption('webpLossless', options.lossless); - } - if (is.object(options) && is.defined(options.nearLossless)) { - this._setBooleanOption('webpNearLossless', options.nearLossless); - } - if (is.object(options) && is.defined(options.smartSubsample)) { - this._setBooleanOption('webpSmartSubsample', options.smartSubsample); - } - if (is.object(options) && is.defined(options.reductionEffort)) { - if (is.integer(options.reductionEffort) && is.inRange(options.reductionEffort, 0, 6)) { - this.options.webpReductionEffort = options.reductionEffort; - } else { - throw is.invalidParameterError('reductionEffort', 'integer between 0 and 6', options.reductionEffort); + if (is.defined(options.lossless)) { + this._setBooleanOption('webpLossless', options.lossless); + } + if (is.defined(options.nearLossless)) { + this._setBooleanOption('webpNearLossless', options.nearLossless); + } + if (is.defined(options.smartSubsample)) { + this._setBooleanOption('webpSmartSubsample', options.smartSubsample); + } + if (is.defined(options.smartDeblock)) { + this._setBooleanOption('webpSmartDeblock', options.smartDeblock); + } + if (is.defined(options.preset)) { + if (is.string(options.preset) && is.inArray(options.preset, ['default', 'photo', 'picture', 'drawing', 'icon', 'text'])) { + this.options.webpPreset = options.preset; + } else { + throw is.invalidParameterError('preset', 'one of: default, photo, picture, drawing, icon, text', options.preset); + } + } + if (is.defined(options.effort)) { + if (is.integer(options.effort) && is.inRange(options.effort, 0, 6)) { + this.options.webpEffort = options.effort; + } else { + throw is.invalidParameterError('effort', 'integer between 0 and 6', options.effort); + } + } + if (is.defined(options.minSize)) { + this._setBooleanOption('webpMinSize', options.minSize); + } + if (is.defined(options.mixed)) { + this._setBooleanOption('webpMixed', options.mixed); } } - trySetAnimationOptions(options, this.options); return this._updateFormatOut('webp', options); } /** - * Use these GIF options for output image. + * Use these GIF options for the output image. * - * Requires libvips compiled with support for ImageMagick or GraphicsMagick. - * The prebuilt binaries do not include this - see - * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}. + * The first entry in the palette is reserved for transparency. + * + * The palette of the input image will be re-used if possible. + * + * @since 0.30.0 + * + * @example + * // Convert PNG to GIF + * await sharp(pngBuffer) + * .gif() + * .toBuffer(); + * + * @example + * // Convert animated WebP to animated GIF + * await sharp('animated.webp', { animated: true }) + * .toFile('animated.gif'); + * + * @example + * // Create a 128x128, cropped, non-dithered, animated thumbnail of an animated GIF + * const out = await sharp('in.gif', { animated: true }) + * .resize({ width: 128, height: 128 }) + * .gif({ dither: 0 }) + * .toBuffer(); + * + * @example + * // Lossy file size reduction of animated GIF + * await sharp('in.gif', { animated: true }) + * .gif({ interFrameMaxError: 8 }) + * .toFile('optim.gif'); * * @param {Object} [options] - output options - * @param {number} [options.pageHeight] - page height for animated output + * @param {boolean} [options.reuse=true] - re-use existing palette, otherwise generate new (slow) + * @param {boolean} [options.progressive=false] - use progressive (interlace) scan + * @param {number} [options.colours=256] - maximum number of palette entries, including transparency, between 2 and 256 + * @param {number} [options.colors=256] - alternative spelling of `options.colours` + * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest) + * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, between 0 (least) and 1 (most) + * @param {number} [options.interFrameMaxError=0] - maximum inter-frame error for transparency, between 0 (lossless) and 32 + * @param {number} [options.interPaletteMaxError=3] - maximum inter-palette error for palette reuse, between 0 and 256 + * @param {boolean} [options.keepDuplicateFrames=false] - keep duplicate frames in the output instead of combining them * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation - * @param {number[]} [options.delay] - list of delays between animation frames (in milliseconds) + * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds) * @param {boolean} [options.force=true] - force GIF output, otherwise attempt to use input format * @returns {Sharp} * @throws {Error} Invalid options */ -/* istanbul ignore next */ function gif (options) { - if (!this.constructor.format.magick.output.buffer) { - throw errMagickSave; + if (is.object(options)) { + if (is.defined(options.reuse)) { + this._setBooleanOption('gifReuse', options.reuse); + } + if (is.defined(options.progressive)) { + this._setBooleanOption('gifProgressive', options.progressive); + } + const colours = options.colours || options.colors; + if (is.defined(colours)) { + if (is.integer(colours) && is.inRange(colours, 2, 256)) { + this.options.gifBitdepth = bitdepthFromColourCount(colours); + } else { + throw is.invalidParameterError('colours', 'integer between 2 and 256', colours); + } + } + if (is.defined(options.effort)) { + if (is.number(options.effort) && is.inRange(options.effort, 1, 10)) { + this.options.gifEffort = options.effort; + } else { + throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort); + } + } + if (is.defined(options.dither)) { + if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) { + this.options.gifDither = options.dither; + } else { + throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither); + } + } + if (is.defined(options.interFrameMaxError)) { + if (is.number(options.interFrameMaxError) && is.inRange(options.interFrameMaxError, 0, 32)) { + this.options.gifInterFrameMaxError = options.interFrameMaxError; + } else { + throw is.invalidParameterError('interFrameMaxError', 'number between 0.0 and 32.0', options.interFrameMaxError); + } + } + if (is.defined(options.interPaletteMaxError)) { + if (is.number(options.interPaletteMaxError) && is.inRange(options.interPaletteMaxError, 0, 256)) { + this.options.gifInterPaletteMaxError = options.interPaletteMaxError; + } else { + throw is.invalidParameterError('interPaletteMaxError', 'number between 0.0 and 256.0', options.interPaletteMaxError); + } + } + if (is.defined(options.keepDuplicateFrames)) { + if (is.bool(options.keepDuplicateFrames)) { + this._setBooleanOption('gifKeepDuplicateFrames', options.keepDuplicateFrames); + } else { + throw is.invalidParameterError('keepDuplicateFrames', 'boolean', options.keepDuplicateFrames); + } + } } trySetAnimationOptions(options, this.options); return this._updateFormatOut('gif', options); } +/** + * Use these JP2 options for output image. + * + * Requires libvips compiled with support for OpenJPEG. + * The prebuilt binaries do not include this - see + * {@link /install#custom-libvips installing a custom libvips}. + * + * @example + * // Convert any input to lossless JP2 output + * const data = await sharp(input) + * .jp2({ lossless: true }) + * .toBuffer(); + * + * @example + * // Convert any input to very high quality JP2 output + * const data = await sharp(input) + * .jp2({ + * quality: 100, + * chromaSubsampling: '4:4:4' + * }) + * .toBuffer(); + * + * @since 0.29.1 + * + * @param {Object} [options] - output options + * @param {number} [options.quality=80] - quality, integer 1-100 + * @param {boolean} [options.lossless=false] - use lossless compression mode + * @param {number} [options.tileWidth=512] - horizontal tile size + * @param {number} [options.tileHeight=512] - vertical tile size + * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling + * @returns {Sharp} + * @throws {Error} Invalid options + */ +function jp2 (options) { + /* node:coverage ignore next 41 */ + if (!this.constructor.format.jp2k.output.buffer) { + throw errJp2Save(); + } + if (is.object(options)) { + if (is.defined(options.quality)) { + if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) { + this.options.jp2Quality = options.quality; + } else { + throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality); + } + } + if (is.defined(options.lossless)) { + if (is.bool(options.lossless)) { + this.options.jp2Lossless = options.lossless; + } else { + throw is.invalidParameterError('lossless', 'boolean', options.lossless); + } + } + if (is.defined(options.tileWidth)) { + if (is.integer(options.tileWidth) && is.inRange(options.tileWidth, 1, 32768)) { + this.options.jp2TileWidth = options.tileWidth; + } else { + throw is.invalidParameterError('tileWidth', 'integer between 1 and 32768', options.tileWidth); + } + } + if (is.defined(options.tileHeight)) { + if (is.integer(options.tileHeight) && is.inRange(options.tileHeight, 1, 32768)) { + this.options.jp2TileHeight = options.tileHeight; + } else { + throw is.invalidParameterError('tileHeight', 'integer between 1 and 32768', options.tileHeight); + } + } + if (is.defined(options.chromaSubsampling)) { + if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) { + this.options.jp2ChromaSubsampling = options.chromaSubsampling; + } else { + throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling); + } + } + } + return this._updateFormatOut('jp2', options); +} + /** * Set animation options if available. * @private * * @param {Object} [source] - output options - * @param {number} [source.pageHeight] - page height for animated output * @param {number} [source.loop=0] - number of animation iterations, use 0 for infinite animation * @param {number[]} [source.delay] - list of delays between animation frames (in milliseconds) * @param {Object} [target] - target object for valid options * @throws {Error} Invalid options */ function trySetAnimationOptions (source, target) { - if (is.object(source) && is.defined(source.pageHeight)) { - if (is.integer(source.pageHeight) && source.pageHeight > 0) { - target.pageHeight = source.pageHeight; - } else { - throw is.invalidParameterError('pageHeight', 'integer larger than 0', source.pageHeight); - } - } if (is.object(source) && is.defined(source.loop)) { if (is.integer(source.loop) && is.inRange(source.loop, 0, 65535)) { target.loop = source.loop; @@ -484,13 +949,16 @@ function trySetAnimationOptions (source, target) { } } if (is.object(source) && is.defined(source.delay)) { - if ( + // We allow singular values as well + if (is.integer(source.delay) && is.inRange(source.delay, 0, 65535)) { + target.delay = [source.delay]; + } else if ( Array.isArray(source.delay) && source.delay.every(is.integer) && source.delay.every(v => is.inRange(v, 0, 65535))) { target.delay = source.delay; } else { - throw is.invalidParameterError('delay', 'array of integers between 0 and 65535', source.delay); + throw is.invalidParameterError('delay', 'integer or an array of integers between 0 and 65535', source.delay); } } } @@ -498,6 +966,9 @@ function trySetAnimationOptions (source, target) { /** * Use these TIFF options for output image. * + * The `density` can be set in pixels/inch via {@link #withmetadata withMetadata} + * instead of providing `xres` and `yres` in pixels/mm. + * * @example * // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output * sharp('input.svg') @@ -511,7 +982,8 @@ 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 {string} [options.compression='jpeg'] - compression options: lzw, deflate, jpeg, ccittfax4 + * @param {string} [options.compression='jpeg'] - compression options: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k + * @param {boolean} [options.bigtiff=false] - use BigTIFF variant (has no effect when compression is none) * @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 @@ -519,7 +991,9 @@ function trySetAnimationOptions (source, target) { * @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 {string} [options.resolutionUnit='inch'] - resolution unit options: inch, cm * @param {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit + * @param {boolean} [options.miniswhite=false] - write 1-bit images as miniswhite * @returns {Sharp} * @throws {Error} Invalid options */ @@ -557,6 +1031,10 @@ function tiff (options) { throw is.invalidParameterError('tileHeight', 'integer greater than zero', options.tileHeight); } } + // miniswhite + if (is.defined(options.miniswhite)) { + this._setBooleanOption('tiffMiniswhite', options.miniswhite); + } // pyramid if (is.defined(options.pyramid)) { this._setBooleanOption('tiffPyramid', options.pyramid); @@ -578,12 +1056,16 @@ function tiff (options) { } // compression if (is.defined(options.compression)) { - if (is.string(options.compression) && is.inArray(options.compression, ['lzw', 'deflate', 'jpeg', 'ccittfax4', 'none'])) { + if (is.string(options.compression) && is.inArray(options.compression, ['none', 'jpeg', 'deflate', 'packbits', 'ccittfax4', 'lzw', 'webp', 'zstd', 'jp2k'])) { this.options.tiffCompression = options.compression; } else { - throw is.invalidParameterError('compression', 'one of: lzw, deflate, jpeg, ccittfax4, none', options.compression); + throw is.invalidParameterError('compression', 'one of: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k', options.compression); } } + // bigtiff + if (is.defined(options.bigtiff)) { + this._setBooleanOption('tiffBigtiff', options.bigtiff); + } // predictor if (is.defined(options.predictor)) { if (is.string(options.predictor) && is.inArray(options.predictor, ['none', 'horizontal', 'float'])) { @@ -592,6 +1074,14 @@ function tiff (options) { throw is.invalidParameterError('predictor', 'one of: none, horizontal, float', options.predictor); } } + // resolutionUnit + if (is.defined(options.resolutionUnit)) { + if (is.string(options.resolutionUnit) && is.inArray(options.resolutionUnit, ['inch', 'cm'])) { + this.options.tiffResolutionUnit = options.resolutionUnit; + } else { + throw is.invalidParameterError('resolutionUnit', 'one of: inch, cm', options.resolutionUnit); + } + } } return this._updateFormatOut('tiff', options); } @@ -599,16 +1089,30 @@ function 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. + * AVIF image sequences are not supported. + * Prebuilt binaries support a bitdepth of 8 only. + * + * This feature is experimental on the Windows ARM64 platform + * and requires a CPU with ARM64v8.4 or later. + * + * @example + * const data = await sharp(input) + * .avif({ effort: 2 }) + * .toBuffer(); + * + * @example + * const data = await sharp(input) + * .avif({ lossless: true }) + * .toBuffer(); * * @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 + * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 9 (slowest) + * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling + * @param {number} [options.bitdepth=8] - set bitdepth to 8, 10 or 12 bit * @returns {Sharp} * @throws {Error} Invalid options */ @@ -619,22 +1123,33 @@ function avif (options) { /** * Use these HEIF options for output image. * - * Support for patent-encumbered HEIC images requires the use of a + * Support for patent-encumbered HEIC images using `hevc` compression requires the use of a * globally-installed libvips compiled with support for libheif, libde265 and x265. * + * @example + * const data = await sharp(input) + * .heif({ compression: 'hevc' }) + * .toBuffer(); + * * @since 0.23.0 * - * @param {Object} [options] - output options + * @param {Object} options - output options + * @param {string} options.compression - compression format: av1, hevc * @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 + * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 9 (slowest) + * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling + * @param {number} [options.bitdepth=8] - set bitdepth to 8, 10 or 12 bit * @returns {Sharp} * @throws {Error} Invalid options */ function heif (options) { if (is.object(options)) { + if (is.string(options.compression) && is.inArray(options.compression, ['av1', 'hevc'])) { + this.options.heifCompression = options.compression; + } else { + throw is.invalidParameterError('compression', 'one of: av1, hevc', options.compression); + } if (is.defined(options.quality)) { if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) { this.options.heifQuality = options.quality; @@ -649,18 +1164,11 @@ function heif (options) { throw is.invalidParameterError('lossless', 'boolean', options.lossless); } } - if (is.defined(options.compression)) { - if (is.string(options.compression) && is.inArray(options.compression, ['av1', 'hevc'])) { - this.options.heifCompression = options.compression; - } else { - 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; + if (is.defined(options.effort)) { + if (is.integer(options.effort) && is.inRange(options.effort, 0, 9)) { + this.options.heifEffort = options.effort; } else { - throw is.invalidParameterError('speed', 'integer between 0 and 8', options.speed); + throw is.invalidParameterError('effort', 'integer between 0 and 9', options.effort); } } if (is.defined(options.chromaSubsampling)) { @@ -670,42 +1178,135 @@ function heif (options) { throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling); } } + if (is.defined(options.bitdepth)) { + if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [8, 10, 12])) { + if (options.bitdepth !== 8 && this.constructor.versions.heif) { + throw is.invalidParameterError('bitdepth when using prebuilt binaries', 8, options.bitdepth); + } + this.options.heifBitdepth = options.bitdepth; + } else { + throw is.invalidParameterError('bitdepth', '8, 10 or 12', options.bitdepth); + } + } + } else { + throw is.invalidParameterError('options', 'Object', options); } return this._updateFormatOut('heif', options); } /** - * Force output to be raw, uncompressed, 8-bit unsigned integer (unit8) pixel data. + * Use these JPEG-XL (JXL) options for output image. + * + * This feature is experimental, please do not use in production systems. + * + * Requires libvips compiled with support for libjxl. + * The prebuilt binaries do not include this - see + * {@link /install/#custom-libvips installing a custom libvips}. + * + * @since 0.31.3 + * + * @param {Object} [options] - output options + * @param {number} [options.distance=1.0] - maximum encoding error, between 0 (highest quality) and 15 (lowest quality) + * @param {number} [options.quality] - calculate `distance` based on JPEG-like quality, between 1 and 100, overrides distance if specified + * @param {number} [options.decodingTier=0] - target decode speed tier, between 0 (highest quality) and 4 (lowest quality) + * @param {boolean} [options.lossless=false] - use lossless compression + * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 9 (slowest) + * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation + * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds) + * @returns {Sharp} + * @throws {Error} Invalid options + */ +function jxl (options) { + if (is.object(options)) { + if (is.defined(options.quality)) { + if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) { + // https://github.com/libjxl/libjxl/blob/0aeea7f180bafd6893c1db8072dcb67d2aa5b03d/tools/cjxl_main.cc#L640-L644 + this.options.jxlDistance = options.quality >= 30 + ? 0.1 + (100 - options.quality) * 0.09 + : 53 / 3000 * options.quality * options.quality - 23 / 20 * options.quality + 25; + } else { + throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality); + } + } else if (is.defined(options.distance)) { + if (is.number(options.distance) && is.inRange(options.distance, 0, 15)) { + this.options.jxlDistance = options.distance; + } else { + throw is.invalidParameterError('distance', 'number between 0.0 and 15.0', options.distance); + } + } + if (is.defined(options.decodingTier)) { + if (is.integer(options.decodingTier) && is.inRange(options.decodingTier, 0, 4)) { + this.options.jxlDecodingTier = options.decodingTier; + } else { + throw is.invalidParameterError('decodingTier', 'integer between 0 and 4', options.decodingTier); + } + } + if (is.defined(options.lossless)) { + if (is.bool(options.lossless)) { + this.options.jxlLossless = options.lossless; + } else { + throw is.invalidParameterError('lossless', 'boolean', options.lossless); + } + } + if (is.defined(options.effort)) { + if (is.integer(options.effort) && is.inRange(options.effort, 1, 9)) { + this.options.jxlEffort = options.effort; + } else { + throw is.invalidParameterError('effort', 'integer between 1 and 9', options.effort); + } + } + } + trySetAnimationOptions(options, this.options); + return this._updateFormatOut('jxl', options); +} + +/** + * Force output to be raw, uncompressed pixel data. * Pixel ordering is left-to-right, top-to-bottom, without padding. * Channel ordering will be RGB or RGBA for non-greyscale colourspaces. * * @example - * // Extract raw RGB pixel data from JPEG input + * // Extract raw, unsigned 8-bit RGB pixel data from JPEG input * const { data, info } = await sharp('input.jpg') * .raw() * .toBuffer({ resolveWithObject: true }); * * @example - * // Extract alpha channel as raw pixel data from PNG input + * // Extract alpha channel as raw, unsigned 16-bit pixel data from PNG input * const data = await sharp('input.png') * .ensureAlpha() * .extractChannel(3) * .toColourspace('b-w') - * .raw() + * .raw({ depth: 'ushort' }) * .toBuffer(); * + * @param {Object} [options] - output options + * @param {string} [options.depth='uchar'] - bit depth, one of: char, uchar (default), short, ushort, int, uint, float, complex, double, dpcomplex * @returns {Sharp} + * @throws {Error} Invalid options */ -function raw () { +function raw (options) { + if (is.object(options)) { + if (is.defined(options.depth)) { + if (is.string(options.depth) && is.inArray(options.depth, + ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'float', 'complex', 'double', 'dpcomplex'] + )) { + this.options.rawDepth = options.depth; + } else { + throw is.invalidParameterError('depth', 'one of: char, uchar, short, ushort, int, uint, float, complex, double, dpcomplex', options.depth); + } + } + } return this._updateFormatOut('raw'); } /** * Use tile-based deep zoom (image pyramid) output. + * * Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions. * Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format. * - * Warning: multiple sharp instances concurrently producing tile output can expose a possible race condition in some versions of libgsf. + * The container will be set to `zip` when the output is a Buffer or Stream, otherwise it will default to `fs`. * * @example * sharp('input.tiff') @@ -718,18 +1319,30 @@ function raw () { * // output_files contains 512x512 tiles grouped by zoom level * }); * + * @example + * const zipFileWithTiles = await sharp(input) + * .tile({ basename: "tiles" }) + * .toBuffer(); + * + * @example + * const iiififier = sharp().tile({ layout: "iiif" }); + * readableStream + * .pipe(iiififier) + * .pipe(writeableStream); + * * @param {Object} [options] * @param {number} [options.size=256] tile size in pixels, a value between 1 and 8192. * @param {number} [options.overlap=0] tile overlap in pixels, a value between 0 and 8192. * @param {number} [options.angle=0] tile angle of rotation, must be a multiple of 90. * @param {string|Object} [options.background={r: 255, g: 255, b: 255, alpha: 1}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to white without transparency. * @param {string} [options.depth] how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout. - * @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 {number} [options.skipBlanks=-1] Threshold to skip tile generation. Range is 0-255 for 8-bit images, 0-65535 for 16-bit images. Default is 5 for `google` layout, -1 (no skip) otherwise. * @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 {string} [options.layout='dz'] filesystem layout, possible values are `dz`, `iiif`, `iiif3`, `zoomify` or `google`. * @param {boolean} [options.centre=false] centre image in tile. * @param {boolean} [options.center=false] alternative spelling of centre. - * @param {string} [options.id='https://example.com/iiif'] when `layout` is `iiif`, sets the `@id` attribute of `info.json` + * @param {string} [options.id='https://example.com/iiif'] when `layout` is `iiif`/`iiif3`, sets the `@id`/`id` attribute of `info.json` + * @param {string} [options.basename] the name of the directory within the zip file when container is `zip`. * @returns {Sharp} * @throws {Error} Invalid parameters */ @@ -764,10 +1377,10 @@ function tile (options) { } // Layout if (is.defined(options.layout)) { - if (is.string(options.layout) && is.inArray(options.layout, ['dz', 'google', 'iiif', 'zoomify'])) { + if (is.string(options.layout) && is.inArray(options.layout, ['dz', 'google', 'iiif', 'iiif3', 'zoomify'])) { this.options.tileLayout = options.layout; } else { - throw is.invalidParameterError('layout', 'one of: dz, google, iiif, zoomify', options.layout); + throw is.invalidParameterError('layout', 'one of: dz, google, iiif, iiif3, zoomify', options.layout); } } // Angle of rotation, @@ -811,6 +1424,14 @@ function tile (options) { throw is.invalidParameterError('id', 'string', options.id); } } + // Basename for zip container + if (is.defined(options.basename)) { + if (is.string(options.basename)) { + this.options.tileBasename = options.basename; + } else { + throw is.invalidParameterError('basename', 'string', options.basename); + } + } } // Format if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) { @@ -821,6 +1442,42 @@ function tile (options) { return this._updateFormatOut('dz'); } +/** + * Set a timeout for processing, in seconds. + * Use a value of zero to continue processing indefinitely, the default behaviour. + * + * The clock starts when libvips opens an input image for processing. + * Time spent waiting for a libuv thread to become available is not included. + * + * @example + * // Ensure processing takes no longer than 3 seconds + * try { + * const data = await sharp(input) + * .blur(1000) + * .timeout({ seconds: 3 }) + * .toBuffer(); + * } catch (err) { + * if (err.message.includes('timeout')) { ... } + * } + * + * @since 0.29.2 + * + * @param {Object} options + * @param {number} options.seconds - Number of seconds after which processing will be stopped + * @returns {Sharp} + */ +function timeout (options) { + if (!is.plainObject(options)) { + throw is.invalidParameterError('options', 'object', options); + } + if (is.integer(options.seconds) && is.inRange(options.seconds, 0, 3600)) { + this.options.timeoutSeconds = options.seconds; + } else { + throw is.invalidParameterError('seconds', 'integer between 0 and 3600', options.seconds); + } + return this; +} + /** * Update the output format unless options.force is false, * in which case revert to input format. @@ -857,10 +1514,10 @@ function _setBooleanOption (key, val) { * @private */ function _read () { - /* istanbul ignore else */ if (!this.options.streamOut) { this.options.streamOut = true; - this._pipeline(); + const stack = Error(); + this._pipeline(undefined, stack); } } @@ -869,18 +1526,30 @@ function _read () { * Supports callback, stream and promise variants * @private */ -function _pipeline (callback) { +function _pipeline (callback, stack) { if (typeof callback === 'function') { // output=file/buffer if (this._isStreamInput()) { // output=file/buffer, input=stream this.on('finish', () => { this._flattenBufferIn(); - sharp.pipeline(this.options, callback); + sharp.pipeline(this.options, (err, data, info) => { + if (err) { + callback(is.nativeError(err, stack)); + } else { + callback(null, data, info); + } + }); }); } else { // output=file/buffer, input=file/buffer - sharp.pipeline(this.options, callback); + sharp.pipeline(this.options, (err, data, info) => { + if (err) { + callback(is.nativeError(err, stack)); + } else { + callback(null, data, info); + } + }); } return this; } else if (this.options.streamOut) { @@ -891,12 +1560,13 @@ function _pipeline (callback) { this._flattenBufferIn(); sharp.pipeline(this.options, (err, data, info) => { if (err) { - this.emit('error', err); + this.emit('error', is.nativeError(err, stack)); } else { this.emit('info', info); this.push(data); } this.push(null); + this.on('end', () => this.emit('close')); }); }); if (this.streamInFinished) { @@ -906,12 +1576,13 @@ function _pipeline (callback) { // output=stream, input=file/buffer sharp.pipeline(this.options, (err, data, info) => { if (err) { - this.emit('error', err); + this.emit('error', is.nativeError(err, stack)); } else { this.emit('info', info); this.push(data); } this.push(null); + this.on('end', () => this.emit('close')); }); } return this; @@ -924,7 +1595,7 @@ function _pipeline (callback) { this._flattenBufferIn(); sharp.pipeline(this.options, (err, data, info) => { if (err) { - reject(err); + reject(is.nativeError(err, stack)); } else { if (this.options.resolveWithObject) { resolve({ data, info }); @@ -940,10 +1611,10 @@ function _pipeline (callback) { return new Promise((resolve, reject) => { sharp.pipeline(this.options, (err, data, info) => { if (err) { - reject(err); + reject(is.nativeError(err, stack)); } else { if (this.options.resolveWithObject) { - resolve({ data: data, info: info }); + resolve({ data, info }); } else { resolve(data); } @@ -956,24 +1627,36 @@ function _pipeline (callback) { /** * Decorate the Sharp prototype with output-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Object.assign(Sharp.prototype, { // Public toFile, toBuffer, + keepExif, + withExif, + withExifMerge, + keepIccProfile, + withIccProfile, + keepXmp, + withXmp, + keepMetadata, withMetadata, toFormat, jpeg, + jp2, png, webp, tiff, avif, heif, + jxl, gif, raw, tile, + timeout, // Private _updateFormatOut, _setBooleanOption, diff --git a/lib/platform.js b/lib/platform.js deleted file mode 100644 index 383e6b6ab..000000000 --- a/lib/platform.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; - -const detectLibc = require('detect-libc'); - -const env = process.env; - -module.exports = function () { - const arch = env.npm_config_arch || process.arch; - const platform = env.npm_config_platform || process.platform; - /* istanbul ignore next */ - const libc = (platform === 'linux' && detectLibc.isNonGlibcLinux) ? detectLibc.family : ''; - - const platformId = [`${platform}${libc}`]; - - if (arch === 'arm') { - const fallback = process.versions.electron ? '7' : '6'; - platformId.push(`armv${env.npm_config_arm_version || process.config.variables.arm_version || fallback}`); - } else if (arch === 'arm64') { - platformId.push(`arm64v${env.npm_config_arm_version || '8'}`); - } else { - platformId.push(arch); - } - - return platformId.join('-'); -}; diff --git a/lib/resize.js b/lib/resize.js index 9843910f7..544fbba3a 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -1,4 +1,7 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const is = require('./is'); @@ -36,6 +39,18 @@ const position = { 'left top': 8 }; +/** + * How to extend the image. + * @member + * @private + */ +const extendWith = { + background: 'background', + copy: 'copy', + repeat: 'repeat', + mirror: 'mirror' +}; + /** * Strategies for automagic cover behaviour. * @member @@ -53,10 +68,13 @@ const strategy = { */ const kernel = { nearest: 'nearest', + linear: 'linear', cubic: 'cubic', mitchell: 'mitchell', lanczos2: 'lanczos2', - lanczos3: 'lanczos3' + lanczos3: 'lanczos3', + mks2013: 'mks2013', + mks2021: 'mks2021' }; /** @@ -89,14 +107,21 @@ const mapFitToCanvas = { * @private */ function isRotationExpected (options) { - return (options.angle % 360) !== 0 || options.useExifOrientation === true || options.rotationAngle !== 0; + return (options.angle % 360) !== 0 || options.rotationAngle !== 0; +} + +/** + * @private + */ +function isResizeExpected (options) { + return options.width !== -1 || options.height !== -1; } /** * Resize image to `width`, `height` or `width x height`. * * When both a `width` and `height` are provided, the possible methods by which the image should **fit** these are: - * - `cover`: (default) Preserving aspect ratio, ensure the image covers both provided dimensions by cropping/clipping to fit. + * - `cover`: (default) Preserving aspect ratio, attempt to ensure the image covers both provided dimensions by cropping/clipping to fit. * - `contain`: Preserving aspect ratio, contain within both provided dimensions using "letterboxing" where necessary. * - `fill`: Ignore the aspect ratio of the input and stretch to both provided dimensions. * - `inside`: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified. @@ -104,24 +129,35 @@ function isRotationExpected (options) { * * Some of these values are based on the [object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) CSS property. * - * When using a `fit` of `cover` or `contain`, the default **position** is `centre`. Other options are: + * <img alt="Examples of various values for the fit property when resizing" width="100%" style="aspect-ratio: 998/243" src="/api-resize-fit.svg"> + * + * When using a **fit** of `cover` or `contain`, the default **position** is `centre`. Other options are: * - `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`. * - `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`. * - `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy. * * Some of these values are based on the [object-position](https://developer.mozilla.org/en-US/docs/Web/CSS/object-position) CSS property. * - * The experimental strategy-based approach resizes so one dimension is at its target length + * The strategy-based approach initially resizes so one dimension is at its target length * then repeatedly ranks edge regions, discarding the edge with the lowest score based on the selected strategy. * - `entropy`: focus on the region with the highest [Shannon entropy](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29). * - `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones. * - * Possible interpolation kernels are: + * Possible downsizing kernels are: * - `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation). + * - `linear`: Use a [triangle filter](https://en.wikipedia.org/wiki/Triangular_function). * - `cubic`: Use a [Catmull-Rom spline](https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline). * - `mitchell`: Use a [Mitchell-Netravali spline](https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf). * - `lanczos2`: Use a [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel) with `a=2`. * - `lanczos3`: Use a Lanczos kernel with `a=3` (the default). + * - `mks2013`: Use a [Magic Kernel Sharp](https://johncostella.com/magic/mks.pdf) 2013 kernel, as adopted by Facebook. + * - `mks2021`: Use a Magic Kernel Sharp 2021 kernel, with more accurate (reduced) sharpening than the 2013 version. + * + * When upsampling, these kernels map to `nearest`, `linear` and `cubic` interpolators. + * Downsampling kernels without a matching upsampling interpolator map to `cubic`. + * + * Only one resize can occur per pipeline. + * Previous calls to `resize` in the same pipeline will be ignored. * * @example * sharp(input) @@ -183,6 +219,20 @@ function isRotationExpected (options) { * }); * * @example + * sharp(input) + * .resize(200, 200, { + * fit: sharp.fit.outside, + * withoutReduction: true + * }) + * .toFormat('jpeg') + * .toBuffer() + * .then(function(outputBuffer) { + * // outputBuffer contains JPEG image data + * // of at least 200 pixels wide and 200 pixels high while maintaining aspect ratio + * // and no smaller than the input image + * }); + * + * @example * const scaleByHalf = await sharp(input) * .metadata() * .then(({ width }) => sharp(input) @@ -190,28 +240,35 @@ function isRotationExpected (options) { * .toBuffer() * ); * - * @param {number} [width] - pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. - * @param {number} [height] - pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. + * @param {number} [width] - How many pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. + * @param {number} [height] - How many pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. * @param {Object} [options] - * @param {String} [options.width] - alternative means of specifying `width`. If both are present this take priority. - * @param {String} [options.height] - alternative means of specifying `height`. If both are present this take priority. - * @param {String} [options.fit='cover'] - how the image should be resized to fit both provided dimensions, one of `cover`, `contain`, `fill`, `inside` or `outside`. - * @param {String} [options.position='centre'] - position, gravity or strategy to use when `fit` is `cover` or `contain`. - * @param {String|Object} [options.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour when using a `fit` of `contain`, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency. - * @param {String} [options.kernel='lanczos3'] - the kernel to use for image reduction. - * @param {Boolean} [options.withoutEnlargement=false] - do not enlarge if the width *or* height are already less than the specified dimensions, equivalent to GraphicsMagick's `>` geometry option. - * @param {Boolean} [options.fastShrinkOnLoad=true] - take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. + * @param {number} [options.width] - An alternative means of specifying `width`. If both are present this takes priority. + * @param {number} [options.height] - An alternative means of specifying `height`. If both are present this takes priority. + * @param {String} [options.fit='cover'] - How the image should be resized/cropped to fit the target dimension(s), one of `cover`, `contain`, `fill`, `inside` or `outside`. + * @param {String} [options.position='centre'] - A position, gravity or strategy to use when `fit` is `cover` or `contain`. + * @param {String|Object} [options.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour when `fit` is `contain`, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency. + * @param {String} [options.kernel='lanczos3'] - The kernel to use for image reduction and the inferred interpolator to use for upsampling. Use the `fastShrinkOnLoad` option to control kernel vs shrink-on-load. + * @param {Boolean} [options.withoutEnlargement=false] - Do not scale up if the width *or* height are already less than the target dimensions, equivalent to GraphicsMagick's `>` geometry option. This may result in output dimensions smaller than the target dimensions. + * @param {Boolean} [options.withoutReduction=false] - Do not scale down if the width *or* height are already greater than the target dimensions, equivalent to GraphicsMagick's `<` geometry option. This may still result in a crop to reach the target dimensions. + * @param {Boolean} [options.fastShrinkOnLoad=true] - Take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern or round-down of an auto-scaled dimension. * @returns {Sharp} * @throws {Error} Invalid parameters */ -function resize (width, height, options) { - if (is.defined(width)) { - if (is.object(width) && !is.defined(options)) { - options = width; - } else if (is.integer(width) && width > 0) { - this.options.width = width; +function resize (widthOrOptions, height, options) { + if (isResizeExpected(this.options)) { + this.options.debuglog('ignoring previous resize options'); + } + if (this.options.widthPost !== -1) { + this.options.debuglog('operation order will be: extract, resize, extract'); + } + if (is.defined(widthOrOptions)) { + if (is.object(widthOrOptions) && !is.defined(options)) { + options = widthOrOptions; + } else if (is.integer(widthOrOptions) && widthOrOptions > 0) { + this.options.width = widthOrOptions; } else { - throw is.invalidParameterError('width', 'positive integer', width); + throw is.invalidParameterError('width', 'positive integer', widthOrOptions); } } else { this.options.width = -1; @@ -276,16 +333,24 @@ function resize (width, height, options) { if (is.defined(options.withoutEnlargement)) { this._setBooleanOption('withoutEnlargement', options.withoutEnlargement); } + // Without reduction + if (is.defined(options.withoutReduction)) { + this._setBooleanOption('withoutReduction', options.withoutReduction); + } // Shrink on load if (is.defined(options.fastShrinkOnLoad)) { this._setBooleanOption('fastShrinkOnLoad', options.fastShrinkOnLoad); } } + if (isRotationExpected(this.options) && isResizeExpected(this.options)) { + this.options.rotateBefore = true; + } return this; } /** - * Extends/pads the edges of the image with the provided background colour. + * Extend / pad / extrude one or more edges of the image with either + * the provided background colour or pixels derived from the image. * This operation will always occur after resizing and extraction, if any. * * @example @@ -311,11 +376,21 @@ function resize (width, height, options) { * }) * ... * + * @example + * // Extrude image by 8 pixels to the right, mirroring existing right hand edge + * sharp(input) + * .extend({ + * right: 8, + * background: 'mirror' + * }) + * ... + * * @param {(number|Object)} extend - single pixel count to add to all edges or an Object with per-edge counts * @param {number} [extend.top=0] * @param {number} [extend.left=0] * @param {number} [extend.bottom=0] * @param {number} [extend.right=0] + * @param {String} [extend.extendWith='background'] - populate new pixels using this method, one of: background, copy, repeat, mirror. * @param {String|Object} [extend.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency. * @returns {Sharp} * @throws {Error} Invalid parameters @@ -356,6 +431,13 @@ function extend (extend) { } } this._setBackgroundColourOption('extendBackground', extend.background); + if (is.defined(extend.extendWith)) { + if (is.string(extendWith[extend.extendWith])) { + this.options.extendWith = extendWith[extend.extendWith]; + } else { + throw is.invalidParameterError('extendWith', 'one of: background, copy, repeat, mirror', extend.extendWith); + } + } } else { throw is.invalidParameterError('extend', 'integer or object', extend); } @@ -367,7 +449,7 @@ function extend (extend) { * * - Use `extract` before `resize` for pre-resize extraction. * - Use `extract` after `resize` for post-resize extraction. - * - Use `extract` before and after for both. + * - Use `extract` twice and `resize` once for extract-then-resize-then-extract in a fixed operation order. * * @example * sharp(input) @@ -393,7 +475,10 @@ function extend (extend) { * @throws {Error} Invalid parameters */ function extract (options) { - const suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post'; + const suffix = isResizeExpected(this.options) || this.options.widthPre !== -1 ? 'Post' : 'Pre'; + if (this.options[`width${suffix}`] !== -1) { + this.options.debuglog('ignoring previous extract options'); + } ['left', 'top', 'width', 'height'].forEach(function (name) { const value = options[name]; if (is.integer(value) && value >= 0) { @@ -403,42 +488,98 @@ function extract (options) { } }, this); // Ensure existing rotation occurs before pre-resize extraction - if (suffix === 'Pre' && isRotationExpected(this.options)) { - this.options.rotateBeforePreExtract = true; + if (isRotationExpected(this.options) && !isResizeExpected(this.options)) { + if (this.options.widthPre === -1 || this.options.widthPost === -1) { + this.options.rotateBefore = true; + } + } + if (this.options.input.autoOrient) { + this.options.orientBefore = true; } return this; } /** - * 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. + * Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel. + * + * Images with an alpha channel will use the combined bounding box of alpha and non-alpha channels. + * + * If the result of this operation would trim an image to nothing then no change is made. + * + * The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties. + * + * @example + * // Trim pixels with a colour similar to that of the top-left pixel. + * await sharp(input) + * .trim() + * .toFile(output); * - * The `info` response Object, obtained from callback of `.toFile()` or `.toBuffer()`, - * will contain `trimOffsetLeft` and `trimOffsetTop` properties. + * @example + * // Trim pixels with the exact same colour as that of the top-left pixel. + * await sharp(input) + * .trim({ + * threshold: 0 + * }) + * .toFile(output); + * + * @example + * // Assume input is line art and trim only pixels with a similar colour to red. + * const output = await sharp(input) + * .trim({ + * background: "#FF0000", + * lineArt: true + * }) + * .toBuffer(); + * + * @example + * // Trim all "yellow-ish" pixels, being more lenient with the higher threshold. + * const output = await sharp(input) + * .trim({ + * background: "yellow", + * threshold: 42, + * }) + * .toBuffer(); * - * @param {number} [threshold=10] the allowed difference from the top-left pixel, a number greater than zero. + * @param {Object} [options] + * @param {string|Object} [options.background='top-left pixel'] - Background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to that of the top-left pixel. + * @param {number} [options.threshold=10] - Allowed difference from the above colour, a positive number. + * @param {boolean} [options.lineArt=false] - Does the input more closely resemble line art (e.g. vector) rather than being photographic? * @returns {Sharp} * @throws {Error} Invalid parameters */ -function trim (threshold) { - if (!is.defined(threshold)) { - this.options.trimThreshold = 10; - } else if (is.number(threshold) && threshold > 0) { - this.options.trimThreshold = threshold; - } else { - throw is.invalidParameterError('threshold', 'number greater than zero', threshold); +function trim (options) { + this.options.trimThreshold = 10; + if (is.defined(options)) { + if (is.object(options)) { + if (is.defined(options.background)) { + this._setBackgroundColourOption('trimBackground', options.background); + } + if (is.defined(options.threshold)) { + if (is.number(options.threshold) && options.threshold >= 0) { + this.options.trimThreshold = options.threshold; + } else { + throw is.invalidParameterError('threshold', 'positive number', options.threshold); + } + } + if (is.defined(options.lineArt)) { + this._setBooleanOption('trimLineArt', options.lineArt); + } + } else { + throw is.invalidParameterError('trim', 'object', options); + } } - if (this.options.trimThreshold && isRotationExpected(this.options)) { - this.options.rotateBeforePreExtract = true; + if (isRotationExpected(this.options)) { + this.options.rotateBefore = true; } return this; } /** * Decorate the Sharp prototype with resize-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Object.assign(Sharp.prototype, { resize, extend, diff --git a/lib/sharp.js b/lib/sharp.js new file mode 100644 index 000000000..1081c9314 --- /dev/null +++ b/lib/sharp.js @@ -0,0 +1,121 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +// Inspects the runtime environment and exports the relevant sharp.node binary + +const { familySync, versionSync } = require('detect-libc'); + +const { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips'); +const runtimePlatform = runtimePlatformArch(); + +const paths = [ + `../src/build/Release/sharp-${runtimePlatform}.node`, + '../src/build/Release/sharp-wasm32.node', + `@img/sharp-${runtimePlatform}/sharp.node`, + '@img/sharp-wasm32/sharp.node' +]; + +/* node:coverage disable */ + +let path, sharp; +const errors = []; +for (path of paths) { + try { + sharp = require(path); + break; + } catch (err) { + errors.push(err); + } +} + +if (sharp && path.startsWith('@img/sharp-linux-x64') && !sharp._isUsingX64V2()) { + const err = new Error('Prebuilt binaries for linux-x64 require v2 microarchitecture'); + err.code = 'Unsupported CPU'; + errors.push(err); + sharp = null; +} + +if (sharp) { + module.exports = sharp; +} else { + const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os)); + + const help = [`Could not load the "sharp" module using the ${runtimePlatform} runtime`]; + errors.forEach(err => { + if (err.code !== 'MODULE_NOT_FOUND') { + help.push(`${err.code}: ${err.message}`); + } + }); + const messages = errors.map(err => err.message).join(' '); + help.push('Possible solutions:'); + // Common error messages + if (isUnsupportedNodeRuntime()) { + const { found, expected } = isUnsupportedNodeRuntime(); + help.push( + '- Please upgrade Node.js:', + ` Found ${found}`, + ` Requires ${expected}` + ); + } else if (prebuiltPlatforms.includes(runtimePlatform)) { + const [os, cpu] = runtimePlatform.split('-'); + const libc = os.endsWith('musl') ? ' --libc=musl' : ''; + help.push( + '- Ensure optional dependencies can be installed:', + ' npm install --include=optional sharp', + '- Ensure your package manager supports multi-platform installation:', + ' See https://sharp.pixelplumbing.com/install#cross-platform', + '- Add platform-specific dependencies:', + ` npm install --os=${os.replace('musl', '')}${libc} --cpu=${cpu} sharp` + ); + } else { + help.push( + `- Manually install libvips >= ${minimumLibvipsVersion}`, + '- Add experimental WebAssembly-based dependencies:', + ' npm install --cpu=wasm32 sharp', + ' npm install @img/sharp-wasm32' + ); + } + if (isLinux && /(symbol not found|CXXABI_)/i.test(messages)) { + try { + const { config } = require(`@img/sharp-libvips-${runtimePlatform}/package`); + const libcFound = `${familySync()} ${versionSync()}`; + const libcRequires = `${config.musl ? 'musl' : 'glibc'} ${config.musl || config.glibc}`; + help.push( + '- Update your OS:', + ` Found ${libcFound}`, + ` Requires ${libcRequires}` + ); + } catch (_errEngines) {} + } + if (isLinux && /\/snap\/core[0-9]{2}/.test(messages)) { + help.push( + '- Remove the Node.js Snap, which does not support native modules', + ' snap remove node' + ); + } + if (isMacOs && /Incompatible library version/.test(messages)) { + help.push( + '- Update Homebrew:', + ' brew update && brew upgrade vips' + ); + } + if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) { + help.push('- Run Node.js without using the --no-addons flag'); + } + // Link to installation docs + if (isWindows && /The specified procedure could not be found/.test(messages)) { + help.push( + '- Using the canvas package on Windows?', + ' See https://sharp.pixelplumbing.com/install#canvas-and-windows', + '- Check for outdated versions of sharp in the dependency tree:', + ' npm ls sharp' + ); + } + help.push( + '- Consult the installation documentation:', + ' See https://sharp.pixelplumbing.com/install' + ); + throw new Error(help.join('\n')); +} diff --git a/lib/utility.js b/lib/utility.js index 1f14fab51..c0ad39f86 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -1,10 +1,17 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const events = require('events'); +const events = require('node:events'); const detectLibc = require('detect-libc'); const is = require('./is'); -const sharp = require('../build/Release/sharp.node'); +const { runtimePlatformArch } = require('./libvips'); +const sharp = require('./sharp'); + +const runtimePlatform = runtimePlatformArch(); +const libvipsVersion = sharp.libvipsVersion(); /** * An Object containing nested boolean values representing the available input and output formats/methods. @@ -14,6 +21,10 @@ const sharp = require('../build/Release/sharp.node'); * @returns {Object} */ const format = sharp.format(); +format.heif.output.alias = ['avif', 'heic']; +format.jpeg.output.alias = ['jpe', 'jpg']; +format.tiff.output.alias = ['tif']; +format.jp2k.output.alias = ['j2c', 'j2k', 'jp2', 'jpx']; /** * An Object containing the available interpolators and their proper values @@ -27,26 +38,49 @@ const interpolators = { 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. */ + /** [LBB interpolation](https://github.com/libvips/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. */ + /** [VSQBS interpolation](https://github.com/libvips/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. + * An Object containing the version numbers of sharp, libvips + * and (when using prebuilt binaries) its dependencies. + * * @member * @example * console.log(sharp.versions); */ let versions = { - vips: sharp.libvipsVersion() + vips: libvipsVersion.semver }; -try { - versions = require(`../vendor/${versions.vips}/versions.json`); -} catch (err) {} +/* node:coverage ignore next 15 */ +if (!libvipsVersion.isGlobal) { + if (!libvipsVersion.isWasm) { + try { + versions = require(`@img/sharp-${runtimePlatform}/versions`); + } catch (_) { + try { + versions = require(`@img/sharp-libvips-${runtimePlatform}/versions`); + } catch (_) {} + } + } else { + try { + versions = require('@img/sharp-wasm32/versions'); + } catch (_) {} + } +} +versions.sharp = require('../package.json').version; + +/* node:coverage ignore next 5 */ +if (versions.heif && format.heif) { + // Prebuilt binaries provide AV1 + format.heif.input.fileSuffix = ['.avif']; + format.heif.output.alias = ['avif']; +} /** * Gets or, when options are provided, sets the limits of _libvips'_ operation cache. @@ -85,7 +119,11 @@ cache(true); /** * Gets or, when a concurrency is provided, sets - * the number of threads _libvips'_ should create to process each image. + * the maximum number of threads _libvips_ should use to process _each image_. + * These are from a thread pool managed by glib, + * which helps avoid the overhead of creating new threads. + * + * This method always returns the current concurrency. * * The default value is the number of CPU cores, * except when using glibc-based Linux without jemalloc, @@ -93,10 +131,13 @@ cache(true); * * A value of `0` will reset this to the number of CPU cores. * - * The maximum number of images that can be processed in parallel - * is limited by libuv's `UV_THREADPOOL_SIZE` environment variable. + * Some image format libraries spawn additional threads, + * e.g. libaom manages its own 4 threads when encoding AVIF images, + * and these are independent of the value set here. * - * This method always returns the current concurrency. + * :::note + * Further {@link /performance/ control over performance} is available. + * ::: * * @example * const threads = sharp.concurrency(); // 4 @@ -109,10 +150,13 @@ cache(true); function concurrency (concurrency) { return sharp.concurrency(is.integer(concurrency) ? concurrency : null); } -/* istanbul ignore next */ -if (detectLibc.family === detectLibc.GLIBC && !sharp._isUsingJemalloc()) { +/* node:coverage ignore next 7 */ +if (detectLibc.familySync() === detectLibc.GLIBC && !sharp._isUsingJemalloc()) { // Reduce default concurrency to 1 when using glibc memory allocator sharp.concurrency(1); +} else if (detectLibc.familySync() === detectLibc.MUSL && sharp.concurrency() === 1024) { + // Reduce default concurrency when musl thread over-subscription detected + sharp.concurrency(require('node:os').availableParallelism()); } /** @@ -143,17 +187,17 @@ function counters () { /** * Get and set use of SIMD vector unit instructions. - * Requires libvips to have been compiled with liborc support. + * Requires libvips to have been compiled with highway support. * * Improves the performance of `resize`, `blur` and `sharpen` operations * by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM NEON. * * @example * const simd = sharp.simd(); - * // simd is `true` if the runtime use of liborc is currently enabled + * // simd is `true` if the runtime use of highway is currently enabled * @example * const simd = sharp.simd(false); - * // prevent libvips from using liborc at runtime + * // prevent libvips from using highway at runtime * * @param {boolean} [simd=true] * @returns {boolean} @@ -161,13 +205,79 @@ function counters () { function simd (simd) { return sharp.simd(is.bool(simd) ? simd : null); } -simd(true); + +/** + * Block libvips operations at runtime. + * + * This is in addition to the `VIPS_BLOCK_UNTRUSTED` environment variable, + * which when set will block all "untrusted" operations. + * + * @since 0.32.4 + * + * @example <caption>Block all TIFF input.</caption> + * sharp.block({ + * operation: ['VipsForeignLoadTiff'] + * }); + * + * @param {Object} options + * @param {Array<string>} options.operation - List of libvips low-level operation names to block. + */ +function block (options) { + if (is.object(options)) { + if (Array.isArray(options.operation) && options.operation.every(is.string)) { + sharp.block(options.operation, true); + } else { + throw is.invalidParameterError('operation', 'Array<string>', options.operation); + } + } else { + throw is.invalidParameterError('options', 'object', options); + } +} + +/** + * Unblock libvips operations at runtime. + * + * This is useful for defining a list of allowed operations. + * + * @since 0.32.4 + * + * @example <caption>Block all input except WebP from the filesystem.</caption> + * sharp.block({ + * operation: ['VipsForeignLoad'] + * }); + * sharp.unblock({ + * operation: ['VipsForeignLoadWebpFile'] + * }); + * + * @example <caption>Block all input except JPEG and PNG from a Buffer or Stream.</caption> + * sharp.block({ + * operation: ['VipsForeignLoad'] + * }); + * sharp.unblock({ + * operation: ['VipsForeignLoadJpegBuffer', 'VipsForeignLoadPngBuffer'] + * }); + * + * @param {Object} options + * @param {Array<string>} options.operation - List of libvips low-level operation names to unblock. + */ +function unblock (options) { + if (is.object(options)) { + if (Array.isArray(options.operation) && options.operation.every(is.string)) { + sharp.block(options.operation, false); + } else { + throw is.invalidParameterError('operation', 'Array<string>', options.operation); + } + } else { + throw is.invalidParameterError('options', 'object', options); + } +} /** * Decorate the Sharp class with utility-related functions. + * @module Sharp * @private */ -module.exports = function (Sharp) { +module.exports = (Sharp) => { Sharp.cache = cache; Sharp.concurrency = concurrency; Sharp.counters = counters; @@ -176,4 +286,6 @@ module.exports = function (Sharp) { Sharp.interpolators = interpolators; Sharp.versions = versions; Sharp.queue = queue; + Sharp.block = block; + Sharp.unblock = unblock; }; diff --git a/npm/darwin-arm64/package.json b/npm/darwin-arm64/package.json new file mode 100644 index 000000000..df6b34a92 --- /dev/null +++ b/npm/darwin-arm64/package.json @@ -0,0 +1,40 @@ +{ + "name": "@img/sharp-darwin-arm64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with macOS 64-bit ARM", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/darwin-arm64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-darwin-arm64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "os": [ + "darwin" + ], + "cpu": [ + "arm64" + ] +} diff --git a/npm/darwin-x64/package.json b/npm/darwin-x64/package.json new file mode 100644 index 000000000..147d109dd --- /dev/null +++ b/npm/darwin-x64/package.json @@ -0,0 +1,40 @@ +{ + "name": "@img/sharp-darwin-x64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with macOS x64", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/darwin-x64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-darwin-x64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "os": [ + "darwin" + ], + "cpu": [ + "x64" + ] +} diff --git a/npm/from-local-build.js b/npm/from-local-build.js new file mode 100644 index 000000000..fc35bd2ed --- /dev/null +++ b/npm/from-local-build.js @@ -0,0 +1,64 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +// Populate the npm package for the current platform with the local build + +const { copyFileSync, cpSync, readFileSync, writeFileSync, appendFileSync } = require('node:fs'); +const { basename, join } = require('node:path'); + +const { buildPlatformArch } = require('../lib/libvips'); + +const licensing = ` +## Licensing + +Copyright 2013 Lovell Fuller and others. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +[https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +`; + +const platform = buildPlatformArch(); +const destDir = join(__dirname, platform); +console.log(`Populating npm package for platform: ${platform}`); + +// Copy binaries +const releaseDir = join(__dirname, '..', 'src', 'build', 'Release'); +const libDir = join(destDir, 'lib'); +cpSync(releaseDir, libDir, { + recursive: true, + filter: (file) => { + const name = basename(file); + return name === 'Release' || + (name.startsWith('sharp-') && name.includes('.node')) || + (name.startsWith('libvips-') && name.endsWith('.dll')); + } +}); + +// Generate README +const { name, description } = require(`./${platform}/package.json`); +writeFileSync(join(destDir, 'README.md'), `# \`${name}\`\n\n${description}.\n${licensing}`); + +// Copy Apache-2.0 LICENSE +copyFileSync(join(__dirname, '..', 'LICENSE'), join(destDir, 'LICENSE')); + +// Copy files for packages without an explicit sharp-libvips dependency (Windows, wasm) +if (platform.startsWith('win') || platform.startsWith('wasm')) { + const libvipsPlatform = platform === 'wasm32' ? 'dev-wasm32' : platform; + const sharpLibvipsDir = join(require(`@img/sharp-libvips-${libvipsPlatform}/lib`), '..'); + // Copy versions.json + copyFileSync(join(sharpLibvipsDir, 'versions.json'), join(destDir, 'versions.json')); + // Append third party licensing to README + const readme = readFileSync(join(sharpLibvipsDir, 'README.md'), { encoding: 'utf-8' }); + const thirdParty = readme.substring(readme.indexOf('\nThis software contains')); + appendFileSync(join(destDir, 'README.md'), thirdParty); +} diff --git a/npm/linux-arm/package.json b/npm/linux-arm/package.json new file mode 100644 index 000000000..cd4d5a2b6 --- /dev/null +++ b/npm/linux-arm/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linux-arm", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (glibc) ARM (32-bit)", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linux-arm" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linux-arm.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "glibc": ">=2.31" + }, + "os": [ + "linux" + ], + "libc": [ + "glibc" + ], + "cpu": [ + "arm" + ] +} diff --git a/npm/linux-arm64/package.json b/npm/linux-arm64/package.json new file mode 100644 index 000000000..b373bb920 --- /dev/null +++ b/npm/linux-arm64/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linux-arm64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (glibc) 64-bit ARM", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linux-arm64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linux-arm64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "glibc": ">=2.26" + }, + "os": [ + "linux" + ], + "libc": [ + "glibc" + ], + "cpu": [ + "arm64" + ] +} diff --git a/npm/linux-ppc64/package.json b/npm/linux-ppc64/package.json new file mode 100644 index 000000000..db2b62c01 --- /dev/null +++ b/npm/linux-ppc64/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linux-ppc64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (glibc) ppc64", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linux-ppc64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linux-ppc64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "glibc": ">=2.36" + }, + "os": [ + "linux" + ], + "libc": [ + "glibc" + ], + "cpu": [ + "ppc64" + ] +} diff --git a/npm/linux-riscv64/package.json b/npm/linux-riscv64/package.json new file mode 100644 index 000000000..9f0e95204 --- /dev/null +++ b/npm/linux-riscv64/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linux-riscv64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (glibc) RISC-V 64-bit", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linux-riscv64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linux-riscv64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "glibc": ">=2.41" + }, + "os": [ + "linux" + ], + "libc": [ + "glibc" + ], + "cpu": [ + "riscv64" + ] +} diff --git a/npm/linux-s390x/package.json b/npm/linux-s390x/package.json new file mode 100644 index 000000000..423692edd --- /dev/null +++ b/npm/linux-s390x/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linux-s390x", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (glibc) s390x", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linux-s390x" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linux-s390x.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "glibc": ">=2.36" + }, + "os": [ + "linux" + ], + "libc": [ + "glibc" + ], + "cpu": [ + "s390x" + ] +} diff --git a/npm/linux-x64/package.json b/npm/linux-x64/package.json new file mode 100644 index 000000000..95a8a035f --- /dev/null +++ b/npm/linux-x64/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linux-x64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (glibc) x64", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linux-x64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linux-x64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "glibc": ">=2.26" + }, + "os": [ + "linux" + ], + "libc": [ + "glibc" + ], + "cpu": [ + "x64" + ] +} diff --git a/npm/linuxmusl-arm64/package.json b/npm/linuxmusl-arm64/package.json new file mode 100644 index 000000000..5e214bf18 --- /dev/null +++ b/npm/linuxmusl-arm64/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linuxmusl-arm64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (musl) 64-bit ARM", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linuxmusl-arm64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linuxmusl-arm64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "musl": ">=1.2.2" + }, + "os": [ + "linux" + ], + "libc": [ + "musl" + ], + "cpu": [ + "arm64" + ] +} diff --git a/npm/linuxmusl-x64/package.json b/npm/linuxmusl-x64/package.json new file mode 100644 index 000000000..8be92db0d --- /dev/null +++ b/npm/linuxmusl-x64/package.json @@ -0,0 +1,46 @@ +{ + "name": "@img/sharp-linuxmusl-x64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Linux (musl) x64", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/linuxmusl-x64" + }, + "license": "Apache-2.0", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.2.4" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-linuxmusl-x64.node", + "./package": "./package.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "musl": ">=1.2.2" + }, + "os": [ + "linux" + ], + "libc": [ + "musl" + ], + "cpu": [ + "x64" + ] +} diff --git a/npm/package.json b/npm/package.json new file mode 100644 index 000000000..773daa94a --- /dev/null +++ b/npm/package.json @@ -0,0 +1,21 @@ +{ + "name": "@img/sharp", + "version": "0.34.5", + "private": "true", + "workspaces": [ + "darwin-arm64", + "darwin-x64", + "linux-arm", + "linux-arm64", + "linux-ppc64", + "linux-riscv64", + "linux-s390x", + "linux-x64", + "linuxmusl-arm64", + "linuxmusl-x64", + "wasm32", + "win32-arm64", + "win32-ia32", + "win32-x64" + ] +} diff --git a/npm/release-notes.js b/npm/release-notes.js new file mode 100644 index 000000000..13cc18772 --- /dev/null +++ b/npm/release-notes.js @@ -0,0 +1,9 @@ +const { readFileSync, writeFileSync } = require('node:fs'); + +const { version } = require('./package.json'); +const versionWithoutPreRelease = version.replace(/-rc\.\d+$/, ''); + +const markdown = readFileSync(`./docs/src/content/docs/changelog/v${versionWithoutPreRelease}.md`, 'utf8'); +const markdownWithoutFrontmatter = markdown.replace(/---\n.*?\n---\n+/s, ''); + +writeFileSync('./release-notes.md', markdownWithoutFrontmatter); diff --git a/npm/wasm32/package.json b/npm/wasm32/package.json new file mode 100644 index 000000000..d5775bec3 --- /dev/null +++ b/npm/wasm32/package.json @@ -0,0 +1,39 @@ +{ + "name": "@img/sharp-wasm32", + "version": "0.34.5", + "description": "Prebuilt sharp for use with wasm32", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/wasm32" + }, + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "files": [ + "lib", + "versions.json" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-wasm32.node.js", + "./package": "./package.json", + "./versions": "./versions.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "dependencies": { + "@emnapi/runtime": "^1.7.0" + }, + "cpu": [ + "wasm32" + ] +} diff --git a/npm/win32-arm64/package.json b/npm/win32-arm64/package.json new file mode 100644 index 000000000..651f420ea --- /dev/null +++ b/npm/win32-arm64/package.json @@ -0,0 +1,39 @@ +{ + "name": "@img/sharp-win32-arm64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Windows 64-bit ARM", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/win32-arm64" + }, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "files": [ + "lib", + "versions.json" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-win32-arm64.node", + "./package": "./package.json", + "./versions": "./versions.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "os": [ + "win32" + ], + "cpu": [ + "arm64" + ] +} diff --git a/npm/win32-ia32/package.json b/npm/win32-ia32/package.json new file mode 100644 index 000000000..21c6dbba2 --- /dev/null +++ b/npm/win32-ia32/package.json @@ -0,0 +1,39 @@ +{ + "name": "@img/sharp-win32-ia32", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Windows x86 (32-bit)", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/win32-ia32" + }, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "files": [ + "lib", + "versions.json" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-win32-ia32.node", + "./package": "./package.json", + "./versions": "./versions.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "os": [ + "win32" + ], + "cpu": [ + "ia32" + ] +} diff --git a/npm/win32-x64/package.json b/npm/win32-x64/package.json new file mode 100644 index 000000000..8b867b5e2 --- /dev/null +++ b/npm/win32-x64/package.json @@ -0,0 +1,39 @@ +{ + "name": "@img/sharp-win32-x64", + "version": "0.34.5", + "description": "Prebuilt sharp for use with Windows x64", + "author": "Lovell Fuller <npm@lovell.info>", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/win32-x64" + }, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "files": [ + "lib", + "versions.json" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-win32-x64.node", + "./package": "./package.json", + "./versions": "./versions.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "os": [ + "win32" + ], + "cpu": [ + "x64" + ] +} diff --git a/package.json b/package.json index 10e973b06..0c0d00988 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "sharp", - "description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images", - "version": "0.28.0", + "description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, GIF, AVIF and TIFF images", + "version": "0.34.5", "author": "Lovell Fuller <npm@lovell.info>", - "homepage": "https://github.com/lovell/sharp", + "homepage": "https://sharp.pixelplumbing.com", "contributors": [ "Pierre Inglebert <pierre.inglebert@gmail.com>", "Jonathan Ong <jonathanrichardong@gmail.com>", @@ -74,30 +74,51 @@ "Christian Flintrup <chr@gigahost.dk>", "Manan Jadhav <manan@motionden.com>", "Leon Radley <leon@radley.se>", - "alza54 <alza54@thiocod.in>" + "alza54 <alza54@thiocod.in>", + "Jacob Smith <jacob@frende.me>", + "Michael Nutt <michael@nutt.im>", + "Brad Parham <baparham@gmail.com>", + "Taneli Vatanen <taneli.vatanen@gmail.com>", + "Joris Dugué <zaruike10@gmail.com>", + "Chris Banks <christopher.bradley.banks@gmail.com>", + "Ompal Singh <ompal.hitm09@gmail.com>", + "Brodan <christopher.hranj@gmail.com>", + "Ankur Parihar <ankur.github@gmail.com>", + "Brahim Ait elhaj <brahima@gmail.com>", + "Mart Jansink <m.jansink@gmail.com>", + "Lachlan Newman <lachnewman007@gmail.com>", + "Dennis Beatty <dennis@dcbeatty.com>", + "Ingvar Stepanyan <me@rreverser.com>", + "Don Denton <don@happycollision.com>" ], "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", - "test-unit": "nyc --reporter=lcov --branches=99 mocha --slow=1000 --timeout=60000 ./test/unit/*.js", - "test-licensing": "license-checker --production --summary --onlyAllow=\"Apache-2.0;BSD;ISC;MIT\"", - "test-coverage": "./test/coverage/report.sh", + "build": "node install/build.js", + "install": "node install/check.js || npm run build", + "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*", + "test": "npm run lint && npm run test-unit", + "lint": "npm run lint-cpp && npm run lint-js && npm run lint-types", + "lint-cpp": "cpplint --quiet src/*.h src/*.cc", + "lint-js": "biome lint", + "lint-types": "tsd --files ./test/types/sharp.test-d.ts", "test-leak": "./test/leak/leak.sh", - "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" + "test-unit": "node --experimental-test-coverage test/unit.mjs", + "package-from-local-build": "node npm/from-local-build.js", + "package-release-notes": "node npm/release-notes.js", + "docs-build": "node docs/build.mjs", + "docs-serve": "cd docs && npm start", + "docs-publish": "cd docs && npm run build && npx firebase-tools deploy --project pixelplumbing --only hosting:pixelplumbing-sharp" }, + "type": "commonjs", "main": "lib/index.js", + "types": "lib/index.d.ts", "files": [ - "binding.gyp", - "install/**", - "lib/**", - "src/**" + "install", + "lib", + "src/*.{cc,h,gyp}" ], "repository": { "type": "git", - "url": "git://github.com/lovell/sharp" + "url": "git://github.com/lovell/sharp.git" }, "keywords": [ "jpeg", @@ -107,6 +128,7 @@ "tiff", "gif", "svg", + "jp2", "dzi", "image", "resize", @@ -117,56 +139,64 @@ "vips" ], "dependencies": { - "color": "^3.1.3", - "detect-libc": "^1.0.3", - "node-addon-api": "^3.1.0", - "prebuild-install": "^6.0.1", - "semver": "^7.3.5", - "simple-get": "^3.1.0", - "tar-fs": "^2.1.1", - "tunnel-agent": "^0.6.0" + "@img/colour": "^1.0.0", + "detect-libc": "^2.1.2", + "semver": "^7.7.3" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.34.5", + "@img/sharp-darwin-x64": "0.34.5", + "@img/sharp-libvips-darwin-arm64": "1.2.4", + "@img/sharp-libvips-darwin-x64": "1.2.4", + "@img/sharp-libvips-linux-arm": "1.2.4", + "@img/sharp-libvips-linux-arm64": "1.2.4", + "@img/sharp-libvips-linux-ppc64": "1.2.4", + "@img/sharp-libvips-linux-riscv64": "1.2.4", + "@img/sharp-libvips-linux-s390x": "1.2.4", + "@img/sharp-libvips-linux-x64": "1.2.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", + "@img/sharp-libvips-linuxmusl-x64": "1.2.4", + "@img/sharp-linux-arm": "0.34.5", + "@img/sharp-linux-arm64": "0.34.5", + "@img/sharp-linux-ppc64": "0.34.5", + "@img/sharp-linux-riscv64": "0.34.5", + "@img/sharp-linux-s390x": "0.34.5", + "@img/sharp-linux-x64": "0.34.5", + "@img/sharp-linuxmusl-arm64": "0.34.5", + "@img/sharp-linuxmusl-x64": "0.34.5", + "@img/sharp-wasm32": "0.34.5", + "@img/sharp-win32-arm64": "0.34.5", + "@img/sharp-win32-ia32": "0.34.5", + "@img/sharp-win32-x64": "0.34.5" }, "devDependencies": { - "async": "^3.2.0", - "cc": "^3.0.1", - "decompress-zip": "^0.3.3", - "documentation": "^13.2.0", - "exif-reader": "^1.0.3", - "icc": "^2.0.0", - "license-checker": "^25.0.1", - "mocha": "^8.3.2", - "mock-fs": "^4.13.0", - "nyc": "^15.1.0", - "prebuild": "^10.0.1", - "rimraf": "^3.0.2", - "semistandard": "^16.0.0" + "@biomejs/biome": "^2.3.4", + "@cpplint/cli": "^0.1.0", + "@emnapi/runtime": "^1.7.0", + "@img/sharp-libvips-dev": "1.2.4", + "@img/sharp-libvips-dev-wasm32": "1.2.4", + "@img/sharp-libvips-win32-arm64": "1.2.4", + "@img/sharp-libvips-win32-ia32": "1.2.4", + "@img/sharp-libvips-win32-x64": "1.2.4", + "@types/node": "*", + "emnapi": "^1.7.0", + "exif-reader": "^2.0.2", + "extract-zip": "^2.0.1", + "icc": "^3.0.0", + "jsdoc-to-markdown": "^9.1.3", + "node-addon-api": "^8.5.0", + "node-gyp": "^11.5.0", + "tar-fs": "^3.1.1", + "tsd": "^0.33.0" }, "license": "Apache-2.0", - "config": { - "libvips": "8.10.6", - "runtime": "napi", - "target": 3 - }, "engines": { - "node": ">=10" + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "config": { + "libvips": ">=8.17.3" }, "funding": { "url": "https://opencollective.com/libvips" - }, - "binary": { - "napi_versions": [ - 3 - ] - }, - "semistandard": { - "env": [ - "mocha" - ] - }, - "cc": { - "linelength": "120", - "filter": [ - "build/include" - ] } } diff --git a/src/CPPLINT.cfg b/src/CPPLINT.cfg new file mode 100644 index 000000000..7a643b075 --- /dev/null +++ b/src/CPPLINT.cfg @@ -0,0 +1,10 @@ +set noparent + +linelength=120 + +filter=-build/include +filter=+build/include_alpha +filter=+build/include_subdir +filter=+build/include_what_you_use + +filter=-whitespace/indent_namespace diff --git a/src/binding.gyp b/src/binding.gyp new file mode 100644 index 000000000..2040cde5b --- /dev/null +++ b/src/binding.gyp @@ -0,0 +1,298 @@ +# Copyright 2013 Lovell Fuller and others. +# SPDX-License-Identifier: Apache-2.0 + +{ + 'variables': { + 'vips_version': '<!(node -p "require(\'../lib/libvips\').minimumLibvipsVersion")', + 'platform_and_arch': '<!(node -p "require(\'../lib/libvips\').buildPlatformArch()")', + 'sharp_libvips_version': '<!(node -p "require(\'../package.json\').optionalDependencies[\'@img/sharp-libvips-<(platform_and_arch)\']")', + 'sharp_libvips_yarn_locator': '<!(node -p "require(\'../lib/libvips\').yarnLocator()")', + 'sharp_libvips_include_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsIncludeDir()")', + 'sharp_libvips_cplusplus_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsCPlusPlusDir()")', + 'sharp_libvips_lib_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsLibDir()")' + }, + 'targets': [{ + 'target_name': 'libvips-cpp-<(vips_version)', + 'conditions': [ + ['OS == "win"', { + # Build libvips C++ binding for Windows due to MSVC std library ABI changes + 'type': 'shared_library', + 'defines': [ + '_VIPS_PUBLIC=__declspec(dllexport)', + '_ALLOW_KEYWORD_MACROS', + 'G_DISABLE_ASSERT', + 'G_DISABLE_CAST_CHECKS', + 'G_DISABLE_CHECKS' + ], + 'sources': [ + '<(sharp_libvips_cplusplus_dir)/VConnection.cpp', + '<(sharp_libvips_cplusplus_dir)/VError.cpp', + '<(sharp_libvips_cplusplus_dir)/VImage.cpp', + '<(sharp_libvips_cplusplus_dir)/VInterpolate.cpp', + '<(sharp_libvips_cplusplus_dir)/VRegion.cpp' + ], + 'include_dirs': [ + '<(sharp_libvips_include_dir)', + '<(sharp_libvips_include_dir)/glib-2.0', + '<(sharp_libvips_lib_dir)/glib-2.0/include' + ], + 'link_settings': { + 'library_dirs': [ + '<(sharp_libvips_lib_dir)' + ], + 'libraries': [ + 'libvips.lib' + ], + }, + 'configurations': { + 'Release': { + 'msvs_settings': { + 'VCCLCompilerTool': { + "AdditionalOptions": [ + "/std:c++17" + ], + 'ExceptionHandling': 1, + 'Optimization': 1, + 'WholeProgramOptimization': 'true' + }, + 'VCLibrarianTool': { + 'AdditionalOptions': [ + '/LTCG:INCREMENTAL' + ] + }, + 'VCLinkerTool': { + 'ImageHasSafeExceptionHandlers': 'false', + 'OptimizeReferences': 2, + 'EnableCOMDATFolding': 2, + 'LinkIncremental': 1, + 'AdditionalOptions': [ + '/LTCG:INCREMENTAL' + ] + } + }, + 'msvs_disabled_warnings': [ + 4275 + ] + } + } + }, { + # Ignore this target for non-Windows + 'type': 'none' + }] + ] + }, { + 'target_name': 'sharp-<(platform_and_arch)', + 'defines': [ + 'G_DISABLE_ASSERT', + 'G_DISABLE_CAST_CHECKS', + 'G_DISABLE_CHECKS', + 'NAPI_VERSION=9', + 'NODE_ADDON_API_DISABLE_DEPRECATED', + 'NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS' + ], + 'dependencies': [ + '<!(node -p "require(\'node-addon-api\').gyp")', + 'libvips-cpp-<(vips_version)' + ], + 'variables': { + 'conditions': [ + ['OS != "win"', { + 'pkg_config_path': '<!(node -p "require(\'../lib/libvips\').pkgConfigPath()")', + 'use_global_libvips': '<!(node -p "Boolean(require(\'../lib/libvips\').useGlobalLibvips()).toString()")' + }, { + 'pkg_config_path': '', + 'use_global_libvips': '' + }] + ] + }, + 'sources': [ + 'common.cc', + 'metadata.cc', + 'stats.cc', + 'operations.cc', + 'pipeline.cc', + 'utilities.cc', + 'sharp.cc' + ], + 'include_dirs': [ + '<!(node -p "require(\'node-addon-api\').include_dir")', + ], + 'conditions': [ + ['use_global_libvips == "true"', { + # Use pkg-config for include and lib + 'include_dirs': ['<!@(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --cflags-only-I vips-cpp vips glib-2.0 | sed s/-I//g)'], + 'libraries': ['<!@(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --libs vips-cpp)'], + 'defines': [ + 'SHARP_USE_GLOBAL_LIBVIPS' + ], + 'conditions': [ + ['OS == "linux"', { + 'defines': [ + # Inspect libvips-cpp.so to determine which C++11 ABI version was used and set _GLIBCXX_USE_CXX11_ABI accordingly. This is quite horrible. + '_GLIBCXX_USE_CXX11_ABI=<!(if readelf -Ws "$(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --variable libdir vips-cpp)/libvips-cpp.so" | c++filt | grep -qF __cxx11;then echo "1";else echo "0";fi)' + ] + }] + ] + }, { + # Use pre-built libvips stored locally within node_modules + 'include_dirs': [ + '<(sharp_libvips_include_dir)', + '<(sharp_libvips_include_dir)/glib-2.0', + '<(sharp_libvips_lib_dir)/glib-2.0/include' + ], + 'library_dirs': [ + '<(sharp_libvips_lib_dir)' + ], + 'conditions': [ + ['OS == "win"', { + 'defines': [ + '_ALLOW_KEYWORD_MACROS', + '_FILE_OFFSET_BITS=64' + ], + 'link_settings': { + 'libraries': [ + 'libvips.lib' + ] + } + }], + ['OS == "mac"', { + 'link_settings': { + 'libraries': [ + 'libvips-cpp.<(vips_version).dylib' + ] + }, + 'xcode_settings': { + 'OTHER_LDFLAGS': [ + '-Wl,-s', + '-Wl,-dead_strip', + # Ensure runtime linking is relative to sharp.node + '-Wl,-rpath,\'@loader_path/../../sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath,\'@loader_path/../../../sharp-libvips-<(platform_and_arch)/<(sharp_libvips_version)/lib\'', + '-Wl,-rpath,\'@loader_path/../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath,\'@loader_path/../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath,\'@loader_path/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath,\'@loader_path/../../../../../@img-sharp-libvips-<(platform_and_arch)-npm-<(sharp_libvips_version)-<(sharp_libvips_yarn_locator)/node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'' + ] + } + }], + ['OS == "linux"', { + 'defines': [ + '_GLIBCXX_USE_CXX11_ABI=1' + ], + 'cflags_cc': [ + '<!(node -p "require(\'detect-libc\').isNonGlibcLinuxSync() ? \'\' : \'-flto=auto\'")' + ], + 'link_settings': { + 'libraries': [ + '-l:libvips-cpp.so.<(vips_version)' + ], + 'ldflags': [ + '-lstdc++fs', + '-Wl,-s', + '-Wl,--disable-new-dtags', + '-Wl,-z,nodelete', + '-Wl,-Bsymbolic-functions', + '-Wl,-rpath=\'$$ORIGIN/../../sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath=\'$$ORIGIN/../../../sharp-libvips-<(platform_and_arch)/<(sharp_libvips_version)/lib\'', + '-Wl,-rpath=\'$$ORIGIN/../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath=\'$$ORIGIN/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'', + '-Wl,-rpath,\'$$ORIGIN/../../../../../@img-sharp-libvips-<(platform_and_arch)-npm-<(sharp_libvips_version)-<(sharp_libvips_yarn_locator)/node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'' + ] + } + }], + ['OS == "emscripten"', { + 'product_extension': 'node.js', + 'link_settings': { + 'ldflags': [ + '-fexceptions', + '--pre-js=<!(node -p "require.resolve(\'./emscripten/pre.js\')")', + '-Oz', + '-sALLOW_MEMORY_GROWTH', + '-sENVIRONMENT=node', + '-sEXPORTED_FUNCTIONS=emnapiInit,_vips_shutdown,_uv_library_shutdown', + '-sNODERAWFS', + '-sWASM_ASYNC_COMPILATION=0' + ], + 'libraries': [ + '<!@(PKG_CONFIG_PATH="<!(node -p "require(\'@img/sharp-libvips-dev-wasm32/lib\')")/pkgconfig" pkg-config --static --libs vips-cpp)' + ], + } + }] + ] + }] + ], + 'cflags_cc': [ + '-std=c++17', + '-fexceptions', + '-Wall', + '-Os' + ], + 'xcode_settings': { + 'CLANG_CXX_LANGUAGE_STANDARD': 'c++17', + 'MACOSX_DEPLOYMENT_TARGET': '10.15', + 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', + 'GCC_ENABLE_CPP_RTTI': 'YES', + 'OTHER_CPLUSPLUSFLAGS': [ + '-fexceptions', + '-Wall', + '-Oz' + ] + }, + 'configurations': { + 'Release': { + 'conditions': [ + ['target_arch == "arm"', { + 'cflags_cc': [ + '-Wno-psabi' + ] + }], + ['OS == "win"', { + 'msvs_settings': { + 'VCCLCompilerTool': { + "AdditionalOptions": [ + "/std:c++17" + ], + 'ExceptionHandling': 1, + 'Optimization': 1, + 'WholeProgramOptimization': 'true' + }, + 'VCLibrarianTool': { + 'AdditionalOptions': [ + '/LTCG:INCREMENTAL' + ] + }, + 'VCLinkerTool': { + 'ImageHasSafeExceptionHandlers': 'false', + 'OptimizeReferences': 2, + 'EnableCOMDATFolding': 2, + 'LinkIncremental': 1, + 'AdditionalOptions': [ + '/LTCG:INCREMENTAL' + ] + } + }, + 'msvs_disabled_warnings': [ + 4275 + ] + }] + ] + } + }, + }, { + 'target_name': 'copy-dll', + 'type': 'none', + 'dependencies': [ + 'sharp-<(platform_and_arch)' + ], + 'conditions': [ + ['OS == "win"', { + 'copies': [{ + 'destination': 'build/Release', + 'files': [ + '<(sharp_libvips_lib_dir)/libvips-42.dll' + ] + }] + }] + ] + }] +} diff --git a/src/common.cc b/src/common.cc index ce414bb2c..a27c90c7c 100644 --- a/src/common.cc +++ b/src/common.cc @@ -1,29 +1,22 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ +#include <algorithm> #include <cstdlib> +#include <map> +#include <mutex> +#include <queue> #include <string> -#include <string.h> +#include <tuple> +#include <utility> #include <vector> -#include <queue> -#include <map> -#include <mutex> // NOLINT(build/c++11) #include <napi.h> #include <vips/vips8> -#include "common.h" +#include "./common.h" using vips::VImage; @@ -36,6 +29,9 @@ namespace sharp { std::string AttrAsStr(Napi::Object obj, std::string attr) { return obj.Get(attr).As<Napi::String>(); } + std::string AttrAsStr(Napi::Object obj, unsigned int const attr) { + return obj.Get(attr).As<Napi::String>(); + } uint32_t AttrAsUint32(Napi::Object obj, std::string attr) { return obj.Get(attr).As<Napi::Number>().Uint32Value(); } @@ -45,6 +41,9 @@ namespace sharp { int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr) { return obj.Get(attr).As<Napi::Number>().Int32Value(); } + int64_t AttrAsInt64(Napi::Object obj, std::string attr) { + return obj.Get(attr).As<Napi::Number>().Int64Value(); + } double AttrAsDouble(Napi::Object obj, std::string attr) { return obj.Get(attr).As<Napi::Number>().DoubleValue(); } @@ -80,18 +79,25 @@ namespace sharp { Napi::Buffer<char> buffer = input.Get("buffer").As<Napi::Buffer<char>>(); descriptor->bufferLength = buffer.Length(); descriptor->buffer = buffer.Data(); - descriptor->isBuffer = TRUE; + descriptor->isBuffer = true; } - descriptor->failOnError = AttrAsBool(input, "failOnError"); + descriptor->failOn = AttrAsEnum<VipsFailOn>(input, "failOn", VIPS_TYPE_FAIL_ON); // Density for vector-based input if (HasAttr(input, "density")) { descriptor->density = AttrAsDouble(input, "density"); } + // Should we ignore any embedded ICC profile + if (HasAttr(input, "ignoreIcc")) { + descriptor->ignoreIcc = AttrAsBool(input, "ignoreIcc"); + } // Raw pixel input if (HasAttr(input, "rawChannels")) { + descriptor->rawDepth = AttrAsEnum<VipsBandFormat>(input, "rawDepth", VIPS_TYPE_BAND_FORMAT); descriptor->rawChannels = AttrAsUint32(input, "rawChannels"); descriptor->rawWidth = AttrAsUint32(input, "rawWidth"); descriptor->rawHeight = AttrAsUint32(input, "rawHeight"); + descriptor->rawPremultiplied = AttrAsBool(input, "rawPremultiplied"); + descriptor->rawPageHeight = AttrAsUint32(input, "rawPageHeight"); } // Multi-page input (GIF, TIFF, PDF) if (HasAttr(input, "pages")) { @@ -100,15 +106,35 @@ namespace sharp { if (HasAttr(input, "page")) { descriptor->page = AttrAsUint32(input, "page"); } + // SVG + if (HasAttr(input, "svgStylesheet")) { + descriptor->svgStylesheet = AttrAsStr(input, "svgStylesheet"); + } + if (HasAttr(input, "svgHighBitdepth")) { + descriptor->svgHighBitdepth = AttrAsBool(input, "svgHighBitdepth"); + } // Multi-level input (OpenSlide) - if (HasAttr(input, "level")) { - descriptor->level = AttrAsUint32(input, "level"); + if (HasAttr(input, "openSlideLevel")) { + descriptor->openSlideLevel = AttrAsUint32(input, "openSlideLevel"); + } + // subIFD (OME-TIFF) + if (HasAttr(input, "subifd")) { + descriptor->tiffSubifd = AttrAsInt32(input, "tiffSubifd"); + } + // // PDF background color + if (HasAttr(input, "pdfBackground")) { + descriptor->pdfBackground = AttrAsVectorOfDouble(input, "pdfBackground"); + } + // Use JPEG 2000 oneshot mode? + if (HasAttr(input, "jp2Oneshot")) { + descriptor->jp2Oneshot = AttrAsBool(input, "jp2Oneshot"); } // Create new image if (HasAttr(input, "createChannels")) { descriptor->createChannels = AttrAsUint32(input, "createChannels"); descriptor->createWidth = AttrAsUint32(input, "createWidth"); descriptor->createHeight = AttrAsUint32(input, "createHeight"); + descriptor->createPageHeight = AttrAsUint32(input, "createPageHeight"); if (HasAttr(input, "createNoiseType")) { descriptor->createNoiseType = AttrAsStr(input, "createNoiseType"); descriptor->createNoiseMean = AttrAsDouble(input, "createNoiseMean"); @@ -117,18 +143,76 @@ namespace sharp { descriptor->createBackground = AttrAsVectorOfDouble(input, "createBackground"); } } + // Create new image with text + if (HasAttr(input, "textValue")) { + descriptor->textValue = AttrAsStr(input, "textValue"); + if (HasAttr(input, "textFont")) { + descriptor->textFont = AttrAsStr(input, "textFont"); + } + if (HasAttr(input, "textFontfile")) { + descriptor->textFontfile = AttrAsStr(input, "textFontfile"); + } + if (HasAttr(input, "textWidth")) { + descriptor->textWidth = AttrAsUint32(input, "textWidth"); + } + if (HasAttr(input, "textHeight")) { + descriptor->textHeight = AttrAsUint32(input, "textHeight"); + } + if (HasAttr(input, "textAlign")) { + descriptor->textAlign = AttrAsEnum<VipsAlign>(input, "textAlign", VIPS_TYPE_ALIGN); + } + if (HasAttr(input, "textJustify")) { + descriptor->textJustify = AttrAsBool(input, "textJustify"); + } + if (HasAttr(input, "textDpi")) { + descriptor->textDpi = AttrAsUint32(input, "textDpi"); + } + if (HasAttr(input, "textRgba")) { + descriptor->textRgba = AttrAsBool(input, "textRgba"); + } + if (HasAttr(input, "textSpacing")) { + descriptor->textSpacing = AttrAsUint32(input, "textSpacing"); + } + if (HasAttr(input, "textWrap")) { + descriptor->textWrap = AttrAsEnum<VipsTextWrap>(input, "textWrap", VIPS_TYPE_TEXT_WRAP); + } + } + // Join images together + if (HasAttr(input, "joinAnimated")) { + descriptor->joinAnimated = AttrAsBool(input, "joinAnimated"); + } + if (HasAttr(input, "joinAcross")) { + descriptor->joinAcross = AttrAsUint32(input, "joinAcross"); + } + if (HasAttr(input, "joinShim")) { + descriptor->joinShim = AttrAsUint32(input, "joinShim"); + } + if (HasAttr(input, "joinBackground")) { + descriptor->joinBackground = AttrAsVectorOfDouble(input, "joinBackground"); + } + if (HasAttr(input, "joinHalign")) { + descriptor->joinHalign = AttrAsEnum<VipsAlign>(input, "joinHalign", VIPS_TYPE_ALIGN); + } + if (HasAttr(input, "joinValign")) { + descriptor->joinValign = AttrAsEnum<VipsAlign>(input, "joinValign", VIPS_TYPE_ALIGN); + } // Limit input images to a given number of pixels, where pixels = width * height - descriptor->limitInputPixels = AttrAsUint32(input, "limitInputPixels"); - // Allow switch from random to sequential access - descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM; + descriptor->limitInputPixels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputPixels")); + if (HasAttr(input, "access")) { + descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM; + } + // Remove safety features and allow unlimited input + descriptor->unlimited = AttrAsBool(input, "unlimited"); + // Use the EXIF orientation to auto orient the image + descriptor->autoOrient = AttrAsBool(input, "autoOrient"); return descriptor; } // How many tasks are in the queue? - volatile int counterQueue = 0; + std::atomic<int> counterQueue{0}; // How many tasks are being processed? - volatile int counterProcess = 0; + std::atomic<int> counterProcess{0}; // Filename extension checkers static bool EndsWith(std::string const &str, std::string const &end) { @@ -146,6 +230,10 @@ namespace sharp { bool IsGif(std::string const &str) { return EndsWith(str, ".gif") || EndsWith(str, ".GIF"); } + bool IsJp2(std::string const &str) { + return EndsWith(str, ".jp2") || EndsWith(str, ".jpx") || EndsWith(str, ".j2k") || EndsWith(str, ".j2c") + || EndsWith(str, ".JP2") || EndsWith(str, ".JPX") || EndsWith(str, ".J2K") || EndsWith(str, ".J2C"); + } bool IsTiff(std::string const &str) { return EndsWith(str, ".tif") || EndsWith(str, ".tiff") || EndsWith(str, ".TIF") || EndsWith(str, ".TIFF"); } @@ -158,6 +246,9 @@ namespace sharp { bool IsAvif(std::string const &str) { return EndsWith(str, ".avif") || EndsWith(str, ".AVIF"); } + bool IsJxl(std::string const &str) { + return EndsWith(str, ".jxl") || EndsWith(str, ".JXL"); + } bool IsDz(std::string const &str) { return EndsWith(str, ".dzi") || EndsWith(str, ".DZI"); } @@ -168,6 +259,13 @@ namespace sharp { return EndsWith(str, ".v") || EndsWith(str, ".V") || EndsWith(str, ".vips") || EndsWith(str, ".VIPS"); } + /* + Trim space from end of string. + */ + std::string TrimEnd(std::string const &str) { + return str.substr(0, str.find_last_not_of(" \n\r\f") + 1); + } + /* Provide a string identifier for the given image type. */ @@ -179,6 +277,7 @@ namespace sharp { case ImageType::WEBP: id = "webp"; break; case ImageType::TIFF: id = "tiff"; break; case ImageType::GIF: id = "gif"; break; + case ImageType::JP2: id = "jp2"; break; case ImageType::SVG: id = "svg"; break; case ImageType::HEIF: id = "heif"; break; case ImageType::PDF: id = "pdf"; break; @@ -187,6 +286,9 @@ namespace sharp { case ImageType::PPM: id = "ppm"; break; case ImageType::FITS: id = "fits"; break; case ImageType::EXR: id = "exr"; break; + case ImageType::JXL: id = "jxl"; break; + case ImageType::RAD: id = "rad"; break; + case ImageType::DCRAW: id = "dcraw"; break; case ImageType::VIPS: id = "vips"; break; case ImageType::RAW: id = "raw"; break; case ImageType::UNKNOWN: id = "unknown"; break; @@ -215,6 +317,8 @@ namespace sharp { { "VipsForeignLoadGifBuffer", ImageType::GIF }, { "VipsForeignLoadNsgifFile", ImageType::GIF }, { "VipsForeignLoadNsgifBuffer", ImageType::GIF }, + { "VipsForeignLoadJp2kBuffer", ImageType::JP2 }, + { "VipsForeignLoadJp2kFile", ImageType::JP2 }, { "VipsForeignLoadSvgFile", ImageType::SVG }, { "VipsForeignLoadSvgBuffer", ImageType::SVG }, { "VipsForeignLoadHeifFile", ImageType::HEIF }, @@ -223,11 +327,20 @@ namespace sharp { { "VipsForeignLoadPdfBuffer", ImageType::PDF }, { "VipsForeignLoadMagickFile", ImageType::MAGICK }, { "VipsForeignLoadMagickBuffer", ImageType::MAGICK }, - { "VipsForeignLoadOpenslide", ImageType::OPENSLIDE }, + { "VipsForeignLoadMagick7File", ImageType::MAGICK }, + { "VipsForeignLoadMagick7Buffer", ImageType::MAGICK }, + { "VipsForeignLoadOpenslideFile", ImageType::OPENSLIDE }, { "VipsForeignLoadPpmFile", ImageType::PPM }, - { "VipsForeignLoadFits", ImageType::FITS }, + { "VipsForeignLoadFitsFile", ImageType::FITS }, { "VipsForeignLoadOpenexr", ImageType::EXR }, + { "VipsForeignLoadJxlFile", ImageType::JXL }, + { "VipsForeignLoadJxlBuffer", ImageType::JXL }, + { "VipsForeignLoadRadFile", ImageType::RAD }, + { "VipsForeignLoadRadBuffer", ImageType::RAD }, + { "VipsForeignLoadDcRawFile", ImageType::DCRAW }, + { "VipsForeignLoadDcRawBuffer", ImageType::DCRAW }, { "VipsForeignLoadVips", ImageType::VIPS }, + { "VipsForeignLoadVipsFile", ImageType::VIPS }, { "VipsForeignLoadRaw", ImageType::RAW } }; @@ -273,11 +386,66 @@ namespace sharp { imageType == ImageType::WEBP || imageType == ImageType::MAGICK || imageType == ImageType::GIF || + imageType == ImageType::JP2 || imageType == ImageType::TIFF || imageType == ImageType::HEIF || imageType == ImageType::PDF; } + /* + Does this image type support removal of safety limits? + */ + bool ImageTypeSupportsUnlimited(ImageType imageType) { + return + imageType == ImageType::JPEG || + imageType == ImageType::PNG || + imageType == ImageType::SVG || + imageType == ImageType::TIFF || + imageType == ImageType::HEIF; + } + + /* + Format-specific options builder + */ + vips::VOption* GetOptionsForImageType(ImageType imageType, InputDescriptor *descriptor) { + vips::VOption *option = VImage::option() + ->set("access", descriptor->access) + ->set("fail_on", descriptor->failOn); + if (descriptor->unlimited && ImageTypeSupportsUnlimited(imageType)) { + option->set("unlimited", true); + } + if (ImageTypeSupportsPage(imageType)) { + option->set("n", descriptor->pages); + option->set("page", descriptor->page); + } + switch (imageType) { + case ImageType::SVG: + option->set("dpi", descriptor->density) + ->set("stylesheet", descriptor->svgStylesheet.data()) + ->set("high_bitdepth", descriptor->svgHighBitdepth); + break; + case ImageType::TIFF: + option->set("subifd", descriptor->tiffSubifd); + break; + case ImageType::PDF: + option->set("dpi", descriptor->density) + ->set("background", descriptor->pdfBackground); + break; + case ImageType::OPENSLIDE: + option->set("level", descriptor->openSlideLevel); + break; + case ImageType::JP2: + option->set("oneshot", descriptor->jp2Oneshot); + break; + case ImageType::MAGICK: + option->set("density", std::to_string(descriptor->density).data()); + break; + default: + break; + } + return option; + } + /* Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data) */ @@ -287,12 +455,20 @@ namespace sharp { if (descriptor->isBuffer) { if (descriptor->rawChannels > 0) { // Raw, uncompressed pixel data + bool const is8bit = vips_band_format_is8bit(descriptor->rawDepth); image = VImage::new_from_memory(descriptor->buffer, descriptor->bufferLength, - descriptor->rawWidth, descriptor->rawHeight, descriptor->rawChannels, VIPS_FORMAT_UCHAR); + descriptor->rawWidth, descriptor->rawHeight, descriptor->rawChannels, descriptor->rawDepth); if (descriptor->rawChannels < 3) { - image.get_image()->Type = VIPS_INTERPRETATION_B_W; + image.get_image()->Type = is8bit ? VIPS_INTERPRETATION_B_W : VIPS_INTERPRETATION_GREY16; } else { - image.get_image()->Type = VIPS_INTERPRETATION_sRGB; + image.get_image()->Type = is8bit ? VIPS_INTERPRETATION_sRGB : VIPS_INTERPRETATION_RGB16; + } + if (descriptor->rawPageHeight > 0) { + image.set(VIPS_META_PAGE_HEIGHT, descriptor->rawPageHeight); + image.set(VIPS_META_N_PAGES, static_cast<int>(descriptor->rawHeight / descriptor->rawPageHeight)); + } + if (descriptor->rawPremultiplied) { + image = image.unpremultiply(); } imageType = ImageType::RAW; } else { @@ -300,25 +476,7 @@ namespace sharp { imageType = DetermineImageType(descriptor->buffer, descriptor->bufferLength); if (imageType != ImageType::UNKNOWN) { try { - vips::VOption *option = VImage::option() - ->set("access", descriptor->access) - ->set("fail", descriptor->failOnError); - if (imageType == ImageType::SVG) { - option->set("unlimited", TRUE); - } - if (imageType == ImageType::SVG || imageType == ImageType::PDF) { - option->set("dpi", descriptor->density); - } - if (imageType == ImageType::MAGICK) { - option->set("density", std::to_string(descriptor->density).data()); - } - if (ImageTypeSupportsPage(imageType)) { - option->set("n", descriptor->pages); - option->set("page", descriptor->page); - } - if (imageType == ImageType::OPENSLIDE) { - option->set("level", descriptor->level); - } + vips::VOption *option = GetOptionsForImageType(imageType, descriptor); image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option); if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) { image = SetDensity(image, descriptor->density); @@ -331,66 +489,81 @@ namespace sharp { } } } else { - if (descriptor->createChannels > 0) { + int const channels = descriptor->createChannels; + if (channels > 0) { // Create new image 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); + bands.push_back(VImage::gaussnoise(descriptor->createWidth, descriptor->createHeight, VImage::option() + ->set("mean", descriptor->createNoiseMean) + ->set("sigma", descriptor->createNoiseSigma))); } + image = VImage::bandjoin(bands).copy(VImage::option()->set("interpretation", + channels < 3 ? VIPS_INTERPRETATION_B_W: VIPS_INTERPRETATION_sRGB)); } else { std::vector<double> background = { descriptor->createBackground[0], descriptor->createBackground[1], descriptor->createBackground[2] }; - if (descriptor->createChannels == 4) { + if (channels == 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) + .copy(VImage::option()->set("interpretation", + channels < 3 ? VIPS_INTERPRETATION_B_W : VIPS_INTERPRETATION_sRGB)) + .new_from_image(background); + } + if (descriptor->createPageHeight > 0) { + image.set(VIPS_META_PAGE_HEIGHT, descriptor->createPageHeight); + image.set(VIPS_META_N_PAGES, static_cast<int>(descriptor->createHeight / descriptor->createPageHeight)); + } + image = image.cast(VIPS_FORMAT_UCHAR); + imageType = ImageType::RAW; + } else if (descriptor->textValue.length() > 0) { + // Create a new image with text + vips::VOption *textOptions = VImage::option() + ->set("align", descriptor->textAlign) + ->set("justify", descriptor->textJustify) + ->set("rgba", descriptor->textRgba) + ->set("spacing", descriptor->textSpacing) + ->set("wrap", descriptor->textWrap) + ->set("autofit_dpi", &descriptor->textAutofitDpi); + if (descriptor->textWidth > 0) { + textOptions->set("width", descriptor->textWidth); + } + // Ignore dpi if height is set + if (descriptor->textWidth > 0 && descriptor->textHeight > 0) { + textOptions->set("height", descriptor->textHeight); + } else if (descriptor->textDpi > 0) { + textOptions->set("dpi", descriptor->textDpi); + } + if (descriptor->textFont.length() > 0) { + textOptions->set("font", const_cast<char*>(descriptor->textFont.data())); + } + if (descriptor->textFontfile.length() > 0) { + textOptions->set("fontfile", const_cast<char*>(descriptor->textFontfile.data())); + } + image = VImage::text(const_cast<char *>(descriptor->textValue.data()), textOptions); + if (!descriptor->textRgba) { + image = image.copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_B_W)); } - image.get_image()->Type = VIPS_INTERPRETATION_sRGB; imageType = ImageType::RAW; } else { // From filesystem imageType = DetermineImageType(descriptor->file.data()); if (imageType == ImageType::MISSING) { - throw vips::VError("Input file is missing"); + if (descriptor->file.find("<svg") != std::string::npos) { + throw vips::VError("Input file is missing, did you mean " + "sharp(Buffer.from('" + descriptor->file.substr(0, 8) + "...')?"); + } + throw vips::VError("Input file is missing: " + descriptor->file); } if (imageType != ImageType::UNKNOWN) { try { - vips::VOption *option = VImage::option() - ->set("access", descriptor->access) - ->set("fail", descriptor->failOnError); - if (imageType == ImageType::SVG) { - option->set("unlimited", TRUE); - } - if (imageType == ImageType::SVG || imageType == ImageType::PDF) { - option->set("dpi", descriptor->density); - } - if (imageType == ImageType::MAGICK) { - option->set("density", std::to_string(descriptor->density).data()); - } - if (ImageTypeSupportsPage(imageType)) { - option->set("n", descriptor->pages); - option->set("page", descriptor->page); - } - if (imageType == ImageType::OPENSLIDE) { - option->set("level", descriptor->level); - } + vips::VOption *option = GetOptionsForImageType(imageType, descriptor); image = VImage::new_from_file(descriptor->file.data(), option); if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) { image = SetDensity(image, descriptor->density); @@ -403,9 +576,10 @@ namespace sharp { } } } + // Limit input images to a given number of pixels, where pixels = width * height if (descriptor->limitInputPixels > 0 && - static_cast<uint64_t>(image.width() * image.height()) > static_cast<uint64_t>(descriptor->limitInputPixels)) { + static_cast<uint64_t>(image.width()) * image.height() > descriptor->limitInputPixels) { throw vips::VError("Input image exceeds pixel limit"); } return std::make_tuple(image, imageType); @@ -415,15 +589,54 @@ namespace sharp { Does this image have an embedded profile? */ bool HasProfile(VImage image) { - return (image.get_typeof(VIPS_META_ICC_NAME) != 0) ? TRUE : FALSE; + return image.get_typeof(VIPS_META_ICC_NAME) == VIPS_TYPE_BLOB; } /* - Does this image have an alpha channel? - Uses colour space interpretation with number of channels to guess this. + Get copy of embedded profile. */ - bool HasAlpha(VImage image) { - return image.has_alpha(); + std::pair<char*, size_t> GetProfile(VImage image) { + std::pair<char*, size_t> icc(nullptr, 0); + if (HasProfile(image)) { + size_t length; + const void *data = image.get_blob(VIPS_META_ICC_NAME, &length); + icc.first = static_cast<char*>(g_malloc(length)); + icc.second = length; + memcpy(icc.first, data, length); + } + return icc; + } + + /* + Set embedded profile. + */ + VImage SetProfile(VImage image, std::pair<char*, size_t> icc) { + if (icc.first != nullptr) { + image = image.copy(); + image.set(VIPS_META_ICC_NAME, reinterpret_cast<VipsCallbackFn>(vips_area_free_cb), icc.first, icc.second); + } + return image; + } + + static void* RemoveExifCallback(VipsImage *image, char const *field, GValue *value, void *data) { + std::vector<std::string> *fieldNames = static_cast<std::vector<std::string> *>(data); + std::string fieldName(field); + if (fieldName.substr(0, 8) == ("exif-ifd")) { + fieldNames->push_back(fieldName); + } + return nullptr; + } + + /* + Remove all EXIF-related image fields. + */ + VImage RemoveExif(VImage image) { + std::vector<std::string> fieldNames; + vips_image_map(image.get_image(), static_cast<VipsImageMapFn>(RemoveExifCallback), &fieldNames); + for (const auto& f : fieldNames) { + image.remove(f.data()); + } + return image; } /* @@ -452,38 +665,52 @@ namespace sharp { VImage RemoveExifOrientation(VImage image) { VImage copy = image.copy(); copy.remove(VIPS_META_ORIENTATION); + copy.remove("exif-ifd0-Orientation"); return copy; } /* Set animation properties if necessary. - Non-provided properties will be loaded from image. */ - VImage SetAnimationProperties(VImage image, int pageHeight, std::vector<int> delay, int loop) { - bool hasDelay = delay.size() != 1 || delay.front() != -1; - - if (pageHeight == 0 && image.get_typeof(VIPS_META_PAGE_HEIGHT) == G_TYPE_INT) { - pageHeight = image.get_int(VIPS_META_PAGE_HEIGHT); - } + VImage SetAnimationProperties(VImage image, int nPages, int pageHeight, std::vector<int> delay, int loop) { + bool hasDelay = !delay.empty(); + VImage copy = image.copy(); - if (!hasDelay && image.get_typeof("delay") == VIPS_TYPE_ARRAY_INT) { - delay = image.get_array_int("delay"); - hasDelay = true; + // Only set page-height if we have more than one page, or this could + // accidentally turn into an animated image later. + if (nPages > 1) copy.set(VIPS_META_PAGE_HEIGHT, pageHeight); + if (hasDelay) { + if (delay.size() == 1) { + // We have just one delay, repeat that value for all frames. + delay.insert(delay.end(), nPages - 1, delay[0]); + } + copy.set("delay", delay); } - - if (loop == -1 && image.get_typeof("loop") == G_TYPE_INT) { - loop = image.get_int("loop"); + if (nPages == 1 && !hasDelay && loop == -1) { + loop = 1; } + if (loop != -1) copy.set("loop", loop); - if (pageHeight == 0) return image; + return copy; + } - // It is necessary to create the copy as otherwise, pageHeight will be ignored! + /* + Remove animation properties from image. + */ + VImage RemoveAnimationProperties(VImage image) { VImage copy = image.copy(); + copy.remove(VIPS_META_PAGE_HEIGHT); + copy.remove("delay"); + copy.remove("loop"); + return copy; + } - copy.set(VIPS_META_PAGE_HEIGHT, pageHeight); - if (hasDelay) copy.set("delay", delay); - if (loop != -1) copy.set("loop", loop); - + /* + Remove GIF palette from image. + */ + VImage RemoveGifPalette(VImage image) { + VImage copy = image.copy(); + copy.remove("gif-palette"); return copy; } @@ -507,12 +734,19 @@ namespace sharp { VImage SetDensity(VImage image, const double density) { const double pixelsPerMm = density / 25.4; VImage copy = image.copy(); - copy.set("Xres", pixelsPerMm); - copy.set("Yres", pixelsPerMm); - copy.set(VIPS_META_RESOLUTION_UNIT, "in"); + copy.get_image()->Xres = pixelsPerMm; + copy.get_image()->Yres = pixelsPerMm; return copy; } + /* + Multi-page images can have a page height. Fetch it, and sanity check it. + If page-height is not set, it defaults to the image height + */ + int GetPageHeight(VImage image) { + return vips_image_get_page_height(image.get_image()); + } + /* Check the proposed format supports the current dimensions. */ @@ -532,6 +766,10 @@ namespace sharp { if (image.width() > 65535 || height > 65535) { throw vips::VError("Processed image is too large for the GIF format"); } + } else if (imageType == ImageType::HEIF) { + if (image.width() > 16384 || height > 16384) { + throw vips::VError("Processed image is too large for the HEIF format"); + } } } @@ -569,6 +807,32 @@ namespace sharp { return warning; } + /* + Attach an event listener for progress updates, used to detect timeout + */ + void SetTimeout(VImage image, int const seconds) { + if (seconds > 0) { + VipsImage *im = image.get_image(); + if (im->progress_signal == NULL) { + int *timeout = VIPS_NEW(im, int); + *timeout = seconds; + g_signal_connect(im, "eval", G_CALLBACK(VipsProgressCallBack), timeout); + vips_image_set_progress(im, true); + } + } + } + + /* + Event listener for progress updates, used to detect timeout + */ + void VipsProgressCallBack(VipsImage *im, VipsProgress *progress, int *timeout) { + if (*timeout > 0 && progress->run >= *timeout) { + vips_image_set_kill(im, true); + vips_error("timeout", "%d%% complete", progress->percent); + *timeout = 0; + } + } + /* Calculate the (left, top) coordinates of the output image within the input image, applying the given gravity during an embed. @@ -710,50 +974,30 @@ namespace sharp { return interpretation == VIPS_INTERPRETATION_RGB16 || interpretation == VIPS_INTERPRETATION_GREY16; } - /* - Return the image alpha maximum. Useful for combining alpha bands. scRGB - images are 0 - 1 for image data, but the alpha is 0 - 255. - */ - double MaximumImageAlpha(VipsInterpretation const interpretation) { - return Is16Bit(interpretation) ? 65535.0 : 255.0; - } - - /* - Get boolean operation type from string - */ - VipsOperationBoolean GetBooleanOperation(std::string const opStr) { - return static_cast<VipsOperationBoolean>( - vips_enum_from_nick(nullptr, VIPS_TYPE_OPERATION_BOOLEAN, opStr.data())); - } - - /* - Get interpretation type from string - */ - VipsInterpretation GetInterpretation(std::string const typeStr) { - return static_cast<VipsInterpretation>( - vips_enum_from_nick(nullptr, VIPS_TYPE_INTERPRETATION, typeStr.data())); - } - /* Convert RGBA value to another colourspace */ - std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation) { + std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, + VipsInterpretation const interpretation, bool premultiply) { int const bands = static_cast<int>(rgba.size()); - if (bands < 3 || interpretation == VIPS_INTERPRETATION_sRGB || interpretation == VIPS_INTERPRETATION_RGB) { + if (bands < 3) { return rgba; - } else { - VImage pixel = VImage::new_matrix(1, 1); - pixel.set("bands", bands); - pixel = pixel.new_from_image(rgba); - pixel = pixel.colourspace(interpretation, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB)); - return pixel(0, 0); } + VImage pixel = VImage::new_matrix(1, 1); + pixel.set("bands", bands); + pixel = pixel + .new_from_image(rgba) + .colourspace(interpretation, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB)); + if (premultiply) { + pixel = pixel.premultiply(); + } + return pixel(0, 0); } /* Apply the alpha channel to a given colour */ - std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour) { + std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour, bool premultiply) { // Scale up 8-bit values to match 16-bit input image double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0; // Create alphaColour colour @@ -772,25 +1016,25 @@ namespace sharp { 0.0722 * colour[2]) }; } - // Add alpha channel to alphaColour colour - if (colour[3] < 255.0 || HasAlpha(image)) { - alphaColour.push_back(colour[3] * multiplier); + // Add alpha channel(s) to alphaColour colour + if (colour[3] < 255.0 || image.has_alpha()) { + int extraBands = image.bands() > 4 ? image.bands() - 3 : 1; + alphaColour.insert(alphaColour.end(), extraBands, colour[3] * multiplier); } // Ensure alphaColour colour uses correct colourspace - alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation()); + alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation(), premultiply); // Add non-transparent alpha channel, if required - if (colour[3] < 255.0 && !HasAlpha(image)) { - image = image.bandjoin( - VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)); + if (colour[3] < 255.0 && !image.has_alpha()) { + image = image.bandjoin_const({ 255 * multiplier }); } return std::make_tuple(image, alphaColour); } /* - Removes alpha channel, if any. + Removes alpha channels, if any. */ VImage RemoveAlpha(VImage image) { - if (HasAlpha(image)) { + while (image.bands() > 1 && image.has_alpha()) { image = image.extract_band(0, VImage::option()->set("n", image.bands() - 1)); } return image; @@ -799,11 +1043,87 @@ namespace sharp { /* Ensures alpha channel, if missing. */ - VImage EnsureAlpha(VImage image) { - if (!HasAlpha(image)) { - std::vector<double> alpha; - alpha.push_back(sharp::MaximumImageAlpha(image.interpretation())); - image = image.bandjoin_const(alpha); + VImage EnsureAlpha(VImage image, double const value) { + if (!image.has_alpha()) { + image = image.bandjoin_const({ value * vips_interpretation_max_alpha(image.interpretation()) }); + } + return image; + } + + std::pair<double, double> ResolveShrink(int width, int height, int targetWidth, int targetHeight, + Canvas canvas, bool withoutEnlargement, bool withoutReduction) { + double hshrink = 1.0; + double vshrink = 1.0; + + if (targetWidth > 0 && targetHeight > 0) { + // Fixed width and height + hshrink = static_cast<double>(width) / targetWidth; + vshrink = static_cast<double>(height) / targetHeight; + + switch (canvas) { + case Canvas::CROP: + case Canvas::MIN: + if (hshrink < vshrink) { + vshrink = hshrink; + } else { + hshrink = vshrink; + } + break; + case Canvas::EMBED: + case Canvas::MAX: + if (hshrink > vshrink) { + vshrink = hshrink; + } else { + hshrink = vshrink; + } + break; + case Canvas::IGNORE_ASPECT: + break; + } + } else if (targetWidth > 0) { + // Fixed width + hshrink = static_cast<double>(width) / targetWidth; + + if (canvas != Canvas::IGNORE_ASPECT) { + // Auto height + vshrink = hshrink; + } + } else if (targetHeight > 0) { + // Fixed height + vshrink = static_cast<double>(height) / targetHeight; + + if (canvas != Canvas::IGNORE_ASPECT) { + // Auto width + hshrink = vshrink; + } + } + + // We should not reduce or enlarge the output image, if + // withoutReduction or withoutEnlargement is specified. + if (withoutReduction) { + // Equivalent of VIPS_SIZE_UP + hshrink = std::min(1.0, hshrink); + vshrink = std::min(1.0, vshrink); + } else if (withoutEnlargement) { + // Equivalent of VIPS_SIZE_DOWN + hshrink = std::max(1.0, hshrink); + vshrink = std::max(1.0, vshrink); + } + + // We don't want to shrink so much that we send an axis to 0 + hshrink = std::min(hshrink, static_cast<double>(width)); + vshrink = std::min(vshrink, static_cast<double>(height)); + + return std::make_pair(hshrink, vshrink); + } + + /* + Ensure decoding remains sequential. + */ + VImage StaySequential(VImage image, bool condition) { + if (vips_image_is_sequential(image.get_image()) && condition) { + image = image.copy_memory().copy(); + image.remove(VIPS_META_SEQUENTIAL); } return image; } diff --git a/src/common.h b/src/common.h index 74eaddede..c15755bb0 100644 --- a/src/common.h +++ b/src/common.h @@ -1,22 +1,15 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #ifndef SRC_COMMON_H_ #define SRC_COMMON_H_ +#include <atomic> #include <string> #include <tuple> +#include <utility> #include <vector> #include <napi.h> @@ -25,18 +18,14 @@ // Verify platform and compiler compatibility #if (VIPS_MAJOR_VERSION < 8) || \ - (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 10) || \ - (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 10 && VIPS_MICRO_VERSION < 6) -#error "libvips version 8.10.6+ is required - please see https://sharp.pixelplumbing.com/install" + (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 17) || \ + (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 17 && VIPS_MICRO_VERSION < 3) +#error "libvips version 8.17.3+ is required - please see https://sharp.pixelplumbing.com/install" #endif -#if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))) -#error "GCC version 4.6+ is required for C++11 features - please see https://sharp.pixelplumbing.com/install" -#endif - -#if (defined(__clang__) && defined(__has_feature)) -#if (!__has_feature(cxx_range_for)) -#error "clang version 3.0+ is required for C++11 features - please see https://sharp.pixelplumbing.com/install" +#if defined(__has_include) +#if !__has_include(<filesystem>) +#error "C++17 compiler required - please see https://sharp.pixelplumbing.com/install" #endif #endif @@ -44,55 +33,112 @@ using vips::VImage; namespace sharp { - struct InputDescriptor { // NOLINT(runtime/indentation_namespace) + struct InputDescriptor { std::string name; std::string file; + bool autoOrient; char *buffer; - bool failOnError; - int limitInputPixels; + VipsFailOn failOn; + uint64_t limitInputPixels; + bool unlimited; VipsAccess access; size_t bufferLength; bool isBuffer; double density; + bool ignoreIcc; + VipsBandFormat rawDepth; int rawChannels; int rawWidth; int rawHeight; + bool rawPremultiplied; + int rawPageHeight; int pages; int page; - int level; int createChannels; int createWidth; int createHeight; + int createPageHeight; std::vector<double> createBackground; std::string createNoiseType; double createNoiseMean; double createNoiseSigma; + std::string textValue; + std::string textFont; + std::string textFontfile; + int textWidth; + int textHeight; + VipsAlign textAlign; + bool textJustify; + int textDpi; + bool textRgba; + int textSpacing; + VipsTextWrap textWrap; + int textAutofitDpi; + bool joinAnimated; + int joinAcross; + int joinShim; + std::vector<double> joinBackground; + VipsAlign joinHalign; + VipsAlign joinValign; + std::string svgStylesheet; + bool svgHighBitdepth; + int tiffSubifd; + int openSlideLevel; + std::vector<double> pdfBackground; + bool jp2Oneshot; InputDescriptor(): + autoOrient(false), buffer(nullptr), - failOnError(TRUE), + failOn(VIPS_FAIL_ON_WARNING), limitInputPixels(0x3FFF * 0x3FFF), - access(VIPS_ACCESS_RANDOM), + unlimited(false), + access(VIPS_ACCESS_SEQUENTIAL), bufferLength(0), - isBuffer(FALSE), + isBuffer(false), density(72.0), + ignoreIcc(false), + rawDepth(VIPS_FORMAT_UCHAR), rawChannels(0), rawWidth(0), rawHeight(0), + rawPremultiplied(false), + rawPageHeight(0), pages(1), page(0), - level(0), createChannels(0), createWidth(0), createHeight(0), + createPageHeight(0), createBackground{ 0.0, 0.0, 0.0, 255.0 }, createNoiseMean(0.0), - createNoiseSigma(0.0) {} + createNoiseSigma(0.0), + textWidth(0), + textHeight(0), + textAlign(VIPS_ALIGN_LOW), + textJustify(false), + textDpi(72), + textRgba(false), + textSpacing(0), + textWrap(VIPS_TEXT_WRAP_WORD), + textAutofitDpi(0), + joinAnimated(false), + joinAcross(1), + joinShim(0), + joinBackground{ 0.0, 0.0, 0.0, 255.0 }, + joinHalign(VIPS_ALIGN_LOW), + joinValign(VIPS_ALIGN_LOW), + svgHighBitdepth(false), + tiffSubifd(-1), + openSlideLevel(0), + pdfBackground{ 255.0, 255.0, 255.0, 255.0 }, + jp2Oneshot(false) {} }; // Convenience methods to access the attributes of a Napi::Object bool HasAttr(Napi::Object obj, std::string attr); std::string AttrAsStr(Napi::Object obj, std::string attr); + std::string AttrAsStr(Napi::Object obj, unsigned int const attr); uint32_t AttrAsUint32(Napi::Object obj, std::string attr); int32_t AttrAsInt32(Napi::Object obj, std::string attr); int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr); @@ -101,6 +147,10 @@ namespace sharp { bool AttrAsBool(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); + template <class T> T AttrAsEnum(Napi::Object obj, std::string attr, GType type) { + return static_cast<T>( + vips_enum_from_nick(nullptr, type, AttrAsStr(obj, attr).data())); + } // Create an InputDescriptor instance from a Napi::Object describing an input image InputDescriptor* CreateInputDescriptor(Napi::Object input); @@ -109,6 +159,7 @@ namespace sharp { JPEG, PNG, WEBP, + JP2, TIFF, GIF, SVG, @@ -119,31 +170,49 @@ namespace sharp { PPM, FITS, EXR, + JXL, + RAD, + DCRAW, VIPS, RAW, UNKNOWN, MISSING }; + enum class Canvas { + CROP, + EMBED, + MAX, + MIN, + IGNORE_ASPECT + }; + // How many tasks are in the queue? - extern volatile int counterQueue; + extern std::atomic<int> counterQueue; // How many tasks are being processed? - extern volatile int counterProcess; + extern std::atomic<int> counterProcess; // Filename extension checkers bool IsJpeg(std::string const &str); bool IsPng(std::string const &str); bool IsWebp(std::string const &str); + bool IsJp2(std::string const &str); bool IsGif(std::string const &str); bool IsTiff(std::string const &str); bool IsHeic(std::string const &str); bool IsHeif(std::string const &str); bool IsAvif(std::string const &str); + bool IsJxl(std::string const &str); bool IsDz(std::string const &str); bool IsDzZip(std::string const &str); bool IsV(std::string const &str); + /* + Trim space from end of string. + */ + std::string TrimEnd(std::string const &str); + /* Provide a string identifier for the given image type. */ @@ -160,9 +229,9 @@ namespace sharp { ImageType DetermineImageType(char const *file); /* - Does this image type support multiple pages? + Format-specific options builder */ - bool ImageTypeSupportsPage(ImageType imageType); + vips::VOption* GetOptionsForImageType(ImageType imageType, InputDescriptor *descriptor); /* Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data) @@ -175,10 +244,19 @@ namespace sharp { bool HasProfile(VImage image); /* - Does this image have an alpha channel? - Uses colour space interpretation with number of channels to guess this. + Get copy of embedded profile. */ - bool HasAlpha(VImage image); + std::pair<char*, size_t> GetProfile(VImage image); + + /* + Set embedded profile. + */ + VImage SetProfile(VImage image, std::pair<char*, size_t> icc); + + /* + Remove all EXIF-related image fields. + */ + VImage RemoveExif(VImage image); /* Get EXIF Orientation of image, if any. @@ -197,9 +275,18 @@ namespace sharp { /* Set animation properties if necessary. - Non-provided properties will be loaded from image. */ - VImage SetAnimationProperties(VImage image, int pageHeight, std::vector<int> delay, int loop); + VImage SetAnimationProperties(VImage image, int nPages, int pageHeight, std::vector<int> delay, int loop); + + /* + Remove animation properties from image. + */ + VImage RemoveAnimationProperties(VImage image); + + /* + Remove GIF palette from image. + */ + VImage RemoveGifPalette(VImage image); /* Does this image have a non-default density? @@ -216,6 +303,12 @@ namespace sharp { */ VImage SetDensity(VImage image, const double density); + /* + Multi-page images can have a page height. Fetch it, and sanity check it. + If page-height is not set, it defaults to the image height + */ + int GetPageHeight(VImage image); + /* Check the proposed format supports the current dimensions. */ @@ -236,6 +329,16 @@ namespace sharp { */ std::string VipsWarningPop(); + /* + Attach an event listener for progress updates, used to detect timeout + */ + void SetTimeout(VImage image, int const timeoutSeconds); + + /* + Event listener for progress updates, used to detect timeout + */ + void VipsProgressCallBack(VipsImage *image, VipsProgress *progress, int *timeoutSeconds); + /* Calculate the (left, top) coordinates of the output image within the input image, applying the given gravity during an embed. @@ -263,40 +366,36 @@ namespace sharp { bool Is16Bit(VipsInterpretation const interpretation); /* - Return the image alpha maximum. Useful for combining alpha bands. scRGB - images are 0 - 1 for image data, but the alpha is 0 - 255. + Convert RGBA value to another colourspace */ - double MaximumImageAlpha(VipsInterpretation const interpretation); + std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, + VipsInterpretation const interpretation, bool premultiply); /* - Get boolean operation type from string - */ - VipsOperationBoolean GetBooleanOperation(std::string const opStr); + Apply the alpha channel to a given colour + */ + std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour, bool premultiply); /* - Get interpretation type from string + Removes alpha channels, if any. */ - VipsInterpretation GetInterpretation(std::string const typeStr); + VImage RemoveAlpha(VImage image); /* - Convert RGBA value to another colourspace + Ensures alpha channel, if missing. */ - std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation); - - /* - Apply the alpha channel to a given colour - */ - std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour); + VImage EnsureAlpha(VImage image, double const value); /* - Removes alpha channel, if any. + Calculate the horizontal and vertical shrink factors, taking the canvas mode into account. */ - VImage RemoveAlpha(VImage image); + std::pair<double, double> ResolveShrink(int width, int height, int targetWidth, int targetHeight, + Canvas canvas, bool withoutEnlargement, bool withoutReduction); /* - Ensures alpha channel, if missing. + Ensure decoding remains sequential. */ - VImage EnsureAlpha(VImage image); + VImage StaySequential(VImage image, bool condition = true); } // namespace sharp diff --git a/src/emscripten/common.gypi b/src/emscripten/common.gypi new file mode 100644 index 000000000..714affe59 --- /dev/null +++ b/src/emscripten/common.gypi @@ -0,0 +1,39 @@ +# Copyright 2013 Lovell Fuller and others. +# SPDX-License-Identifier: Apache-2.0 + +{ + 'variables': { + 'OS': 'emscripten' + }, + 'target_defaults': { + 'default_configuration': 'Release', + 'type': 'executable', + 'cflags': [ + '-pthread' + ], + 'cflags_cc': [ + '-pthread' + ], + 'ldflags': [ + '--js-library=<!(node -p "require(\'emnapi\').js_library")', + '-sAUTO_JS_LIBRARIES=0', + '-sAUTO_NATIVE_LIBRARIES=0', + '-sDEFAULT_TO_CXX=0', + '-sNODEJS_CATCH_REJECTION=0' + ], + 'defines': [ + '__STDC_FORMAT_MACROS', + 'BUILDING_NODE_EXTENSION', + 'EMNAPI_WORKER_POOL_SIZE=1' + ], + 'include_dirs': [ + '<!(node -p "require(\'emnapi\').include_dir")' + ], + 'sources': [ + '<!@(node -p "require(\'emnapi\').sources.map(x => JSON.stringify(path.relative(process.cwd(), x))).join(\' \')")' + ], + 'configurations': { + 'Release': {} + } + } +} diff --git a/src/emscripten/pre.js b/src/emscripten/pre.js new file mode 100644 index 000000000..1163b9e77 --- /dev/null +++ b/src/emscripten/pre.js @@ -0,0 +1,21 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +/* global Module, ENV, _vips_shutdown, _uv_library_shutdown */ + +Module.preRun = () => { + ENV.VIPS_CONCURRENCY = Number(process.env.VIPS_CONCURRENCY) || 1; +}; + +Module.onRuntimeInitialized = () => { + module.exports = Module.emnapiInit({ + context: require('@emnapi/runtime').getDefaultContext() + }); + + process.once('exit', () => { + _vips_shutdown(); + _uv_library_shutdown(); + }); +}; diff --git a/src/libvips/cplusplus/VConnection.cpp b/src/libvips/cplusplus/VConnection.cpp deleted file mode 100644 index be7afc5ae..000000000 --- a/src/libvips/cplusplus/VConnection.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* Object part of the VSource and VTarget class - */ - -/* - - Copyright (C) 1991-2001 The National Gallery - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA - - */ - -/* - - These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk - - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /*HAVE_CONFIG_H*/ -#include <vips/intl.h> - -#include <vips/vips8> - -#include <vips/debug.h> - -/* -#define VIPS_DEBUG -#define VIPS_DEBUG_VERBOSE - */ - -VIPS_NAMESPACE_START - -VSource -VSource::new_from_descriptor( int descriptor ) -{ - VipsSource *input; - - if( !(input = vips_source_new_from_descriptor( descriptor )) ) - throw VError(); - - VSource out( input ); - - return( out ); -} - -VSource -VSource::new_from_file( const char *filename ) -{ - VipsSource *input; - - if( !(input = vips_source_new_from_file( filename )) ) - throw VError(); - - VSource out( input ); - - return( out ); -} - -VSource -VSource::new_from_blob( VipsBlob *blob ) -{ - VipsSource *input; - - if( !(input = vips_source_new_from_blob( blob )) ) - throw VError(); - - VSource out( input ); - - return( out ); -} - -VSource -VSource::new_from_memory( const void *data, - size_t size ) -{ - VipsSource *input; - - if( !(input = vips_source_new_from_memory( data, size )) ) - throw VError(); - - VSource out( input ); - - return( out ); -} - -VSource -VSource::new_from_options( const char *options ) -{ - VipsSource *input; - - if( !(input = vips_source_new_from_options( options )) ) - throw VError(); - - VSource out( input ); - - return( out ); -} - -VOption * -VOption::set( const char *name, const VSource value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, VIPS_TYPE_SOURCE ); - g_value_set_object( &pair->value, value.get_source() ); - options.push_back( pair ); - - return( this ); -} - -VTarget -VTarget::new_to_descriptor( int descriptor ) -{ - VipsTarget *output; - - if( !(output = vips_target_new_to_descriptor( descriptor )) ) - throw VError(); - - VTarget out( output ); - - return( out ); -} - -VTarget -VTarget::new_to_file( const char *filename ) -{ - VipsTarget *output; - - if( !(output = vips_target_new_to_file( filename )) ) - throw VError(); - - VTarget out( output ); - - return( out ); -} - -VTarget -VTarget::new_to_memory() -{ - VipsTarget *output; - - if( !(output = vips_target_new_to_memory()) ) - throw VError(); - - VTarget out( output ); - - return( out ); -} - -VOption * -VOption::set( const char *name, const VTarget value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, VIPS_TYPE_TARGET ); - g_value_set_object( &pair->value, value.get_target() ); - options.push_back( pair ); - - return( this ); -} - -VIPS_NAMESPACE_END diff --git a/src/libvips/cplusplus/VError.cpp b/src/libvips/cplusplus/VError.cpp deleted file mode 100644 index a3d821884..000000000 --- a/src/libvips/cplusplus/VError.cpp +++ /dev/null @@ -1,50 +0,0 @@ -// Code for error type - -/* - - Copyright (C) 1991-2001 The National Gallery - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA - - */ - -/* - - These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk - - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /*HAVE_CONFIG_H*/ -#include <vips/intl.h> - -#include <vips/vips8> - -VIPS_NAMESPACE_START - -std::ostream &operator<<( std::ostream &file, const VError &err ) -{ - err.ostream_print( file ); - return( file ); -} - -void VError::ostream_print( std::ostream &file ) const -{ - file << _what; -} - -VIPS_NAMESPACE_END diff --git a/src/libvips/cplusplus/VImage.cpp b/src/libvips/cplusplus/VImage.cpp deleted file mode 100644 index 27654f22a..000000000 --- a/src/libvips/cplusplus/VImage.cpp +++ /dev/null @@ -1,1467 +0,0 @@ -/* Object part of VImage class - * - * 30/12/14 - * - allow set enum value from string - * 10/6/16 - * - missing implementation of VImage::write() - * 11/6/16 - * - added arithmetic assignment overloads, += etc. - */ - -/* - - Copyright (C) 1991-2001 The National Gallery - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA - - */ - -/* - - These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk - - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /*HAVE_CONFIG_H*/ -#include <vips/intl.h> - -#include <vips/vips8> - -#include <vips/debug.h> - -/* -#define VIPS_DEBUG -#define VIPS_DEBUG_VERBOSE - */ - -VIPS_NAMESPACE_START - -std::vector<double> -to_vectorv( int n, ... ) -{ - std::vector<double> vector( n ); - va_list ap; - - va_start( ap, n ); - for( int i = 0; i < n; i++ ) - vector[i] = va_arg( ap, double ); - va_end( ap ); - - return( vector ); -} - -std::vector<double> -to_vector( double value ) -{ - return( to_vectorv( 1, value ) ); -} - -std::vector<double> -to_vector( int n, double array[] ) -{ - std::vector<double> vector( n ); - - for( int i = 0; i < n; i++ ) - vector[i] = array[i]; - - return( vector ); -} - -std::vector<double> -negate( std::vector<double> vector ) -{ - std::vector<double> new_vector( vector.size() ); - - for( unsigned int i = 0; i < vector.size(); i++ ) - new_vector[i] = vector[i] * -1; - - return( new_vector ); -} - -std::vector<double> -invert( std::vector<double> vector ) -{ - std::vector<double> new_vector( vector.size() ); - - for( unsigned int i = 0; i < vector.size(); i++ ) - new_vector[i] = 1.0 / vector[i]; - - return( new_vector ); -} - -VOption::~VOption() -{ - std::list<Pair *>::iterator i; - - for( i = options.begin(); i != options.end(); ++i ) - delete *i; -} - -// input bool -VOption * -VOption::set( const char *name, bool value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, G_TYPE_BOOLEAN ); - g_value_set_boolean( &pair->value, value ); - options.push_back( pair ); - - return( this ); -} - -// input int ... this path is used for enums as well -VOption * -VOption::set( const char *name, int value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, G_TYPE_INT ); - g_value_set_int( &pair->value, value ); - options.push_back( pair ); - - return( this ); -} - -// input double -VOption * -VOption::set( const char *name, double value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, G_TYPE_DOUBLE ); - g_value_set_double( &pair->value, value ); - options.push_back( pair ); - - return( this ); -} - -VOption * -VOption::set( const char *name, const char *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, G_TYPE_STRING ); - g_value_set_string( &pair->value, value ); - options.push_back( pair ); - - return( this ); -} - -// input image -VOption * -VOption::set( const char *name, const VImage value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, VIPS_TYPE_IMAGE ); - g_value_set_object( &pair->value, value.get_image() ); - options.push_back( pair ); - - return( this ); -} - -// input double array -VOption * -VOption::set( const char *name, std::vector<double> value ) -{ - Pair *pair = new Pair( name ); - - double *array; - unsigned int i; - - pair->input = true; - - g_value_init( &pair->value, VIPS_TYPE_ARRAY_DOUBLE ); - vips_value_set_array_double( &pair->value, NULL, - static_cast< int >( value.size() ) ); - array = vips_value_get_array_double( &pair->value, NULL ); - - for( i = 0; i < value.size(); i++ ) - array[i] = value[i]; - - options.push_back( pair ); - - return( this ); -} - -// input int array -VOption * -VOption::set( const char *name, std::vector<int> value ) -{ - Pair *pair = new Pair( name ); - - int *array; - unsigned int i; - - pair->input = true; - - g_value_init( &pair->value, VIPS_TYPE_ARRAY_INT ); - vips_value_set_array_int( &pair->value, NULL, - static_cast< int >( value.size() ) ); - array = vips_value_get_array_int( &pair->value, NULL ); - - for( i = 0; i < value.size(); i++ ) - array[i] = value[i]; - - options.push_back( pair ); - - return( this ); -} - -// input image array -VOption * -VOption::set( const char *name, std::vector<VImage> value ) -{ - Pair *pair = new Pair( name ); - - VipsImage **array; - unsigned int i; - - pair->input = true; - - g_value_init( &pair->value, VIPS_TYPE_ARRAY_IMAGE ); - vips_value_set_array_image( &pair->value, - static_cast< int >( value.size() ) ); - array = vips_value_get_array_image( &pair->value, NULL ); - - for( i = 0; i < value.size(); i++ ) { - VipsImage *vips_image = value[i].get_image(); - - array[i] = vips_image; - g_object_ref( vips_image ); - } - - options.push_back( pair ); - - return( this ); -} - -// input blob -VOption * -VOption::set( const char *name, VipsBlob *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, VIPS_TYPE_BLOB ); - g_value_set_boxed( &pair->value, value ); - options.push_back( pair ); - - return( this ); -} - -// output bool -VOption * -VOption::set( const char *name, bool *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = false; - pair->vbool = value; - g_value_init( &pair->value, G_TYPE_BOOLEAN ); - - options.push_back( pair ); - - return( this ); -} - -// output int -VOption * -VOption::set( const char *name, int *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = false; - pair->vint = value; - g_value_init( &pair->value, G_TYPE_INT ); - - options.push_back( pair ); - - return( this ); -} - -// output double -VOption * -VOption::set( const char *name, double *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = false; - pair->vdouble = value; - g_value_init( &pair->value, G_TYPE_DOUBLE ); - - options.push_back( pair ); - - return( this ); -} - -// output image -VOption * -VOption::set( const char *name, VImage *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = false; - pair->vimage = value; - g_value_init( &pair->value, VIPS_TYPE_IMAGE ); - - options.push_back( pair ); - - return( this ); -} - -// output doublearray -VOption * -VOption::set( const char *name, std::vector<double> *value ) -{ - Pair *pair = new Pair( name ); - - pair->input = false; - pair->vvector = value; - g_value_init( &pair->value, VIPS_TYPE_ARRAY_DOUBLE ); - - options.push_back( pair ); - - return( this ); -} - -// output blob -VOption * -VOption::set( const char *name, VipsBlob **value ) -{ - Pair *pair = new Pair( name ); - - pair->input = false; - pair->vblob = value; - g_value_init( &pair->value, VIPS_TYPE_BLOB ); - - options.push_back( pair ); - - return( this ); -} - -// just g_object_set_property(), except we allow set enum from string -static void -set_property( VipsObject *object, const char *name, const GValue *value ) -{ - VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object ); - GType type = G_VALUE_TYPE( value ); - - GParamSpec *pspec; - VipsArgumentClass *argument_class; - VipsArgumentInstance *argument_instance; - - if( vips_object_get_argument( object, name, - &pspec, &argument_class, &argument_instance ) ) { - g_warning( "%s", vips_error_buffer() ); - vips_error_clear(); - return; - } - - if( G_IS_PARAM_SPEC_ENUM( pspec ) && - type == G_TYPE_STRING ) { - GType pspec_type = G_PARAM_SPEC_VALUE_TYPE( pspec ); - - int enum_value; - GValue value2 = { 0 }; - - if( (enum_value = vips_enum_from_nick( object_class->nickname, - pspec_type, g_value_get_string( value ) )) < 0 ) { - g_warning( "%s", vips_error_buffer() ); - vips_error_clear(); - return; - } - - g_value_init( &value2, pspec_type ); - g_value_set_enum( &value2, enum_value ); - g_object_set_property( G_OBJECT( object ), name, &value2 ); - g_value_unset( &value2 ); - } - else - g_object_set_property( G_OBJECT( object ), name, value ); -} - -// walk the options and set props on the operation -void -VOption::set_operation( VipsOperation *operation ) -{ - std::list<Pair *>::iterator i; - - for( i = options.begin(); i != options.end(); ++i ) - if( (*i)->input ) { -#ifdef VIPS_DEBUG_VERBOSE - printf( "set_operation: " ); - vips_object_print_name( VIPS_OBJECT( operation ) ); - char *str_value = - g_strdup_value_contents( &(*i)->value ); - printf( ".%s = %s\n", (*i)->name, str_value ); - g_free( str_value ); -#endif /*VIPS_DEBUG_VERBOSE*/ - - set_property( VIPS_OBJECT( operation ), - (*i)->name, &(*i)->value ); - } -} - -// walk the options and fetch any requested outputs -void -VOption::get_operation( VipsOperation *operation ) -{ - std::list<Pair *>::iterator i; - - for( i = options.begin(); i != options.end(); ++i ) - if( ! (*i)->input ) { - const char *name = (*i)->name; - - g_object_get_property( G_OBJECT( operation ), - name, &(*i)->value ); - -#ifdef VIPS_DEBUG_VERBOSE - printf( "get_operation: " ); - vips_object_print_name( VIPS_OBJECT( operation ) ); - char *str_value = g_strdup_value_contents( - &(*i)->value ); - printf( ".%s = %s\n", name, str_value ); - g_free( str_value ); -#endif /*VIPS_DEBUG_VERBOSE*/ - - GValue *value = &(*i)->value; - GType type = G_VALUE_TYPE( value ); - - if( type == VIPS_TYPE_IMAGE ) { - // rebox object - VipsImage *image = VIPS_IMAGE( - g_value_get_object( value ) ); - *((*i)->vimage) = VImage( image ); - } - else if( type == G_TYPE_INT ) - *((*i)->vint) = g_value_get_int( value ); - else if( type == G_TYPE_BOOLEAN ) - *((*i)->vbool) = g_value_get_boolean( value ); - else if( type == G_TYPE_DOUBLE ) - *((*i)->vdouble) = g_value_get_double( value ); - else if( type == VIPS_TYPE_ARRAY_DOUBLE ) { - int length; - double *array = - vips_value_get_array_double( value, - &length ); - int j; - - ((*i)->vvector)->resize( length ); - for( j = 0; j < length; j++ ) - (*((*i)->vvector))[j] = array[j]; - } - else if( type == VIPS_TYPE_BLOB ) { - // our caller gets a reference - *((*i)->vblob) = - (VipsBlob *) g_value_dup_boxed( value ); - } - } -} - -void -VImage::call_option_string( const char *operation_name, - const char *option_string, VOption *options ) -{ - VipsOperation *operation; - - VIPS_DEBUG_MSG( "call_option_string: starting for %s ...\n", - operation_name ); - - if( !(operation = vips_operation_new( operation_name )) ) { - delete options; - throw( VError() ); - } - - /* Set str options before vargs options, so the user can't - * override things we set deliberately. - */ - if( option_string && - vips_object_set_from_string( VIPS_OBJECT( operation ), - option_string ) ) { - vips_object_unref_outputs( VIPS_OBJECT( operation ) ); - g_object_unref( operation ); - delete options; - throw( VError() ); - } - - if( options ) - options->set_operation( operation ); - - /* Build from cache. - */ - if( vips_cache_operation_buildp( &operation ) ) { - vips_object_unref_outputs( VIPS_OBJECT( operation ) ); - g_object_unref( operation ); - delete options; - throw( VError() ); - } - - /* Walk args again, writing output. - */ - if( options ) - options->get_operation( operation ); - - /* We're done with options! - */ - delete options; - - /* The operation we have built should now have been reffed by - * one of its arguments or have finished its work. Either - * way, we can unref. - */ - g_object_unref( operation ); -} - -void -VImage::call( const char *operation_name, VOption *options ) -{ - call_option_string( operation_name, NULL, options ); -} - -VImage -VImage::new_from_file( const char *name, VOption *options ) -{ - char filename[VIPS_PATH_MAX]; - char option_string[VIPS_PATH_MAX]; - const char *operation_name; - - VImage out; - - vips__filename_split8( name, filename, option_string ); - if( !(operation_name = vips_foreign_find_load( filename )) ) { - delete options; - throw VError(); - } - - call_option_string( operation_name, option_string, - (options ? options : VImage::option())-> - set( "filename", filename )-> - set( "out", &out ) ); - - return( out ); -} - -VImage -VImage::new_from_buffer( const void *buf, size_t len, const char *option_string, - VOption *options ) -{ - const char *operation_name; - VipsBlob *blob; - VImage out; - - if( !(operation_name = vips_foreign_find_load_buffer( buf, len )) ) { - delete options; - throw( VError() ); - } - - /* We don't take a copy of the data or free it. - */ - blob = vips_blob_new( NULL, buf, len ); - options = (options ? options : VImage::option())-> - set( "buffer", blob )-> - set( "out", &out ); - vips_area_unref( VIPS_AREA( blob ) ); - - call_option_string( operation_name, option_string, options ); - - return( out ); -} - -VImage -VImage::new_from_buffer( const std::string &buf, const char *option_string, - VOption *options ) -{ - return( new_from_buffer( buf.c_str(), buf.size(), - option_string, options ) ); -} - -VImage -VImage::new_from_source( VSource source, const char *option_string, - VOption *options ) -{ - const char *operation_name; - VImage out; - - if( !(operation_name = vips_foreign_find_load_source( - source.get_source() )) ) { - delete options; - throw( VError() ); - } - - options = (options ? options : VImage::option())-> - set( "source", source )-> - set( "out", &out ); - - call_option_string( operation_name, option_string, options ); - - return( out ); -} - -VImage -VImage::new_matrix( int width, int height ) -{ - return( VImage( vips_image_new_matrix( width, height ) ) ); -} - -VImage -VImage::new_matrixv( int width, int height, ... ) -{ - VImage matrix = new_matrix( width, height ); - VipsImage *vips_matrix = matrix.get_image(); - - va_list ap; - - va_start( ap, height ); - for( int y = 0; y < height; y++ ) - for( int x = 0; x < width; x++ ) - *VIPS_MATRIX( vips_matrix, x, y ) = - va_arg( ap, double ); - va_end( ap ); - - return( matrix ); -} - -VImage -VImage::write( VImage out ) const -{ - if( vips_image_write( this->get_image(), out.get_image() ) ) - throw VError(); - - return( out ); -} - -void -VImage::write_to_file( const char *name, VOption *options ) const -{ - char filename[VIPS_PATH_MAX]; - char option_string[VIPS_PATH_MAX]; - const char *operation_name; - - vips__filename_split8( name, filename, option_string ); - if( !(operation_name = vips_foreign_find_save( filename )) ) { - delete options; - throw VError(); - } - - call_option_string( operation_name, option_string, - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -void -VImage::write_to_buffer( const char *suffix, void **buf, size_t *size, - VOption *options ) const -{ - char filename[VIPS_PATH_MAX]; - char option_string[VIPS_PATH_MAX]; - const char *operation_name; - VipsBlob *blob; - - vips__filename_split8( suffix, filename, option_string ); - if( !(operation_name = vips_foreign_find_save_buffer( filename )) ) { - delete options; - throw VError(); - } - - call_option_string( operation_name, option_string, - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &blob ) ); - - if( blob ) { - if( buf ) { - *buf = VIPS_AREA( blob )->data; - VIPS_AREA( blob )->free_fn = NULL; - } - if( size ) - *size = VIPS_AREA( blob )->length; - - vips_area_unref( VIPS_AREA( blob ) ); - } -} - -void -VImage::write_to_target( const char *suffix, VTarget target, - VOption *options ) const -{ - char filename[VIPS_PATH_MAX]; - char option_string[VIPS_PATH_MAX]; - const char *operation_name; - - vips__filename_split8( suffix, filename, option_string ); - if( !(operation_name = vips_foreign_find_save_target( filename )) ) { - delete options; - throw VError(); - } - - call_option_string( operation_name, option_string, - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -#include "vips-operators.cpp" - -std::vector<VImage> -VImage::bandsplit( VOption *options ) const -{ - std::vector<VImage> b; - - for( int i = 0; i < bands(); i++ ) - b.push_back( extract_band( i ) ); - - return( b ); -} - -VImage -VImage::bandjoin( VImage other, VOption *options ) const -{ - VImage v[2] = { *this, other }; - std::vector<VImage> vec( v, v + VIPS_NUMBER( v ) ); - - return( bandjoin( vec, options ) ); -} - -VImage -VImage::composite( VImage other, VipsBlendMode mode, VOption *options ) const -{ - VImage v[2] = { *this, other }; - std::vector<VImage> ivec( v, v + VIPS_NUMBER( v ) ); - int m[1] = { static_cast<int>( mode ) }; - std::vector<int> mvec( m, m + VIPS_NUMBER( m ) ); - - return( composite( ivec, mvec, options ) ); -} - -std::complex<double> -VImage::minpos( VOption *options ) const -{ - double x, y; - - (void) min( - (options ? options : VImage::option()) -> - set( "x", &x ) -> - set( "y", &y ) ); - - return( std::complex<double>( x, y ) ); -} - -std::complex<double> -VImage::maxpos( VOption *options ) const -{ - double x, y; - - (void) max( - (options ? options : VImage::option()) -> - set( "x", &x ) -> - set( "y", &y ) ); - - return( std::complex<double>( x, y ) ); -} - -// Operator overloads - -VImage -VImage::operator[]( int index ) const -{ - return( this->extract_band( index ) ); -} - -std::vector<double> -VImage::operator()( int x, int y ) const -{ - return( this->getpoint( x, y ) ); -} - -VImage -operator+( const VImage a, const VImage b ) -{ - return( a.add( b ) ); -} - -VImage -operator+( double a, const VImage b ) -{ - return( b.linear( 1.0, a ) ); -} - -VImage -operator+( const VImage a, double b ) -{ - return( a.linear( 1.0, b ) ); -} - -VImage -operator+( const std::vector<double> a, const VImage b ) -{ - return( b.linear( 1.0, a ) ); -} - -VImage -operator+( const VImage a, const std::vector<double> b ) -{ - return( a.linear( 1.0, b ) ); -} - -VImage & -operator+=( VImage &a, const VImage b ) -{ - return( a = a + b ); -} - -VImage & -operator+=( VImage &a, const double b ) -{ - return( a = a + b ); -} - -VImage & -operator+=( VImage &a, const std::vector<double> b ) -{ - return( a = a + b ); -} - -VImage -operator-( const VImage a, const VImage b ) -{ - return( a.subtract( b ) ); -} - -VImage -operator-( double a, const VImage b ) -{ - return( b.linear( -1.0, a ) ); -} - -VImage -operator-( const VImage a, double b ) -{ - return( a.linear( 1.0, -b ) ); -} - -VImage -operator-( const std::vector<double> a, const VImage b ) -{ - return( b.linear( -1.0, a ) ); -} - -VImage -operator-( const VImage a, const std::vector<double> b ) -{ - return( a.linear( 1.0, vips::negate( b ) ) ); -} - -VImage & -operator-=( VImage &a, const VImage b ) -{ - return( a = a - b ); -} - -VImage & -operator-=( VImage &a, const double b ) -{ - return( a = a - b ); -} - -VImage & -operator-=( VImage &a, const std::vector<double> b ) -{ - return( a = a - b ); -} - -VImage -operator-( const VImage a ) -{ - return( a * -1 ); -} - -VImage -operator*( const VImage a, const VImage b ) -{ - return( a.multiply( b ) ); -} - -VImage -operator*( double a, const VImage b ) -{ - return( b.linear( a, 0.0 ) ); -} - -VImage -operator*( const VImage a, double b ) -{ - return( a.linear( b, 0.0 ) ); -} - -VImage -operator*( const std::vector<double> a, const VImage b ) -{ - return( b.linear( a, 0.0 ) ); -} - -VImage -operator*( const VImage a, const std::vector<double> b ) -{ - return( a.linear( b, 0.0 ) ); -} - -VImage & -operator*=( VImage &a, const VImage b ) -{ - return( a = a * b ); -} - -VImage & -operator*=( VImage &a, const double b ) -{ - return( a = a * b ); -} - -VImage & -operator*=( VImage &a, const std::vector<double> b ) -{ - return( a = a * b ); -} - -VImage -operator/( const VImage a, const VImage b ) -{ - return( a.divide( b ) ); -} - -VImage -operator/( double a, const VImage b ) -{ - return( b.pow( -1.0 ).linear( a, 0.0 ) ); -} - -VImage -operator/( const VImage a, double b ) -{ - return( a.linear( 1.0 / b, 0.0 ) ); -} - -VImage -operator/( const std::vector<double> a, const VImage b ) -{ - return( b.pow( -1.0 ).linear( a, 0.0 ) ); -} - -VImage -operator/( const VImage a, const std::vector<double> b ) -{ - return( a.linear( vips::invert( b ), 0.0 ) ); -} - -VImage & -operator/=( VImage &a, const VImage b ) -{ - return( a = a / b ); -} - -VImage & -operator/=( VImage &a, const double b ) -{ - return( a = a / b ); -} - -VImage & -operator/=( VImage &a, const std::vector<double> b ) -{ - return( a = a / b ); -} - -VImage -operator%( const VImage a, const VImage b ) -{ - return( a.remainder( b ) ); -} - -VImage -operator%( const VImage a, const double b ) -{ - return( a.remainder_const( to_vector( b ) ) ); -} - -VImage -operator%( const VImage a, const std::vector<double> b ) -{ - return( a.remainder_const( b ) ); -} - -VImage & -operator%=( VImage &a, const VImage b ) -{ - return( a = a % b ); -} - -VImage & -operator%=( VImage &a, const double b ) -{ - return( a = a % b ); -} - -VImage & -operator%=( VImage &a, const std::vector<double> b ) -{ - return( a = a % b ); -} - -VImage -operator<( const VImage a, const VImage b ) -{ - return( a.relational( b, VIPS_OPERATION_RELATIONAL_LESS ) ); -} - -VImage -operator<( const double a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_MORE, - to_vector( a ) ) ); -} - -VImage -operator<( const VImage a, const double b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESS, - to_vector( b ) ) ); -} - -VImage -operator<( const std::vector<double> a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_MORE, - a ) ); -} - -VImage -operator<( const VImage a, const std::vector<double> b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESS, - b ) ); -} - -VImage -operator<=( const VImage a, const VImage b ) -{ - return( a.relational( b, VIPS_OPERATION_RELATIONAL_LESSEQ ) ); -} - -VImage -operator<=( const double a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ, - to_vector( a ) ) ); -} - -VImage -operator<=( const VImage a, const double b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ, - to_vector( b ) ) ); -} - -VImage -operator<=( const std::vector<double> a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ, - a ) ); -} - -VImage -operator<=( const VImage a, const std::vector<double> b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ, - b ) ); -} - -VImage -operator>( const VImage a, const VImage b ) -{ - return( a.relational( b, VIPS_OPERATION_RELATIONAL_MORE ) ); -} - -VImage -operator>( const double a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESS, - to_vector( a ) ) ); -} - -VImage -operator>( const VImage a, const double b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_MORE, - to_vector( b ) ) ); -} - -VImage -operator>( const std::vector<double> a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESS, - a ) ); -} - -VImage -operator>( const VImage a, const std::vector<double> b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_MORE, - b ) ); -} - -VImage -operator>=( const VImage a, const VImage b ) -{ - return( a.relational( b, VIPS_OPERATION_RELATIONAL_MOREEQ ) ); -} - -VImage -operator>=( const double a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ, - to_vector( a ) ) ); -} - -VImage -operator>=( const VImage a, const double b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ, - to_vector( b ) ) ); -} - -VImage -operator>=( const std::vector<double> a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ, - a ) ); -} - -VImage -operator>=( const VImage a, const std::vector<double> b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ, - b ) ); -} - -VImage -operator==( const VImage a, const VImage b ) -{ - return( a.relational( b, VIPS_OPERATION_RELATIONAL_EQUAL ) ); -} - -VImage -operator==( const double a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL, - to_vector( a ) ) ); -} - -VImage -operator==( const VImage a, const double b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL, - to_vector( b ) ) ); -} - -VImage -operator==( const std::vector<double> a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL, - a ) ); -} - -VImage -operator==( const VImage a, const std::vector<double> b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL, - b ) ); -} - -VImage -operator!=( const VImage a, const VImage b ) -{ - return( a.relational( b, VIPS_OPERATION_RELATIONAL_NOTEQ ) ); -} - -VImage -operator!=( const double a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ, - to_vector( a ) ) ); -} - -VImage -operator!=( const VImage a, const double b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ, - to_vector( b ) ) ); -} - -VImage -operator!=( const std::vector<double> a, const VImage b ) -{ - return( b.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ, - a ) ); -} - -VImage -operator!=( const VImage a, const std::vector<double> b ) -{ - return( a.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ, - b ) ); -} - -VImage -operator&( const VImage a, const VImage b ) -{ - return( a.boolean( b, VIPS_OPERATION_BOOLEAN_AND ) ); -} - -VImage -operator&( const double a, const VImage b ) -{ - return( b.boolean_const( VIPS_OPERATION_BOOLEAN_AND, - to_vector( a ) ) ); -} - -VImage -operator&( const VImage a, const double b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_AND, - to_vector( b ) ) ); -} - -VImage -operator&( const std::vector<double> a, const VImage b ) -{ - return( b.boolean_const( VIPS_OPERATION_BOOLEAN_AND, a ) ); -} - -VImage -operator&( const VImage a, const std::vector<double> b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_AND, b ) ); -} - -VImage & -operator&=( VImage &a, const VImage b ) -{ - return( a = a & b ); -} - -VImage & -operator&=( VImage &a, const double b ) -{ - return( a = a & b ); -} - -VImage & -operator&=( VImage &a, const std::vector<double> b ) -{ - return( a = a & b ); -} - -VImage -operator|( const VImage a, const VImage b ) -{ - return( a.boolean( b, VIPS_OPERATION_BOOLEAN_OR ) ); -} - -VImage -operator|( const double a, const VImage b ) -{ - return( b.boolean_const( VIPS_OPERATION_BOOLEAN_OR, - to_vector( a ) ) ); -} - -VImage -operator|( const VImage a, const double b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_OR, - to_vector( b ) ) ); -} - -VImage -operator|( const std::vector<double> a, const VImage b ) -{ - return( b.boolean_const( VIPS_OPERATION_BOOLEAN_OR, - a ) ); -} - -VImage -operator|( const VImage a, const std::vector<double> b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_OR, - b ) ); -} - -VImage & -operator|=( VImage &a, const VImage b ) -{ - return( a = a | b ); -} - -VImage & -operator|=( VImage &a, const double b ) -{ - return( a = a | b ); -} - -VImage & -operator|=( VImage &a, const std::vector<double> b ) -{ - return( a = a | b ); -} - -VImage -operator^( const VImage a, const VImage b ) -{ - return( a.boolean( b, VIPS_OPERATION_BOOLEAN_EOR ) ); -} - -VImage -operator^( const double a, const VImage b ) -{ - return( b.boolean_const( VIPS_OPERATION_BOOLEAN_EOR, - to_vector( a ) ) ); -} - -VImage -operator^( const VImage a, const double b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_EOR, - to_vector( b ) ) ); -} - -VImage -operator^( const std::vector<double> a, const VImage b ) -{ - return( b.boolean_const( VIPS_OPERATION_BOOLEAN_EOR, - a ) ); -} - -VImage -operator^( const VImage a, const std::vector<double> b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_EOR, - b ) ); -} - -VImage & -operator^=( VImage &a, const VImage b ) -{ - return( a = a ^ b ); -} - -VImage & -operator^=( VImage &a, const double b ) -{ - return( a = a ^ b ); -} - -VImage & -operator^=( VImage &a, const std::vector<double> b ) -{ - return( a = a ^ b ); -} - -VImage -operator<<( const VImage a, const VImage b ) -{ - return( a.boolean( b, VIPS_OPERATION_BOOLEAN_LSHIFT ) ); -} - -VImage -operator<<( const VImage a, const double b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_LSHIFT, - to_vector( b ) ) ); -} - -VImage -operator<<( const VImage a, const std::vector<double> b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_LSHIFT, - b ) ); -} - -VImage & -operator<<=( VImage &a, const VImage b ) -{ - return( a = a << b ); -} - -VImage & -operator<<=( VImage &a, const double b ) -{ - return( a = a << b ); -} - -VImage & -operator<<=( VImage &a, const std::vector<double> b ) -{ - return( a = a << b ); -} - -VImage -operator>>( const VImage a, const VImage b ) -{ - return( a.boolean( b, VIPS_OPERATION_BOOLEAN_RSHIFT ) ); -} - -VImage -operator>>( const VImage a, const double b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_RSHIFT, - to_vector( b ) ) ); -} - -VImage -operator>>( const VImage a, const std::vector<double> b ) -{ - return( a.boolean_const( VIPS_OPERATION_BOOLEAN_RSHIFT, - b ) ); -} - -VImage & -operator>>=( VImage &a, const VImage b ) -{ - return( a = a << b ); -} - -VImage & -operator>>=( VImage &a, const double b ) -{ - return( a = a << b ); -} - -VImage & -operator>>=( VImage &a, const std::vector<double> b ) -{ - return( a = a << b ); -} - -VIPS_NAMESPACE_END diff --git a/src/libvips/cplusplus/VInterpolate.cpp b/src/libvips/cplusplus/VInterpolate.cpp deleted file mode 100644 index cb59715c0..000000000 --- a/src/libvips/cplusplus/VInterpolate.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* Object part of VInterpolate class - */ - -/* - - Copyright (C) 1991-2001 The National Gallery - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA - - */ - -/* - - These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk - - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /*HAVE_CONFIG_H*/ -#include <vips/intl.h> - -#include <vips/vips8> - -#include <vips/debug.h> - -/* -#define VIPS_DEBUG -#define VIPS_DEBUG_VERBOSE - */ - -VIPS_NAMESPACE_START - -VInterpolate -VInterpolate::new_from_name( const char *name, VOption *options ) -{ - VipsInterpolate *interp; - - if( !(interp = vips_interpolate_new( name )) ) { - delete options; - throw VError(); - } - delete options; - - VInterpolate out( interp ); - - return( out ); -} - -VOption * -VOption::set( const char *name, const VInterpolate value ) -{ - Pair *pair = new Pair( name ); - - pair->input = true; - g_value_init( &pair->value, VIPS_TYPE_INTERPOLATE ); - g_value_set_object( &pair->value, value.get_interpolate() ); - options.push_back( pair ); - - return( this ); -} - -VIPS_NAMESPACE_END diff --git a/src/libvips/cplusplus/vips-operators.cpp b/src/libvips/cplusplus/vips-operators.cpp deleted file mode 100644 index f8456a5f5..000000000 --- a/src/libvips/cplusplus/vips-operators.cpp +++ /dev/null @@ -1,3526 +0,0 @@ -// bodies for vips operations -// Sun 5 Jul 22:36:37 BST 2020 -// this file is generated automatically, do not edit! - -VImage VImage::CMC2LCh( VOption *options ) const -{ - VImage out; - - call( "CMC2LCh", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::CMYK2XYZ( VOption *options ) const -{ - VImage out; - - call( "CMYK2XYZ", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::HSV2sRGB( VOption *options ) const -{ - VImage out; - - call( "HSV2sRGB", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LCh2CMC( VOption *options ) const -{ - VImage out; - - call( "LCh2CMC", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LCh2Lab( VOption *options ) const -{ - VImage out; - - call( "LCh2Lab", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::Lab2LCh( VOption *options ) const -{ - VImage out; - - call( "Lab2LCh", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::Lab2LabQ( VOption *options ) const -{ - VImage out; - - call( "Lab2LabQ", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::Lab2LabS( VOption *options ) const -{ - VImage out; - - call( "Lab2LabS", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::Lab2XYZ( VOption *options ) const -{ - VImage out; - - call( "Lab2XYZ", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LabQ2Lab( VOption *options ) const -{ - VImage out; - - call( "LabQ2Lab", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LabQ2LabS( VOption *options ) const -{ - VImage out; - - call( "LabQ2LabS", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LabQ2sRGB( VOption *options ) const -{ - VImage out; - - call( "LabQ2sRGB", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LabS2Lab( VOption *options ) const -{ - VImage out; - - call( "LabS2Lab", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::LabS2LabQ( VOption *options ) const -{ - VImage out; - - call( "LabS2LabQ", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::XYZ2CMYK( VOption *options ) const -{ - VImage out; - - call( "XYZ2CMYK", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::XYZ2Lab( VOption *options ) const -{ - VImage out; - - call( "XYZ2Lab", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::XYZ2Yxy( VOption *options ) const -{ - VImage out; - - call( "XYZ2Yxy", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::XYZ2scRGB( VOption *options ) const -{ - VImage out; - - call( "XYZ2scRGB", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::Yxy2XYZ( VOption *options ) const -{ - VImage out; - - call( "Yxy2XYZ", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::abs( VOption *options ) const -{ - VImage out; - - call( "abs", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::add( VImage right, VOption *options ) const -{ - VImage out; - - call( "add", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::affine( std::vector<double> matrix, VOption *options ) const -{ - VImage out; - - call( "affine", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "matrix", matrix ) ); - - return( out ); -} - -VImage VImage::analyzeload( const char *filename, VOption *options ) -{ - VImage out; - - call( "analyzeload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::arrayjoin( std::vector<VImage> in, VOption *options ) -{ - VImage out; - - call( "arrayjoin", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "in", in ) ); - - return( out ); -} - -VImage VImage::autorot( VOption *options ) const -{ - VImage out; - - call( "autorot", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -double VImage::avg( VOption *options ) const -{ - double out; - - call( "avg", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::bandbool( VipsOperationBoolean boolean, VOption *options ) const -{ - VImage out; - - call( "bandbool", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "boolean", boolean ) ); - - return( out ); -} - -VImage VImage::bandfold( VOption *options ) const -{ - VImage out; - - call( "bandfold", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::bandjoin( std::vector<VImage> in, VOption *options ) -{ - VImage out; - - call( "bandjoin", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "in", in ) ); - - return( out ); -} - -VImage VImage::bandjoin_const( std::vector<double> c, VOption *options ) const -{ - VImage out; - - call( "bandjoin_const", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "c", c ) ); - - return( out ); -} - -VImage VImage::bandmean( VOption *options ) const -{ - VImage out; - - call( "bandmean", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::bandrank( std::vector<VImage> in, VOption *options ) -{ - VImage out; - - call( "bandrank", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "in", in ) ); - - return( out ); -} - -VImage VImage::bandunfold( VOption *options ) const -{ - VImage out; - - call( "bandunfold", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::black( int width, int height, VOption *options ) -{ - VImage out; - - call( "black", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::boolean( VImage right, VipsOperationBoolean boolean, VOption *options ) const -{ - VImage out; - - call( "boolean", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right )-> - set( "boolean", boolean ) ); - - return( out ); -} - -VImage VImage::boolean_const( VipsOperationBoolean boolean, std::vector<double> c, VOption *options ) const -{ - VImage out; - - call( "boolean_const", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "boolean", boolean )-> - set( "c", c ) ); - - return( out ); -} - -VImage VImage::buildlut( VOption *options ) const -{ - VImage out; - - call( "buildlut", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::byteswap( VOption *options ) const -{ - VImage out; - - call( "byteswap", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::cache( VOption *options ) const -{ - VImage out; - - call( "cache", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::canny( VOption *options ) const -{ - VImage out; - - call( "canny", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::case_image( std::vector<VImage> cases, VOption *options ) const -{ - VImage out; - - call( "case", - (options ? options : VImage::option())-> - set( "index", *this )-> - set( "out", &out )-> - set( "cases", cases ) ); - - return( out ); -} - -VImage VImage::cast( VipsBandFormat format, VOption *options ) const -{ - VImage out; - - call( "cast", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "format", format ) ); - - return( out ); -} - -VImage VImage::colourspace( VipsInterpretation space, VOption *options ) const -{ - VImage out; - - call( "colourspace", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "space", space ) ); - - return( out ); -} - -VImage VImage::compass( VImage mask, VOption *options ) const -{ - VImage out; - - call( "compass", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::complex( VipsOperationComplex cmplx, VOption *options ) const -{ - VImage out; - - call( "complex", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "cmplx", cmplx ) ); - - return( out ); -} - -VImage VImage::complex2( VImage right, VipsOperationComplex2 cmplx, VOption *options ) const -{ - VImage out; - - call( "complex2", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right )-> - set( "cmplx", cmplx ) ); - - return( out ); -} - -VImage VImage::complexform( VImage right, VOption *options ) const -{ - VImage out; - - call( "complexform", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::complexget( VipsOperationComplexget get, VOption *options ) const -{ - VImage out; - - call( "complexget", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "get", get ) ); - - return( out ); -} - -VImage VImage::composite( std::vector<VImage> in, std::vector<int> mode, VOption *options ) -{ - VImage out; - - call( "composite", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "in", in )-> - set( "mode", mode ) ); - - return( out ); -} - -VImage VImage::composite2( VImage overlay, VipsBlendMode mode, VOption *options ) const -{ - VImage out; - - call( "composite2", - (options ? options : VImage::option())-> - set( "base", *this )-> - set( "out", &out )-> - set( "overlay", overlay )-> - set( "mode", mode ) ); - - return( out ); -} - -VImage VImage::conv( VImage mask, VOption *options ) const -{ - VImage out; - - call( "conv", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::conva( VImage mask, VOption *options ) const -{ - VImage out; - - call( "conva", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::convasep( VImage mask, VOption *options ) const -{ - VImage out; - - call( "convasep", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::convf( VImage mask, VOption *options ) const -{ - VImage out; - - call( "convf", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::convi( VImage mask, VOption *options ) const -{ - VImage out; - - call( "convi", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::convsep( VImage mask, VOption *options ) const -{ - VImage out; - - call( "convsep", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::copy( VOption *options ) const -{ - VImage out; - - call( "copy", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -double VImage::countlines( VipsDirection direction, VOption *options ) const -{ - double nolines; - - call( "countlines", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "nolines", &nolines )-> - set( "direction", direction ) ); - - return( nolines ); -} - -VImage VImage::crop( int left, int top, int width, int height, VOption *options ) const -{ - VImage out; - - call( "crop", - (options ? options : VImage::option())-> - set( "input", *this )-> - set( "out", &out )-> - set( "left", left )-> - set( "top", top )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::csvload( const char *filename, VOption *options ) -{ - VImage out; - - call( "csvload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::csvload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "csvload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::csvsave( const char *filename, VOption *options ) const -{ - call( "csvsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -void VImage::csvsave_target( VTarget target, VOption *options ) const -{ - call( "csvsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::dE00( VImage right, VOption *options ) const -{ - VImage out; - - call( "dE00", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::dE76( VImage right, VOption *options ) const -{ - VImage out; - - call( "dE76", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::dECMC( VImage right, VOption *options ) const -{ - VImage out; - - call( "dECMC", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -double VImage::deviate( VOption *options ) const -{ - double out; - - call( "deviate", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::divide( VImage right, VOption *options ) const -{ - VImage out; - - call( "divide", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -void VImage::draw_circle( std::vector<double> ink, int cx, int cy, int radius, VOption *options ) const -{ - call( "draw_circle", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "ink", ink )-> - set( "cx", cx )-> - set( "cy", cy )-> - set( "radius", radius ) ); -} - -void VImage::draw_flood( std::vector<double> ink, int x, int y, VOption *options ) const -{ - call( "draw_flood", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "ink", ink )-> - set( "x", x )-> - set( "y", y ) ); -} - -void VImage::draw_image( VImage sub, int x, int y, VOption *options ) const -{ - call( "draw_image", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "sub", sub )-> - set( "x", x )-> - set( "y", y ) ); -} - -void VImage::draw_line( std::vector<double> ink, int x1, int y1, int x2, int y2, VOption *options ) const -{ - call( "draw_line", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "ink", ink )-> - set( "x1", x1 )-> - set( "y1", y1 )-> - set( "x2", x2 )-> - set( "y2", y2 ) ); -} - -void VImage::draw_mask( std::vector<double> ink, VImage mask, int x, int y, VOption *options ) const -{ - call( "draw_mask", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "ink", ink )-> - set( "mask", mask )-> - set( "x", x )-> - set( "y", y ) ); -} - -void VImage::draw_rect( std::vector<double> ink, int left, int top, int width, int height, VOption *options ) const -{ - call( "draw_rect", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "ink", ink )-> - set( "left", left )-> - set( "top", top )-> - set( "width", width )-> - set( "height", height ) ); -} - -void VImage::draw_smudge( int left, int top, int width, int height, VOption *options ) const -{ - call( "draw_smudge", - (options ? options : VImage::option())-> - set( "image", *this )-> - set( "left", left )-> - set( "top", top )-> - set( "width", width )-> - set( "height", height ) ); -} - -void VImage::dzsave( const char *filename, VOption *options ) const -{ - call( "dzsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::dzsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "dzsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -VImage VImage::embed( int x, int y, int width, int height, VOption *options ) const -{ - VImage out; - - call( "embed", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "x", x )-> - set( "y", y )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::extract_area( int left, int top, int width, int height, VOption *options ) const -{ - VImage out; - - call( "extract_area", - (options ? options : VImage::option())-> - set( "input", *this )-> - set( "out", &out )-> - set( "left", left )-> - set( "top", top )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::extract_band( int band, VOption *options ) const -{ - VImage out; - - call( "extract_band", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "band", band ) ); - - return( out ); -} - -VImage VImage::eye( int width, int height, VOption *options ) -{ - VImage out; - - call( "eye", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::falsecolour( VOption *options ) const -{ - VImage out; - - call( "falsecolour", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::fastcor( VImage ref, VOption *options ) const -{ - VImage out; - - call( "fastcor", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "ref", ref ) ); - - return( out ); -} - -VImage VImage::fill_nearest( VOption *options ) const -{ - VImage out; - - call( "fill_nearest", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -int VImage::find_trim( int *top, int *width, int *height, VOption *options ) const -{ - int left; - - call( "find_trim", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "left", &left )-> - set( "top", top )-> - set( "width", width )-> - set( "height", height ) ); - - return( left ); -} - -VImage VImage::fitsload( const char *filename, VOption *options ) -{ - VImage out; - - call( "fitsload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -void VImage::fitssave( const char *filename, VOption *options ) const -{ - call( "fitssave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VImage VImage::flatten( VOption *options ) const -{ - VImage out; - - call( "flatten", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::flip( VipsDirection direction, VOption *options ) const -{ - VImage out; - - call( "flip", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "direction", direction ) ); - - return( out ); -} - -VImage VImage::float2rad( VOption *options ) const -{ - VImage out; - - call( "float2rad", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::fractsurf( int width, int height, double fractal_dimension, VOption *options ) -{ - VImage out; - - call( "fractsurf", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "fractal_dimension", fractal_dimension ) ); - - return( out ); -} - -VImage VImage::freqmult( VImage mask, VOption *options ) const -{ - VImage out; - - call( "freqmult", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask ) ); - - return( out ); -} - -VImage VImage::fwfft( VOption *options ) const -{ - VImage out; - - call( "fwfft", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::gamma( VOption *options ) const -{ - VImage out; - - call( "gamma", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::gaussblur( double sigma, VOption *options ) const -{ - VImage out; - - call( "gaussblur", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "sigma", sigma ) ); - - return( out ); -} - -VImage VImage::gaussmat( double sigma, double min_ampl, VOption *options ) -{ - VImage out; - - call( "gaussmat", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "sigma", sigma )-> - set( "min_ampl", min_ampl ) ); - - return( out ); -} - -VImage VImage::gaussnoise( int width, int height, VOption *options ) -{ - VImage out; - - call( "gaussnoise", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -std::vector<double> VImage::getpoint( int x, int y, VOption *options ) const -{ - std::vector<double> out_array; - - call( "getpoint", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out_array", &out_array )-> - set( "x", x )-> - set( "y", y ) ); - - return( out_array ); -} - -VImage VImage::gifload( const char *filename, VOption *options ) -{ - VImage out; - - call( "gifload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::gifload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "gifload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::gifload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "gifload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -VImage VImage::globalbalance( VOption *options ) const -{ - VImage out; - - call( "globalbalance", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::gravity( VipsCompassDirection direction, int width, int height, VOption *options ) const -{ - VImage out; - - call( "gravity", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "direction", direction )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::grey( int width, int height, VOption *options ) -{ - VImage out; - - call( "grey", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::grid( int tile_height, int across, int down, VOption *options ) const -{ - VImage out; - - call( "grid", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "tile_height", tile_height )-> - set( "across", across )-> - set( "down", down ) ); - - return( out ); -} - -VImage VImage::heifload( const char *filename, VOption *options ) -{ - VImage out; - - call( "heifload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::heifload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "heifload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::heifload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "heifload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::heifsave( const char *filename, VOption *options ) const -{ - call( "heifsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::heifsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "heifsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -void VImage::heifsave_target( VTarget target, VOption *options ) const -{ - call( "heifsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::hist_cum( VOption *options ) const -{ - VImage out; - - call( "hist_cum", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -double VImage::hist_entropy( VOption *options ) const -{ - double out; - - call( "hist_entropy", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::hist_equal( VOption *options ) const -{ - VImage out; - - call( "hist_equal", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::hist_find( VOption *options ) const -{ - VImage out; - - call( "hist_find", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::hist_find_indexed( VImage index, VOption *options ) const -{ - VImage out; - - call( "hist_find_indexed", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "index", index ) ); - - return( out ); -} - -VImage VImage::hist_find_ndim( VOption *options ) const -{ - VImage out; - - call( "hist_find_ndim", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -bool VImage::hist_ismonotonic( VOption *options ) const -{ - bool monotonic; - - call( "hist_ismonotonic", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "monotonic", &monotonic ) ); - - return( monotonic ); -} - -VImage VImage::hist_local( int width, int height, VOption *options ) const -{ - VImage out; - - call( "hist_local", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::hist_match( VImage ref, VOption *options ) const -{ - VImage out; - - call( "hist_match", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "ref", ref ) ); - - return( out ); -} - -VImage VImage::hist_norm( VOption *options ) const -{ - VImage out; - - call( "hist_norm", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::hist_plot( VOption *options ) const -{ - VImage out; - - call( "hist_plot", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::hough_circle( VOption *options ) const -{ - VImage out; - - call( "hough_circle", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::hough_line( VOption *options ) const -{ - VImage out; - - call( "hough_line", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::icc_export( VOption *options ) const -{ - VImage out; - - call( "icc_export", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::icc_import( VOption *options ) const -{ - VImage out; - - call( "icc_import", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::icc_transform( const char *output_profile, VOption *options ) const -{ - VImage out; - - call( "icc_transform", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "output_profile", output_profile ) ); - - return( out ); -} - -VImage VImage::identity( VOption *options ) -{ - VImage out; - - call( "identity", - (options ? options : VImage::option())-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::ifthenelse( VImage in1, VImage in2, VOption *options ) const -{ - VImage out; - - call( "ifthenelse", - (options ? options : VImage::option())-> - set( "cond", *this )-> - set( "out", &out )-> - set( "in1", in1 )-> - set( "in2", in2 ) ); - - return( out ); -} - -VImage VImage::insert( VImage sub, int x, int y, VOption *options ) const -{ - VImage out; - - call( "insert", - (options ? options : VImage::option())-> - set( "main", *this )-> - set( "out", &out )-> - set( "sub", sub )-> - set( "x", x )-> - set( "y", y ) ); - - return( out ); -} - -VImage VImage::invert( VOption *options ) const -{ - VImage out; - - call( "invert", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::invertlut( VOption *options ) const -{ - VImage out; - - call( "invertlut", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::invfft( VOption *options ) const -{ - VImage out; - - call( "invfft", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::join( VImage in2, VipsDirection direction, VOption *options ) const -{ - VImage out; - - call( "join", - (options ? options : VImage::option())-> - set( "in1", *this )-> - set( "out", &out )-> - set( "in2", in2 )-> - set( "direction", direction ) ); - - return( out ); -} - -VImage VImage::jpegload( const char *filename, VOption *options ) -{ - VImage out; - - call( "jpegload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::jpegload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "jpegload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::jpegload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "jpegload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::jpegsave( const char *filename, VOption *options ) const -{ - call( "jpegsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::jpegsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "jpegsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -void VImage::jpegsave_mime( VOption *options ) const -{ - call( "jpegsave_mime", - (options ? options : VImage::option())-> - set( "in", *this ) ); -} - -void VImage::jpegsave_target( VTarget target, VOption *options ) const -{ - call( "jpegsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::labelregions( VOption *options ) const -{ - VImage mask; - - call( "labelregions", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "mask", &mask ) ); - - return( mask ); -} - -VImage VImage::linear( std::vector<double> a, std::vector<double> b, VOption *options ) const -{ - VImage out; - - call( "linear", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "a", a )-> - set( "b", b ) ); - - return( out ); -} - -VImage VImage::linecache( VOption *options ) const -{ - VImage out; - - call( "linecache", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::logmat( double sigma, double min_ampl, VOption *options ) -{ - VImage out; - - call( "logmat", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "sigma", sigma )-> - set( "min_ampl", min_ampl ) ); - - return( out ); -} - -VImage VImage::magickload( const char *filename, VOption *options ) -{ - VImage out; - - call( "magickload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::magickload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "magickload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -void VImage::magicksave( const char *filename, VOption *options ) const -{ - call( "magicksave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::magicksave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "magicksave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -VImage VImage::mapim( VImage index, VOption *options ) const -{ - VImage out; - - call( "mapim", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "index", index ) ); - - return( out ); -} - -VImage VImage::maplut( VImage lut, VOption *options ) const -{ - VImage out; - - call( "maplut", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "lut", lut ) ); - - return( out ); -} - -VImage VImage::mask_butterworth( int width, int height, double order, double frequency_cutoff, double amplitude_cutoff, VOption *options ) -{ - VImage out; - - call( "mask_butterworth", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "order", order )-> - set( "frequency_cutoff", frequency_cutoff )-> - set( "amplitude_cutoff", amplitude_cutoff ) ); - - return( out ); -} - -VImage VImage::mask_butterworth_band( int width, int height, double order, double frequency_cutoff_x, double frequency_cutoff_y, double radius, double amplitude_cutoff, VOption *options ) -{ - VImage out; - - call( "mask_butterworth_band", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "order", order )-> - set( "frequency_cutoff_x", frequency_cutoff_x )-> - set( "frequency_cutoff_y", frequency_cutoff_y )-> - set( "radius", radius )-> - set( "amplitude_cutoff", amplitude_cutoff ) ); - - return( out ); -} - -VImage VImage::mask_butterworth_ring( int width, int height, double order, double frequency_cutoff, double amplitude_cutoff, double ringwidth, VOption *options ) -{ - VImage out; - - call( "mask_butterworth_ring", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "order", order )-> - set( "frequency_cutoff", frequency_cutoff )-> - set( "amplitude_cutoff", amplitude_cutoff )-> - set( "ringwidth", ringwidth ) ); - - return( out ); -} - -VImage VImage::mask_fractal( int width, int height, double fractal_dimension, VOption *options ) -{ - VImage out; - - call( "mask_fractal", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "fractal_dimension", fractal_dimension ) ); - - return( out ); -} - -VImage VImage::mask_gaussian( int width, int height, double frequency_cutoff, double amplitude_cutoff, VOption *options ) -{ - VImage out; - - call( "mask_gaussian", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "frequency_cutoff", frequency_cutoff )-> - set( "amplitude_cutoff", amplitude_cutoff ) ); - - return( out ); -} - -VImage VImage::mask_gaussian_band( int width, int height, double frequency_cutoff_x, double frequency_cutoff_y, double radius, double amplitude_cutoff, VOption *options ) -{ - VImage out; - - call( "mask_gaussian_band", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "frequency_cutoff_x", frequency_cutoff_x )-> - set( "frequency_cutoff_y", frequency_cutoff_y )-> - set( "radius", radius )-> - set( "amplitude_cutoff", amplitude_cutoff ) ); - - return( out ); -} - -VImage VImage::mask_gaussian_ring( int width, int height, double frequency_cutoff, double amplitude_cutoff, double ringwidth, VOption *options ) -{ - VImage out; - - call( "mask_gaussian_ring", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "frequency_cutoff", frequency_cutoff )-> - set( "amplitude_cutoff", amplitude_cutoff )-> - set( "ringwidth", ringwidth ) ); - - return( out ); -} - -VImage VImage::mask_ideal( int width, int height, double frequency_cutoff, VOption *options ) -{ - VImage out; - - call( "mask_ideal", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "frequency_cutoff", frequency_cutoff ) ); - - return( out ); -} - -VImage VImage::mask_ideal_band( int width, int height, double frequency_cutoff_x, double frequency_cutoff_y, double radius, VOption *options ) -{ - VImage out; - - call( "mask_ideal_band", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "frequency_cutoff_x", frequency_cutoff_x )-> - set( "frequency_cutoff_y", frequency_cutoff_y )-> - set( "radius", radius ) ); - - return( out ); -} - -VImage VImage::mask_ideal_ring( int width, int height, double frequency_cutoff, double ringwidth, VOption *options ) -{ - VImage out; - - call( "mask_ideal_ring", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "frequency_cutoff", frequency_cutoff )-> - set( "ringwidth", ringwidth ) ); - - return( out ); -} - -VImage VImage::match( VImage sec, int xr1, int yr1, int xs1, int ys1, int xr2, int yr2, int xs2, int ys2, VOption *options ) const -{ - VImage out; - - call( "match", - (options ? options : VImage::option())-> - set( "ref", *this )-> - set( "out", &out )-> - set( "sec", sec )-> - set( "xr1", xr1 )-> - set( "yr1", yr1 )-> - set( "xs1", xs1 )-> - set( "ys1", ys1 )-> - set( "xr2", xr2 )-> - set( "yr2", yr2 )-> - set( "xs2", xs2 )-> - set( "ys2", ys2 ) ); - - return( out ); -} - -VImage VImage::math( VipsOperationMath math, VOption *options ) const -{ - VImage out; - - call( "math", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "math", math ) ); - - return( out ); -} - -VImage VImage::math2( VImage right, VipsOperationMath2 math2, VOption *options ) const -{ - VImage out; - - call( "math2", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right )-> - set( "math2", math2 ) ); - - return( out ); -} - -VImage VImage::math2_const( VipsOperationMath2 math2, std::vector<double> c, VOption *options ) const -{ - VImage out; - - call( "math2_const", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "math2", math2 )-> - set( "c", c ) ); - - return( out ); -} - -VImage VImage::matload( const char *filename, VOption *options ) -{ - VImage out; - - call( "matload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::matrixinvert( VOption *options ) const -{ - VImage out; - - call( "matrixinvert", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::matrixload( const char *filename, VOption *options ) -{ - VImage out; - - call( "matrixload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::matrixload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "matrixload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::matrixprint( VOption *options ) const -{ - call( "matrixprint", - (options ? options : VImage::option())-> - set( "in", *this ) ); -} - -void VImage::matrixsave( const char *filename, VOption *options ) const -{ - call( "matrixsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -void VImage::matrixsave_target( VTarget target, VOption *options ) const -{ - call( "matrixsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -double VImage::max( VOption *options ) const -{ - double out; - - call( "max", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::measure( int h, int v, VOption *options ) const -{ - VImage out; - - call( "measure", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "h", h )-> - set( "v", v ) ); - - return( out ); -} - -VImage VImage::merge( VImage sec, VipsDirection direction, int dx, int dy, VOption *options ) const -{ - VImage out; - - call( "merge", - (options ? options : VImage::option())-> - set( "ref", *this )-> - set( "out", &out )-> - set( "sec", sec )-> - set( "direction", direction )-> - set( "dx", dx )-> - set( "dy", dy ) ); - - return( out ); -} - -double VImage::min( VOption *options ) const -{ - double out; - - call( "min", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::morph( VImage mask, VipsOperationMorphology morph, VOption *options ) const -{ - VImage out; - - call( "morph", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "mask", mask )-> - set( "morph", morph ) ); - - return( out ); -} - -VImage VImage::mosaic( VImage sec, VipsDirection direction, int xref, int yref, int xsec, int ysec, VOption *options ) const -{ - VImage out; - - call( "mosaic", - (options ? options : VImage::option())-> - set( "ref", *this )-> - set( "out", &out )-> - set( "sec", sec )-> - set( "direction", direction )-> - set( "xref", xref )-> - set( "yref", yref )-> - set( "xsec", xsec )-> - set( "ysec", ysec ) ); - - return( out ); -} - -VImage VImage::mosaic1( VImage sec, VipsDirection direction, int xr1, int yr1, int xs1, int ys1, int xr2, int yr2, int xs2, int ys2, VOption *options ) const -{ - VImage out; - - call( "mosaic1", - (options ? options : VImage::option())-> - set( "ref", *this )-> - set( "out", &out )-> - set( "sec", sec )-> - set( "direction", direction )-> - set( "xr1", xr1 )-> - set( "yr1", yr1 )-> - set( "xs1", xs1 )-> - set( "ys1", ys1 )-> - set( "xr2", xr2 )-> - set( "yr2", yr2 )-> - set( "xs2", xs2 )-> - set( "ys2", ys2 ) ); - - return( out ); -} - -VImage VImage::msb( VOption *options ) const -{ - VImage out; - - call( "msb", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::multiply( VImage right, VOption *options ) const -{ - VImage out; - - call( "multiply", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::niftiload( const char *filename, VOption *options ) -{ - VImage out; - - call( "niftiload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -void VImage::niftisave( const char *filename, VOption *options ) const -{ - call( "niftisave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VImage VImage::openexrload( const char *filename, VOption *options ) -{ - VImage out; - - call( "openexrload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::openslideload( const char *filename, VOption *options ) -{ - VImage out; - - call( "openslideload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::pdfload( const char *filename, VOption *options ) -{ - VImage out; - - call( "pdfload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::pdfload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "pdfload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::pdfload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "pdfload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -int VImage::percent( double percent, VOption *options ) const -{ - int threshold; - - call( "percent", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "threshold", &threshold )-> - set( "percent", percent ) ); - - return( threshold ); -} - -VImage VImage::perlin( int width, int height, VOption *options ) -{ - VImage out; - - call( "perlin", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::phasecor( VImage in2, VOption *options ) const -{ - VImage out; - - call( "phasecor", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "in2", in2 ) ); - - return( out ); -} - -VImage VImage::pngload( const char *filename, VOption *options ) -{ - VImage out; - - call( "pngload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::pngload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "pngload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::pngload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "pngload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::pngsave( const char *filename, VOption *options ) const -{ - call( "pngsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::pngsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "pngsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -void VImage::pngsave_target( VTarget target, VOption *options ) const -{ - call( "pngsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::ppmload( const char *filename, VOption *options ) -{ - VImage out; - - call( "ppmload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::ppmload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "ppmload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::ppmsave( const char *filename, VOption *options ) const -{ - call( "ppmsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -void VImage::ppmsave_target( VTarget target, VOption *options ) const -{ - call( "ppmsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::premultiply( VOption *options ) const -{ - VImage out; - - call( "premultiply", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::profile( VImage *rows, VOption *options ) const -{ - VImage columns; - - call( "profile", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "columns", &columns )-> - set( "rows", rows ) ); - - return( columns ); -} - -VipsBlob *VImage::profile_load( const char *name, VOption *options ) -{ - VipsBlob *profile; - - call( "profile_load", - (options ? options : VImage::option())-> - set( "profile", &profile )-> - set( "name", name ) ); - - return( profile ); -} - -VImage VImage::project( VImage *rows, VOption *options ) const -{ - VImage columns; - - call( "project", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "columns", &columns )-> - set( "rows", rows ) ); - - return( columns ); -} - -VImage VImage::quadratic( VImage coeff, VOption *options ) const -{ - VImage out; - - call( "quadratic", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "coeff", coeff ) ); - - return( out ); -} - -VImage VImage::rad2float( VOption *options ) const -{ - VImage out; - - call( "rad2float", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::radload( const char *filename, VOption *options ) -{ - VImage out; - - call( "radload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::radload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "radload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::radload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "radload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::radsave( const char *filename, VOption *options ) const -{ - call( "radsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::radsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "radsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -void VImage::radsave_target( VTarget target, VOption *options ) const -{ - call( "radsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::rank( int width, int height, int index, VOption *options ) const -{ - VImage out; - - call( "rank", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height )-> - set( "index", index ) ); - - return( out ); -} - -VImage VImage::rawload( const char *filename, int width, int height, int bands, VOption *options ) -{ - VImage out; - - call( "rawload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename )-> - set( "width", width )-> - set( "height", height )-> - set( "bands", bands ) ); - - return( out ); -} - -void VImage::rawsave( const char *filename, VOption *options ) const -{ - call( "rawsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -void VImage::rawsave_fd( int fd, VOption *options ) const -{ - call( "rawsave_fd", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "fd", fd ) ); -} - -VImage VImage::recomb( VImage m, VOption *options ) const -{ - VImage out; - - call( "recomb", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "m", m ) ); - - return( out ); -} - -VImage VImage::reduce( double hshrink, double vshrink, VOption *options ) const -{ - VImage out; - - call( "reduce", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "hshrink", hshrink )-> - set( "vshrink", vshrink ) ); - - return( out ); -} - -VImage VImage::reduceh( double hshrink, VOption *options ) const -{ - VImage out; - - call( "reduceh", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "hshrink", hshrink ) ); - - return( out ); -} - -VImage VImage::reducev( double vshrink, VOption *options ) const -{ - VImage out; - - call( "reducev", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "vshrink", vshrink ) ); - - return( out ); -} - -VImage VImage::relational( VImage right, VipsOperationRelational relational, VOption *options ) const -{ - VImage out; - - call( "relational", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right )-> - set( "relational", relational ) ); - - return( out ); -} - -VImage VImage::relational_const( VipsOperationRelational relational, std::vector<double> c, VOption *options ) const -{ - VImage out; - - call( "relational_const", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "relational", relational )-> - set( "c", c ) ); - - return( out ); -} - -VImage VImage::remainder( VImage right, VOption *options ) const -{ - VImage out; - - call( "remainder", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::remainder_const( std::vector<double> c, VOption *options ) const -{ - VImage out; - - call( "remainder_const", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "c", c ) ); - - return( out ); -} - -VImage VImage::replicate( int across, int down, VOption *options ) const -{ - VImage out; - - call( "replicate", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "across", across )-> - set( "down", down ) ); - - return( out ); -} - -VImage VImage::resize( double scale, VOption *options ) const -{ - VImage out; - - call( "resize", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "scale", scale ) ); - - return( out ); -} - -VImage VImage::rot( VipsAngle angle, VOption *options ) const -{ - VImage out; - - call( "rot", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "angle", angle ) ); - - return( out ); -} - -VImage VImage::rot45( VOption *options ) const -{ - VImage out; - - call( "rot45", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::rotate( double angle, VOption *options ) const -{ - VImage out; - - call( "rotate", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "angle", angle ) ); - - return( out ); -} - -VImage VImage::round( VipsOperationRound round, VOption *options ) const -{ - VImage out; - - call( "round", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "round", round ) ); - - return( out ); -} - -VImage VImage::sRGB2HSV( VOption *options ) const -{ - VImage out; - - call( "sRGB2HSV", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::sRGB2scRGB( VOption *options ) const -{ - VImage out; - - call( "sRGB2scRGB", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::scRGB2BW( VOption *options ) const -{ - VImage out; - - call( "scRGB2BW", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::scRGB2XYZ( VOption *options ) const -{ - VImage out; - - call( "scRGB2XYZ", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::scRGB2sRGB( VOption *options ) const -{ - VImage out; - - call( "scRGB2sRGB", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::scale( VOption *options ) const -{ - VImage out; - - call( "scale", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::sequential( VOption *options ) const -{ - VImage out; - - call( "sequential", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::sharpen( VOption *options ) const -{ - VImage out; - - call( "sharpen", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::shrink( double hshrink, double vshrink, VOption *options ) const -{ - VImage out; - - call( "shrink", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "hshrink", hshrink )-> - set( "vshrink", vshrink ) ); - - return( out ); -} - -VImage VImage::shrinkh( int hshrink, VOption *options ) const -{ - VImage out; - - call( "shrinkh", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "hshrink", hshrink ) ); - - return( out ); -} - -VImage VImage::shrinkv( int vshrink, VOption *options ) const -{ - VImage out; - - call( "shrinkv", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "vshrink", vshrink ) ); - - return( out ); -} - -VImage VImage::sign( VOption *options ) const -{ - VImage out; - - call( "sign", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::similarity( VOption *options ) const -{ - VImage out; - - call( "similarity", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::sines( int width, int height, VOption *options ) -{ - VImage out; - - call( "sines", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::smartcrop( int width, int height, VOption *options ) const -{ - VImage out; - - call( "smartcrop", - (options ? options : VImage::option())-> - set( "input", *this )-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::sobel( VOption *options ) const -{ - VImage out; - - call( "sobel", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::spcor( VImage ref, VOption *options ) const -{ - VImage out; - - call( "spcor", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "ref", ref ) ); - - return( out ); -} - -VImage VImage::spectrum( VOption *options ) const -{ - VImage out; - - call( "spectrum", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::stats( VOption *options ) const -{ - VImage out; - - call( "stats", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::stdif( int width, int height, VOption *options ) const -{ - VImage out; - - call( "stdif", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::subsample( int xfac, int yfac, VOption *options ) const -{ - VImage out; - - call( "subsample", - (options ? options : VImage::option())-> - set( "input", *this )-> - set( "out", &out )-> - set( "xfac", xfac )-> - set( "yfac", yfac ) ); - - return( out ); -} - -VImage VImage::subtract( VImage right, VOption *options ) const -{ - VImage out; - - call( "subtract", - (options ? options : VImage::option())-> - set( "left", *this )-> - set( "out", &out )-> - set( "right", right ) ); - - return( out ); -} - -VImage VImage::sum( std::vector<VImage> in, VOption *options ) -{ - VImage out; - - call( "sum", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "in", in ) ); - - return( out ); -} - -VImage VImage::svgload( const char *filename, VOption *options ) -{ - VImage out; - - call( "svgload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::svgload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "svgload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::svgload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "svgload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -VImage VImage::switch_image( std::vector<VImage> tests, VOption *options ) -{ - VImage out; - - call( "switch", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "tests", tests ) ); - - return( out ); -} - -void VImage::system( const char *cmd_format, VOption *options ) -{ - call( "system", - (options ? options : VImage::option())-> - set( "cmd_format", cmd_format ) ); -} - -VImage VImage::text( const char *text, VOption *options ) -{ - VImage out; - - call( "text", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "text", text ) ); - - return( out ); -} - -VImage VImage::thumbnail( const char *filename, int width, VOption *options ) -{ - VImage out; - - call( "thumbnail", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename )-> - set( "width", width ) ); - - return( out ); -} - -VImage VImage::thumbnail_buffer( VipsBlob *buffer, int width, VOption *options ) -{ - VImage out; - - call( "thumbnail_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer )-> - set( "width", width ) ); - - return( out ); -} - -VImage VImage::thumbnail_image( int width, VOption *options ) const -{ - VImage out; - - call( "thumbnail_image", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out )-> - set( "width", width ) ); - - return( out ); -} - -VImage VImage::thumbnail_source( VSource source, int width, VOption *options ) -{ - VImage out; - - call( "thumbnail_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source )-> - set( "width", width ) ); - - return( out ); -} - -VImage VImage::tiffload( const char *filename, VOption *options ) -{ - VImage out; - - call( "tiffload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::tiffload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "tiffload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::tiffload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "tiffload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::tiffsave( const char *filename, VOption *options ) const -{ - call( "tiffsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::tiffsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "tiffsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -VImage VImage::tilecache( VOption *options ) const -{ - VImage out; - - call( "tilecache", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::tonelut( VOption *options ) -{ - VImage out; - - call( "tonelut", - (options ? options : VImage::option())-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::transpose3d( VOption *options ) const -{ - VImage out; - - call( "transpose3d", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::unpremultiply( VOption *options ) const -{ - VImage out; - - call( "unpremultiply", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::vipsload( const char *filename, VOption *options ) -{ - VImage out; - - call( "vipsload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -void VImage::vipssave( const char *filename, VOption *options ) const -{ - call( "vipssave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VImage VImage::webpload( const char *filename, VOption *options ) -{ - VImage out; - - call( "webpload", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "filename", filename ) ); - - return( out ); -} - -VImage VImage::webpload_buffer( VipsBlob *buffer, VOption *options ) -{ - VImage out; - - call( "webpload_buffer", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "buffer", buffer ) ); - - return( out ); -} - -VImage VImage::webpload_source( VSource source, VOption *options ) -{ - VImage out; - - call( "webpload_source", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "source", source ) ); - - return( out ); -} - -void VImage::webpsave( const char *filename, VOption *options ) const -{ - call( "webpsave", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "filename", filename ) ); -} - -VipsBlob *VImage::webpsave_buffer( VOption *options ) const -{ - VipsBlob *buffer; - - call( "webpsave_buffer", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "buffer", &buffer ) ); - - return( buffer ); -} - -void VImage::webpsave_target( VTarget target, VOption *options ) const -{ - call( "webpsave_target", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "target", target ) ); -} - -VImage VImage::worley( int width, int height, VOption *options ) -{ - VImage out; - - call( "worley", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::wrap( VOption *options ) const -{ - VImage out; - - call( "wrap", - (options ? options : VImage::option())-> - set( "in", *this )-> - set( "out", &out ) ); - - return( out ); -} - -VImage VImage::xyz( int width, int height, VOption *options ) -{ - VImage out; - - call( "xyz", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::zone( int width, int height, VOption *options ) -{ - VImage out; - - call( "zone", - (options ? options : VImage::option())-> - set( "out", &out )-> - set( "width", width )-> - set( "height", height ) ); - - return( out ); -} - -VImage VImage::zoom( int xfac, int yfac, VOption *options ) const -{ - VImage out; - - call( "zoom", - (options ? options : VImage::option())-> - set( "input", *this )-> - set( "out", &out )-> - set( "xfac", xfac )-> - set( "yfac", yfac ) ); - - return( out ); -} diff --git a/src/metadata.cc b/src/metadata.cc index be0f2f8fd..2fde7bf6a 100644 --- a/src/metadata.cc +++ b/src/metadata.cc @@ -1,25 +1,21 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ +#include <cmath> #include <numeric> +#include <string> +#include <utility> #include <vector> #include <napi.h> #include <vips/vips8> -#include "common.h" -#include "metadata.h" +#include "./common.h" +#include "./metadata.h" + +static void* readPNGComment(VipsImage *image, const char *field, GValue *value, void *p); class MetadataWorker : public Napi::AsyncWorker { public: @@ -29,7 +25,7 @@ class MetadataWorker : public Napi::AsyncWorker { void Execute() { // Decrement queued task counter - g_atomic_int_dec_and_test(&sharp::counterQueue); + sharp::counterQueue--; vips::VImage image; sharp::ImageType imageType = sharp::ImageType::UNKNOWN; @@ -56,8 +52,11 @@ class MetadataWorker : public Napi::AsyncWorker { if (image.get_typeof("interlaced") == G_TYPE_INT) { baton->isProgressive = image.get_int("interlaced") == 1; } - if (image.get_typeof("palette-bit-depth") == G_TYPE_INT) { - baton->paletteBitDepth = image.get_int("palette-bit-depth"); + if (image.get_typeof(VIPS_META_PALETTE) == G_TYPE_INT) { + baton->isPalette = image.get_int(VIPS_META_PALETTE); + } + if (image.get_typeof(VIPS_META_BITS_PER_SAMPLE) == G_TYPE_INT) { + baton->bitsPerSample = image.get_int(VIPS_META_BITS_PER_SAMPLE); } if (image.get_typeof(VIPS_META_N_PAGES) == G_TYPE_INT) { baton->pages = image.get_int(VIPS_META_N_PAGES); @@ -77,6 +76,12 @@ class MetadataWorker : public Napi::AsyncWorker { if (image.get_typeof("heif-compression") == VIPS_TYPE_REF_STRING) { baton->compression = image.get_string("heif-compression"); } + if (image.get_typeof(VIPS_META_RESOLUTION_UNIT) == VIPS_TYPE_REF_STRING) { + baton->resolutionUnit = image.get_string(VIPS_META_RESOLUTION_UNIT); + } + if (image.get_typeof("magick-format") == VIPS_TYPE_REF_STRING) { + baton->formatMagick = image.get_string("magick-format"); + } 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++) { @@ -86,9 +91,15 @@ class MetadataWorker : public Napi::AsyncWorker { baton->levels.push_back(std::pair<int, int>(width, height)); } } + if (image.get_typeof(VIPS_META_N_SUBIFDS) == G_TYPE_INT) { + baton->subifds = image.get_int(VIPS_META_N_SUBIFDS); + } baton->hasProfile = sharp::HasProfile(image); + if (image.get_typeof("background") == VIPS_TYPE_ARRAY_DOUBLE) { + baton->background = image.get_array_double("background"); + } // Derived attributes - baton->hasAlpha = sharp::HasAlpha(image); + baton->hasAlpha = image.has_alpha(); baton->orientation = sharp::ExifOrientation(image); // EXIF if (image.get_typeof(VIPS_META_EXIF_NAME) == VIPS_TYPE_BLOB) { @@ -130,6 +141,8 @@ class MetadataWorker : public Napi::AsyncWorker { memcpy(baton->tifftagPhotoshop, tifftagPhotoshop, tifftagPhotoshopLength); baton->tifftagPhotoshopLength = tifftagPhotoshopLength; } + // PNG comments + vips_image_map(image.get_image(), readPNGComment, &baton->comments); } // Clean up @@ -144,7 +157,7 @@ class MetadataWorker : public Napi::AsyncWorker { // Handle warnings std::string warning = sharp::VipsWarningPop(); while (!warning.empty()) { - debuglog.Call({ Napi::String::New(env, warning) }); + debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) }); warning = sharp::VipsWarningPop(); } @@ -166,8 +179,13 @@ class MetadataWorker : public Napi::AsyncWorker { info.Set("chromaSubsampling", baton->chromaSubsampling); } info.Set("isProgressive", baton->isProgressive); - if (baton->paletteBitDepth > 0) { - info.Set("paletteBitDepth", baton->paletteBitDepth); + info.Set("isPalette", baton->isPalette); + if (baton->bitsPerSample > 0) { + info.Set("bitsPerSample", baton->bitsPerSample); + if (baton->isPalette) { + // Deprecated, remove with libvips 8.17.0 + info.Set("paletteBitDepth", baton->bitsPerSample); + } } if (baton->pages > 0) { info.Set("pages", baton->pages); @@ -192,41 +210,86 @@ class MetadataWorker : public Napi::AsyncWorker { if (!baton->compression.empty()) { info.Set("compression", baton->compression); } + if (!baton->resolutionUnit.empty()) { + info.Set("resolutionUnit", baton->resolutionUnit == "in" ? "inch" : baton->resolutionUnit); + } + if (!baton->formatMagick.empty()) { + info.Set("formatMagick", baton->formatMagick); + } if (!baton->levels.empty()) { int i = 0; Napi::Array levels = Napi::Array::New(env, static_cast<size_t>(baton->levels.size())); - for (std::pair<int, int> const &l : baton->levels) { + for (const auto& [width, height] : baton->levels) { Napi::Object level = Napi::Object::New(env); - level.Set("width", l.first); - level.Set("height", l.second); + level.Set("width", width); + level.Set("height", height); levels.Set(i++, level); } info.Set("levels", levels); } + if (baton->subifds > 0) { + info.Set("subifds", baton->subifds); + } + if (!baton->background.empty()) { + Napi::Object background = Napi::Object::New(env); + if (baton->background.size() == 3) { + background.Set("r", baton->background[0]); + background.Set("g", baton->background[1]); + background.Set("b", baton->background[2]); + } else { + background.Set("gray", round(baton->background[0] * 100 / 255)); + } + info.Set("background", background); + } info.Set("hasProfile", baton->hasProfile); info.Set("hasAlpha", baton->hasAlpha); if (baton->orientation > 0) { info.Set("orientation", baton->orientation); } + Napi::Object autoOrient = Napi::Object::New(env); + info.Set("autoOrient", autoOrient); + if (baton->orientation >= 5) { + autoOrient.Set("width", baton->height); + autoOrient.Set("height", baton->width); + } else { + autoOrient.Set("width", baton->width); + autoOrient.Set("height", baton->height); + } if (baton->exifLength > 0) { - info.Set("exif", Napi::Buffer<char>::New(env, baton->exif, baton->exifLength, sharp::FreeCallback)); + info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback)); } if (baton->iccLength > 0) { - info.Set("icc", Napi::Buffer<char>::New(env, baton->icc, baton->iccLength, sharp::FreeCallback)); + info.Set("icc", Napi::Buffer<char>::NewOrCopy(env, baton->icc, baton->iccLength, sharp::FreeCallback)); } if (baton->iptcLength > 0) { - info.Set("iptc", Napi::Buffer<char>::New(env, baton->iptc, baton->iptcLength, sharp::FreeCallback)); + info.Set("iptc", Napi::Buffer<char>::NewOrCopy(env, baton->iptc, baton->iptcLength, sharp::FreeCallback)); } if (baton->xmpLength > 0) { - info.Set("xmp", Napi::Buffer<char>::New(env, baton->xmp, baton->xmpLength, sharp::FreeCallback)); + if (g_utf8_validate(static_cast<char const *>(baton->xmp), baton->xmpLength, nullptr)) { + info.Set("xmpAsString", + Napi::String::New(env, static_cast<char const *>(baton->xmp), baton->xmpLength)); + } + info.Set("xmp", Napi::Buffer<char>::NewOrCopy(env, baton->xmp, baton->xmpLength, sharp::FreeCallback)); } if (baton->tifftagPhotoshopLength > 0) { info.Set("tifftagPhotoshop", - Napi::Buffer<char>::New(env, baton->tifftagPhotoshop, baton->tifftagPhotoshopLength, sharp::FreeCallback)); + Napi::Buffer<char>::NewOrCopy(env, baton->tifftagPhotoshop, + baton->tifftagPhotoshopLength, sharp::FreeCallback)); + } + if (baton->comments.size() > 0) { + int i = 0; + Napi::Array comments = Napi::Array::New(env, baton->comments.size()); + for (const auto& [keyword, text] : baton->comments) { + Napi::Object comment = Napi::Object::New(env); + comment.Set("keyword", keyword); + comment.Set("text", text); + comments.Set(i++, comment); + } + info.Set("comments", comments); } - Callback().MakeCallback(Receiver().Value(), { env.Null(), info }); + Callback().Call(Receiver().Value(), { env.Null(), info }); } else { - Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, baton->err).Value() }); + Callback().Call(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() }); } delete baton->input; @@ -244,7 +307,7 @@ class MetadataWorker : public Napi::AsyncWorker { Napi::Value metadata(const Napi::CallbackInfo& info) { // V8 objects are converted to non-V8 types held in the baton struct MetadataBaton *baton = new MetadataBaton; - Napi::Object options = info[0].As<Napi::Object>(); + Napi::Object options = info[size_t(0)].As<Napi::Object>(); // Input baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>()); @@ -253,13 +316,31 @@ Napi::Value metadata(const Napi::CallbackInfo& info) { Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>(); // Join queue for worker thread - Napi::Function callback = info[1].As<Napi::Function>(); + Napi::Function callback = info[size_t(1)].As<Napi::Function>(); MetadataWorker *worker = new MetadataWorker(callback, baton, debuglog); worker->Receiver().Set("options", options); worker->Queue(); // Increment queued task counter - g_atomic_int_inc(&sharp::counterQueue); + sharp::counterQueue++; return info.Env().Undefined(); } + +const char *PNG_COMMENT_START = "png-comment-"; +const int PNG_COMMENT_START_LEN = strlen(PNG_COMMENT_START); + +static void* readPNGComment(VipsImage *image, const char *field, GValue *value, void *p) { + MetadataComments *comments = static_cast<MetadataComments *>(p); + + if (vips_isprefix(PNG_COMMENT_START, field)) { + const char *keyword = strchr(field + PNG_COMMENT_START_LEN, '-'); + const char *str; + if (keyword != NULL && !vips_image_get_string(image, field, &str)) { + keyword++; // Skip the hyphen + comments->push_back(std::make_pair(keyword, str)); + } + } + + return NULL; +} diff --git a/src/metadata.h b/src/metadata.h index da9fc80cf..6f02d452c 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -1,25 +1,19 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #ifndef SRC_METADATA_H_ #define SRC_METADATA_H_ #include <string> +#include <vector> #include <napi.h> #include "./common.h" +typedef std::vector<std::pair<std::string, std::string>> MetadataComments; + struct MetadataBaton { // Input sharp::InputDescriptor *input; @@ -33,14 +27,19 @@ struct MetadataBaton { int density; std::string chromaSubsampling; bool isProgressive; - int paletteBitDepth; + bool isPalette; + int bitsPerSample; int pages; int pageHeight; int loop; std::vector<int> delay; int pagePrimary; std::string compression; + std::string resolutionUnit; + std::string formatMagick; std::vector<std::pair<int, int>> levels; + int subifds; + std::vector<double> background; bool hasProfile; bool hasAlpha; int orientation; @@ -54,6 +53,7 @@ struct MetadataBaton { size_t xmpLength; char *tifftagPhotoshop; size_t tifftagPhotoshopLength; + MetadataComments comments; std::string err; MetadataBaton(): @@ -63,11 +63,13 @@ struct MetadataBaton { channels(0), density(0), isProgressive(false), - paletteBitDepth(0), + isPalette(false), + bitsPerSample(0), pages(0), pageHeight(0), loop(-1), pagePrimary(-1), + subifds(0), hasProfile(false), hasAlpha(false), orientation(0), diff --git a/src/operations.cc b/src/operations.cc index 4cf56d4a3..daeba5ab4 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -1,63 +1,67 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #include <algorithm> #include <functional> #include <memory> #include <tuple> #include <vector> - #include <vips/vips8> -#include "common.h" -#include "operations.h" +#include "./common.h" +#include "./operations.h" using vips::VImage; using vips::VError; namespace sharp { /* - * Tint an image using the specified chroma, preserving the original image luminance + * Tint an image using the provided RGB. */ - VImage Tint(VImage image, double const a, double const b) { - // Get original colourspace + VImage Tint(VImage image, std::vector<double> const tint) { + std::vector<double> const tintLab = (VImage::black(1, 1) + tint) + .colourspace(VIPS_INTERPRETATION_LAB, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB)) + .getpoint(0, 0); + // LAB identity function + VImage identityLab = VImage::identity(VImage::option()->set("bands", 3)) + .colourspace(VIPS_INTERPRETATION_LAB, VImage::option()->set("source_space", VIPS_INTERPRETATION_sRGB)); + // Scale luminance range, 0.0 to 1.0 + VImage l = identityLab[0] / 100; + // Weighting functions + VImage weightL = 1.0 - 4.0 * ((l - 0.5) * (l - 0.5)); + VImage weightAB = (weightL * tintLab).extract_band(1, VImage::option()->set("n", 2)); + identityLab = identityLab[0].bandjoin(weightAB); + // Convert lookup table to sRGB + VImage lut = identityLab.colourspace(VIPS_INTERPRETATION_sRGB, + VImage::option()->set("source_space", VIPS_INTERPRETATION_LAB)); + // Original colourspace VipsInterpretation typeBeforeTint = image.interpretation(); if (typeBeforeTint == VIPS_INTERPRETATION_RGB) { typeBeforeTint = VIPS_INTERPRETATION_sRGB; } - // Extract luminance - VImage luminance = image.colourspace(VIPS_INTERPRETATION_LAB)[0]; - // Create the tinted version by combining the L from the original and the chroma from the tint - std::vector<double> chroma {a, b}; - VImage tinted = luminance - .bandjoin(chroma) - .copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_LAB)) - .colourspace(typeBeforeTint); - // Attach original alpha channel, if any - if (HasAlpha(image)) { - // Extract original alpha channel + // Apply lookup table + if (image.has_alpha()) { VImage alpha = image[image.bands() - 1]; - // Join alpha channel to normalised image - tinted = tinted.bandjoin(alpha); + image = RemoveAlpha(image) + .colourspace(VIPS_INTERPRETATION_B_W) + .maplut(lut) + .colourspace(typeBeforeTint) + .bandjoin(alpha); + } else { + image = image + .colourspace(VIPS_INTERPRETATION_B_W) + .maplut(lut) + .colourspace(typeBeforeTint); } - return tinted; + return image; } /* * Stretch luminance to cover full dynamic range. */ - VImage Normalise(VImage image) { + VImage Normalise(VImage image, int const lower, int const upper) { // Get original colourspace VipsInterpretation typeBeforeNormalize = image.interpretation(); if (typeBeforeNormalize == VIPS_INTERPRETATION_RGB) { @@ -67,11 +71,12 @@ namespace sharp { VImage lab = image.colourspace(VIPS_INTERPRETATION_LAB); // Extract luminance VImage luminance = lab[0]; + // Find luminance range - VImage stats = luminance.stats(); - double min = stats(0, 0)[0]; - double max = stats(1, 0)[0]; - if (min != max) { + int const min = lower == 0 ? luminance.min() : luminance.percent(lower); + int const max = upper == 100 ? luminance.max() : luminance.percent(upper); + + if (std::abs(max - min) > 1) { // Extract chroma VImage chroma = lab.extract_band(1, VImage::option()->set("n", 2)); // Calculate multiplication factor and addition @@ -80,7 +85,7 @@ namespace sharp { // Scale luminance, join to chroma, convert back to original colourspace VImage normalized = luminance.linear(f, a).bandjoin(chroma).colourspace(typeBeforeNormalize); // Attach original alpha channel, if any - if (HasAlpha(image)) { + if (image.has_alpha()) { // Extract original alpha channel VImage alpha = image[image.bands() - 1]; // Join alpha channel to normalised image @@ -92,11 +97,18 @@ namespace sharp { return image; } + /* + * Contrast limiting adapative histogram equalization (CLAHE) + */ + VImage Clahe(VImage image, int const width, int const height, int const maxSlope) { + return image.hist_local(width, height, VImage::option()->set("max_slope", maxSlope)); + } + /* * Gamma encoding/decoding */ VImage Gamma(VImage image, double const exponent) { - if (HasAlpha(image)) { + if (image.has_alpha()) { // Separate alpha channel VImage alpha = image[image.bands() - 1]; return RemoveAlpha(image).gamma(VImage::option()->set("exponent", exponent)).bandjoin(alpha); @@ -105,10 +117,36 @@ namespace sharp { } } + /* + * Flatten image to remove alpha channel + */ + VImage Flatten(VImage image, std::vector<double> flattenBackground) { + double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0; + std::vector<double> background { + flattenBackground[0] * multiplier, + flattenBackground[1] * multiplier, + flattenBackground[2] * multiplier + }; + return image.flatten(VImage::option()->set("background", background)); + } + + /** + * Produce the "negative" of the image. + */ + VImage Negate(VImage image, bool const negateAlpha) { + if (image.has_alpha() && !negateAlpha) { + // Separate alpha channel + VImage alpha = image[image.bands() - 1]; + return RemoveAlpha(image).invert().bandjoin(alpha); + } else { + return image.invert(); + } + } + /* * Gaussian blur. Use sigma of -1.0 for fast blur. */ - VImage Blur(VImage image, double const sigma) { + VImage Blur(VImage image, double const sigma, VipsPrecision precision, double const minAmpl) { if (sigma == -1.0) { // Fast, mild blur - averages neighbouring pixels VImage blur = VImage::new_matrixv(3, 3, @@ -119,7 +157,9 @@ namespace sharp { return image.conv(blur); } else { // Slower, accurate Gaussian blur - return image.gaussblur(sigma); + return StaySequential(image).gaussblur(sigma, VImage::option() + ->set("precision", precision) + ->set("min_ampl", minAmpl)); } } @@ -128,10 +168,10 @@ namespace sharp { */ VImage Convolve(VImage image, int const width, int const height, double const scale, double const offset, - std::unique_ptr<double[]> const &kernel_v + std::vector<double> const &kernel_v ) { VImage kernel = VImage::new_from_memory( - kernel_v.get(), + static_cast<void*>(const_cast<double*>(kernel_v.data())), width * height * sizeof(double), width, height, @@ -147,48 +187,53 @@ namespace sharp { * Recomb with a Matrix of the given bands/channel size. * Eg. RGB will be a 3x3 matrix. */ - VImage Recomb(VImage image, std::unique_ptr<double[]> const &matrix) { - double *m = matrix.get(); + VImage Recomb(VImage image, std::vector<double> const& matrix) { + double* m = const_cast<double*>(matrix.data()); image = image.colourspace(VIPS_INTERPRETATION_sRGB); - return image - .recomb(image.bands() == 3 - ? VImage::new_from_memory( - m, 9 * sizeof(double), 3, 3, 1, VIPS_FORMAT_DOUBLE - ) - : VImage::new_matrixv(4, 4, - m[0], m[1], m[2], 0.0, - m[3], m[4], m[5], 0.0, - m[6], m[7], m[8], 0.0, - 0.0, 0.0, 0.0, 1.0)); + if (matrix.size() == 9) { + return image + .recomb(image.bands() == 3 + ? VImage::new_matrix(3, 3, m, 9) + : VImage::new_matrixv(4, 4, + m[0], m[1], m[2], 0.0, + m[3], m[4], m[5], 0.0, + m[6], m[7], m[8], 0.0, + 0.0, 0.0, 0.0, 1.0)); + } else { + return image.recomb(VImage::new_matrix(4, 4, m, 16)); + } } - VImage Modulate(VImage image, double const brightness, double const saturation, int const hue) { - if (HasAlpha(image)) { + VImage Modulate(VImage image, double const brightness, double const saturation, + int const hue, double const lightness) { + VipsInterpretation colourspaceBeforeModulate = image.interpretation(); + if (image.has_alpha()) { // Separate alpha channel VImage alpha = image[image.bands() - 1]; return RemoveAlpha(image) .colourspace(VIPS_INTERPRETATION_LCH) .linear( { brightness, saturation, 1}, - { 0.0, 0.0, static_cast<double>(hue) } + { lightness, 0.0, static_cast<double>(hue) } ) - .colourspace(VIPS_INTERPRETATION_sRGB) + .colourspace(colourspaceBeforeModulate) .bandjoin(alpha); } else { return image .colourspace(VIPS_INTERPRETATION_LCH) .linear( { brightness, saturation, 1 }, - { 0.0, 0.0, static_cast<double>(hue) } + { lightness, 0.0, static_cast<double>(hue) } ) - .colourspace(VIPS_INTERPRETATION_sRGB); + .colourspace(colourspaceBeforeModulate); } } /* * Sharpen flat and jagged areas. Use sigma of -1.0 for fast sharpen. */ - VImage Sharpen(VImage image, double const sigma, double const flat, double const jagged) { + VImage Sharpen(VImage image, double const sigma, double const m1, double const m2, + double const x1, double const y2, double const y3) { if (sigma == -1.0) { // Fast, mild sharpen VImage sharpen = VImage::new_matrixv(3, 3, @@ -203,8 +248,14 @@ namespace sharp { if (colourspaceBeforeSharpen == VIPS_INTERPRETATION_RGB) { colourspaceBeforeSharpen = VIPS_INTERPRETATION_sRGB; } - return image.sharpen( - VImage::option()->set("sigma", sigma)->set("m1", flat)->set("m2", jagged)) + return image + .sharpen(VImage::option() + ->set("sigma", sigma) + ->set("m1", m1) + ->set("m2", m2) + ->set("x1", x1) + ->set("y2", y2) + ->set("y3", y3)) .colourspace(colourspaceBeforeSharpen); } } @@ -234,45 +285,215 @@ namespace sharp { /* Trim an image */ - VImage Trim(VImage image, double const threshold) { + VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt) { if (image.width() < 3 && image.height() < 3) { throw VError("Image to trim must be at least 3x3 pixels"); } - // Top-left pixel provides the background colour - VImage background = image.extract_area(0, 0, 1, 1); - if (HasAlpha(background)) { - background = background.flatten(); + if (background.size() == 0) { + // Top-left pixel provides the default background colour if none is given + background = image.extract_area(0, 0, 1, 1)(0, 0); + } else if (sharp::Is16Bit(image.interpretation())) { + for (size_t i = 0; i < background.size(); i++) { + background[i] *= 256.0; + } + threshold *= 256.0; + } + std::vector<double> backgroundAlpha({ background.back() }); + if (image.has_alpha()) { + background.pop_back(); + } else { + background.resize(image.bands()); } int left, top, width, height; left = image.find_trim(&top, &width, &height, VImage::option() - ->set("background", background(0, 0)) + ->set("background", background) + ->set("line_art", lineArt) ->set("threshold", threshold)); - if (width == 0 || height == 0) { - if (HasAlpha(image)) { - // Search alpha channel - VImage alpha = image[image.bands() - 1]; - VImage backgroundAlpha = alpha.extract_area(0, 0, 1, 1); - left = alpha.find_trim(&top, &width, &height, VImage::option() - ->set("background", backgroundAlpha(0, 0)) - ->set("threshold", threshold)); - } - if (width == 0 || height == 0) { - throw VError("Unexpected error while trimming. Try to lower the tolerance"); + if (image.has_alpha()) { + // Search alpha channel (A) + int leftA, topA, widthA, heightA; + VImage alpha = image[image.bands() - 1]; + leftA = alpha.find_trim(&topA, &widthA, &heightA, VImage::option() + ->set("background", backgroundAlpha) + ->set("line_art", lineArt) + ->set("threshold", threshold)); + if (widthA > 0 && heightA > 0) { + if (width > 0 && height > 0) { + // Combined bounding box (B) + int const leftB = std::min(left, leftA); + int const topB = std::min(top, topA); + int const widthB = std::max(left + width, leftA + widthA) - leftB; + int const heightB = std::max(top + height, topA + heightA) - topB; + return image.extract_area(leftB, topB, widthB, heightB); + } else { + // Use alpha only + return image.extract_area(leftA, topA, widthA, heightA); + } } } - return image.extract_area(left, top, width, height); + if (width > 0 && height > 0) { + return image.extract_area(left, top, width, height); + } + return image; } /* * Calculate (a * in + b) */ - VImage Linear(VImage image, double const a, double const b) { - if (HasAlpha(image)) { + VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) { + size_t const bands = static_cast<size_t>(image.bands()); + if (a.size() > bands) { + throw VError("Band expansion using linear is unsupported"); + } + bool const uchar = !Is16Bit(image.interpretation()); + if (image.has_alpha() && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) { // Separate alpha channel + VImage alpha = image[bands - 1]; + return RemoveAlpha(image).linear(a, b, VImage::option()->set("uchar", uchar)).bandjoin(alpha); + } else { + return image.linear(a, b, VImage::option()->set("uchar", uchar)); + } + } + + /* + * Unflatten + */ + VImage Unflatten(VImage image) { + if (image.has_alpha()) { VImage alpha = image[image.bands() - 1]; - return RemoveAlpha(image).linear(a, b).bandjoin(alpha); + VImage noAlpha = RemoveAlpha(image); + return noAlpha.bandjoin(alpha & (noAlpha.colourspace(VIPS_INTERPRETATION_B_W) < 255)); } else { - return image.linear(a, b); + return image.bandjoin(image.colourspace(VIPS_INTERPRETATION_B_W) < 255); } } + + /* + * Ensure the image is in a given colourspace + */ + VImage EnsureColourspace(VImage image, VipsInterpretation colourspace) { + if (colourspace != VIPS_INTERPRETATION_LAST && image.interpretation() != colourspace) { + image = image.colourspace(colourspace, + VImage::option()->set("source_space", image.interpretation())); + } + return image; + } + + /* + * Split and crop each frame, reassemble, and update pageHeight. + */ + VImage CropMultiPage(VImage image, int left, int top, int width, int height, + int nPages, int *pageHeight) { + if (top == 0 && height == *pageHeight) { + // Fast path; no need to adjust the height of the multi-page image + return image.extract_area(left, 0, width, image.height()); + } else { + std::vector<VImage> pages; + pages.reserve(nPages); + + // Split the image into cropped frames + image = StaySequential(image); + for (int i = 0; i < nPages; i++) { + pages.push_back( + image.extract_area(left, *pageHeight * i + top, width, height)); + } + + // Reassemble the frames into a tall, thin image + VImage assembled = VImage::arrayjoin(pages, + VImage::option()->set("across", 1)); + + // Update the page height + *pageHeight = height; + + return assembled; + } + } + + /* + * Split into frames, embed each frame, reassemble, and update pageHeight. + */ + VImage EmbedMultiPage(VImage image, int left, int top, int width, int height, + VipsExtend extendWith, std::vector<double> background, int nPages, int *pageHeight) { + if (top == 0 && height == *pageHeight) { + // Fast path; no need to adjust the height of the multi-page image + return image.embed(left, 0, width, image.height(), VImage::option() + ->set("extend", extendWith) + ->set("background", background)); + } else if (left == 0 && width == image.width()) { + // Fast path; no need to adjust the width of the multi-page image + std::vector<VImage> pages; + pages.reserve(nPages); + + // Rearrange the tall image into a vertical grid + image = image.grid(*pageHeight, nPages, 1); + + // Do the embed on the wide image + image = image.embed(0, top, image.width(), height, VImage::option() + ->set("extend", extendWith) + ->set("background", background)); + + // Split the wide image into frames + for (int i = 0; i < nPages; i++) { + pages.push_back( + image.extract_area(width * i, 0, width, height)); + } + + // Reassemble the frames into a tall, thin image + VImage assembled = VImage::arrayjoin(pages, + VImage::option()->set("across", 1)); + + // Update the page height + *pageHeight = height; + + return assembled; + } else { + std::vector<VImage> pages; + pages.reserve(nPages); + + // Split the image into frames + for (int i = 0; i < nPages; i++) { + pages.push_back( + image.extract_area(0, *pageHeight * i, image.width(), *pageHeight)); + } + + // Embed each frame in the target size + for (int i = 0; i < nPages; i++) { + pages[i] = pages[i].embed(left, top, width, height, VImage::option() + ->set("extend", extendWith) + ->set("background", background)); + } + + // Reassemble the frames into a tall, thin image + VImage assembled = VImage::arrayjoin(pages, + VImage::option()->set("across", 1)); + + // Update the page height + *pageHeight = height; + + return assembled; + } + } + + /* + * Dilate an image + */ + VImage Dilate(VImage image, int const width) { + int const maskWidth = 2 * width + 1; + VImage mask = VImage::new_matrix(maskWidth, maskWidth); + return image.morph( + mask, + VIPS_OPERATION_MORPHOLOGY_DILATE).invert(); + } + + /* + * Erode an image + */ + VImage Erode(VImage image, int const width) { + int const maskWidth = 2 * width + 1; + VImage mask = VImage::new_matrix(maskWidth, maskWidth); + return image.morph( + mask, + VIPS_OPERATION_MORPHOLOGY_ERODE).invert(); + } + } // namespace sharp diff --git a/src/operations.h b/src/operations.h index 6659ff8c8..c281c02cd 100644 --- a/src/operations.h +++ b/src/operations.h @@ -1,16 +1,7 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #ifndef SRC_OPERATIONS_H_ #define SRC_OPERATIONS_H_ @@ -19,6 +10,7 @@ #include <functional> #include <memory> #include <tuple> +#include <vector> #include <vips/vips8> using vips::VImage; @@ -26,35 +18,51 @@ using vips::VImage; namespace sharp { /* - * Tint an image using the specified chroma, preserving the original image luminance + * Tint an image using the provided RGB. */ - VImage Tint(VImage image, double const a, double const b); + VImage Tint(VImage image, std::vector<double> const tint); /* * Stretch luminance to cover full dynamic range. */ - VImage Normalise(VImage image); + VImage Normalise(VImage image, int const lower, int const upper); + + /* + * Contrast limiting adapative histogram equalization (CLAHE) + */ + VImage Clahe(VImage image, int const width, int const height, int const maxSlope); /* * Gamma encoding/decoding */ VImage Gamma(VImage image, double const exponent); + /* + * Flatten image to remove alpha channel + */ + VImage Flatten(VImage image, std::vector<double> flattenBackground); + + /* + * Produce the "negative" of the image. + */ + VImage Negate(VImage image, bool const negateAlpha); + /* * Gaussian blur. Use sigma of -1.0 for fast blur. */ - VImage Blur(VImage image, double const sigma); + VImage Blur(VImage image, double const sigma, VipsPrecision precision, double const minAmpl); /* * Convolution with a kernel. */ VImage Convolve(VImage image, int const width, int const height, - double const scale, double const offset, std::unique_ptr<double[]> const &kernel_v); + double const scale, double const offset, std::vector<double> const &kernel_v); /* * Sharpen flat and jagged areas. Use sigma of -1.0 for fast sharpen. */ - VImage Sharpen(VImage image, double const sigma, double const flat, double const jagged); + VImage Sharpen(VImage image, double const sigma, double const m1, double const m2, + double const x1, double const y2, double const y3); /* Threshold an image @@ -74,24 +82,56 @@ namespace sharp { /* Trim an image */ - VImage Trim(VImage image, double const threshold); + VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt); /* * Linear adjustment (a * in + b) */ - VImage Linear(VImage image, double const a, double const b); + VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b); + + /* + * Unflatten + */ + VImage Unflatten(VImage image); /* * Recomb with a Matrix of the given bands/channel size. * Eg. RGB will be a 3x3 matrix. */ - VImage Recomb(VImage image, std::unique_ptr<double[]> const &matrix); + VImage Recomb(VImage image, std::vector<double> const &matrix); + + /* + * Modulate brightness, saturation, hue and lightness + */ + VImage Modulate(VImage image, double const brightness, double const saturation, + int const hue, double const lightness); /* - * Modulate brightness, saturation and hue + * Ensure the image is in a given colourspace */ - VImage Modulate(VImage image, double const brightness, double const saturation, int const hue); + VImage EnsureColourspace(VImage image, VipsInterpretation colourspace); + /* + * Split and crop each frame, reassemble, and update pageHeight. + */ + VImage CropMultiPage(VImage image, int left, int top, int width, int height, + int nPages, int *pageHeight); + + /* + * Split into frames, embed each frame, reassemble, and update pageHeight. + */ + VImage EmbedMultiPage(VImage image, int left, int top, int width, int height, + VipsExtend extendWith, std::vector<double> background, int nPages, int *pageHeight); + + /* + * Dilate an image + */ + VImage Dilate(VImage image, int const maskWidth); + + /* + * Erode an image + */ + VImage Erode(VImage image, int const maskWidth); } // namespace sharp #endif // SRC_OPERATIONS_H_ diff --git a/src/pipeline.cc b/src/pipeline.cc index 285cbfe29..5f0a3bb0e 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -1,19 +1,11 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #include <algorithm> #include <cmath> +#include <filesystem> // NOLINT(build/c++17) #include <map> #include <memory> #include <numeric> @@ -27,23 +19,9 @@ #include <vips/vips8> #include <napi.h> -#include "common.h" -#include "operations.h" -#include "pipeline.h" - -#if defined(WIN32) -#define STAT64_STRUCT __stat64 -#define STAT64_FUNCTION _stat64 -#elif defined(__APPLE__) -#define STAT64_STRUCT stat -#define STAT64_FUNCTION stat -#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) -#define STAT64_STRUCT stat -#define STAT64_FUNCTION stat -#else -#define STAT64_STRUCT stat64 -#define STAT64_FUNCTION stat64 -#endif +#include "./common.h" +#include "./operations.h" +#include "./pipeline.h" class PipelineWorker : public Napi::AsyncWorker { public: @@ -58,273 +36,328 @@ class PipelineWorker : public Napi::AsyncWorker { // libuv worker void Execute() { // Decrement queued task counter - g_atomic_int_dec_and_test(&sharp::counterQueue); + sharp::counterQueue--; // Increment processing task counter - g_atomic_int_inc(&sharp::counterProcess); + sharp::counterProcess++; try { // Open input vips::VImage image; sharp::ImageType inputImageType; - std::tie(image, inputImageType) = sharp::OpenInput(baton->input); + if (baton->join.empty()) { + std::tie(image, inputImageType) = sharp::OpenInput(baton->input); + } else { + std::vector<VImage> images; + bool hasAlpha = false; + for (auto &join : baton->join) { + std::tie(image, inputImageType) = sharp::OpenInput(join); + image = sharp::EnsureColourspace(image, baton->colourspacePipeline); + images.push_back(image); + hasAlpha |= image.has_alpha(); + } + if (hasAlpha) { + for (auto &image : images) { + if (!image.has_alpha()) { + image = sharp::EnsureAlpha(image, 1); + } + } + } else { + baton->input->joinBackground.pop_back(); + } + inputImageType = sharp::ImageType::PNG; + image = VImage::arrayjoin(images, VImage::option() + ->set("across", baton->input->joinAcross) + ->set("shim", baton->input->joinShim) + ->set("background", baton->input->joinBackground) + ->set("halign", baton->input->joinHalign) + ->set("valign", baton->input->joinValign)); + if (baton->input->joinAnimated) { + image = image.copy(); + image.set(VIPS_META_N_PAGES, static_cast<int>(images.size())); + image.set(VIPS_META_PAGE_HEIGHT, static_cast<int>(image.height() / images.size())); + } + } + VipsAccess access = baton->input->access; + image = sharp::EnsureColourspace(image, baton->colourspacePipeline); + + int nPages = baton->input->pages; + if (nPages == -1) { + // Resolve the number of pages if we need to render until the end of the document + nPages = image.get_typeof(VIPS_META_N_PAGES) != 0 + ? image.get_int(VIPS_META_N_PAGES) - baton->input->page + : 1; + } + + // Get pre-resize page height + int pageHeight = sharp::GetPageHeight(image); // Calculate angle of rotation - VipsAngle rotation; - if (baton->useExifOrientation) { + VipsAngle rotation = VIPS_ANGLE_D0; + VipsAngle autoRotation = VIPS_ANGLE_D0; + bool autoFlop = false; + + if (baton->input->autoOrient) { // Rotate and flip image according to Exif orientation - bool flip; - bool flop; - std::tie(rotation, flip, flop) = CalculateExifRotationAndFlip(sharp::ExifOrientation(image)); - baton->flip = baton->flip || flip; - baton->flop = baton->flop || flop; - } else { - rotation = CalculateAngleRotation(baton->angle); + std::tie(autoRotation, autoFlop) = CalculateExifRotationAndFlop(sharp::ExifOrientation(image)); + } + + rotation = CalculateAngleRotation(baton->angle); + + bool const shouldRotateBefore = baton->rotateBefore && + (rotation != VIPS_ANGLE_D0 || baton->flip || baton->flop || baton->rotationAngle != 0.0); + bool const shouldOrientBefore = (shouldRotateBefore || baton->orientBefore) && + (autoRotation != VIPS_ANGLE_D0 || autoFlop); + + if (shouldOrientBefore) { + image = sharp::StaySequential(image, autoRotation != VIPS_ANGLE_D0); + if (autoRotation != VIPS_ANGLE_D0) { + if (autoRotation != VIPS_ANGLE_D180) { + MultiPageUnsupported(nPages, "Rotate"); + } + image = image.rot(autoRotation); + autoRotation = VIPS_ANGLE_D0; + } + if (autoFlop) { + image = image.flip(VIPS_DIRECTION_HORIZONTAL); + autoFlop = false; + } } - // Rotate pre-extract - if (baton->rotateBeforePreExtract) { + if (shouldRotateBefore) { + image = sharp::StaySequential(image, rotation != VIPS_ANGLE_D0 || baton->flip || baton->rotationAngle != 0.0); + if (baton->flip) { + image = image.flip(VIPS_DIRECTION_VERTICAL); + baton->flip = false; + } + if (baton->flop) { + image = image.flip(VIPS_DIRECTION_HORIZONTAL); + baton->flop = false; + } if (rotation != VIPS_ANGLE_D0) { + if (rotation != VIPS_ANGLE_D180) { + MultiPageUnsupported(nPages, "Rotate"); + } image = image.rot(rotation); - image = sharp::RemoveExifOrientation(image); + rotation = VIPS_ANGLE_D0; } if (baton->rotationAngle != 0.0) { + MultiPageUnsupported(nPages, "Rotate"); std::vector<double> background; - std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground); - image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background)); + std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground, false); + image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background)).copy_memory(); + baton->rotationAngle = 0.0; } } // Trim - if (baton->trimThreshold > 0.0) { - image = sharp::Trim(image, baton->trimThreshold); + if (baton->trimThreshold >= 0.0) { + MultiPageUnsupported(nPages, "Trim"); + image = sharp::StaySequential(image); + image = sharp::Trim(image, baton->trimBackground, baton->trimThreshold, baton->trimLineArt); baton->trimOffsetLeft = image.xoffset(); baton->trimOffsetTop = image.yoffset(); } // Pre extraction if (baton->topOffsetPre != -1) { - image = image.extract_area(baton->leftOffsetPre, baton->topOffsetPre, baton->widthPre, baton->heightPre); + image = nPages > 1 + ? sharp::CropMultiPage(image, + baton->leftOffsetPre, baton->topOffsetPre, baton->widthPre, baton->heightPre, nPages, &pageHeight) + : image.extract_area(baton->leftOffsetPre, baton->topOffsetPre, baton->widthPre, baton->heightPre); } // Get pre-resize image width and height int inputWidth = image.width(); int inputHeight = image.height(); - if (!baton->rotateBeforePreExtract && - (rotation == VIPS_ANGLE_D90 || rotation == VIPS_ANGLE_D270)) { - // Swap input output width and height when rotating by 90 or 270 degrees - std::swap(inputWidth, inputHeight); - } - // If withoutEnlargement is specified, - // Override target width and height if exceeds respective value from input file - if (baton->withoutEnlargement) { - if (baton->width > inputWidth) { - baton->width = inputWidth; - } - if (baton->height > inputHeight) { - baton->height = inputHeight; - } + // Is there just one page? Shrink to inputHeight instead + if (nPages == 1) { + pageHeight = inputHeight; } // Scaling calculations - double xfactor = 1.0; - double yfactor = 1.0; + double hshrink; + double vshrink; int targetResizeWidth = baton->width; int targetResizeHeight = baton->height; - if (baton->width > 0 && baton->height > 0) { - // Fixed width and height - xfactor = static_cast<double>(inputWidth) / static_cast<double>(baton->width); - yfactor = static_cast<double>(inputHeight) / static_cast<double>(baton->height); - switch (baton->canvas) { - case Canvas::CROP: - if (xfactor < yfactor) { - targetResizeHeight = static_cast<int>(round(static_cast<double>(inputHeight) / xfactor)); - yfactor = xfactor; - } else { - targetResizeWidth = static_cast<int>(round(static_cast<double>(inputWidth) / yfactor)); - xfactor = yfactor; - } - break; - case Canvas::EMBED: - if (xfactor > yfactor) { - targetResizeHeight = static_cast<int>(round(static_cast<double>(inputHeight) / xfactor)); - yfactor = xfactor; - } else { - targetResizeWidth = static_cast<int>(round(static_cast<double>(inputWidth) / yfactor)); - xfactor = yfactor; - } - break; - case Canvas::MAX: - if (xfactor > yfactor) { - targetResizeHeight = baton->height = static_cast<int>(round(static_cast<double>(inputHeight) / xfactor)); - yfactor = xfactor; - } else { - targetResizeWidth = baton->width = static_cast<int>(round(static_cast<double>(inputWidth) / yfactor)); - xfactor = yfactor; - } - break; - case Canvas::MIN: - if (xfactor < yfactor) { - targetResizeHeight = baton->height = static_cast<int>(round(static_cast<double>(inputHeight) / xfactor)); - yfactor = xfactor; - } else { - targetResizeWidth = baton->width = static_cast<int>(round(static_cast<double>(inputWidth) / yfactor)); - xfactor = yfactor; - } - break; - case Canvas::IGNORE_ASPECT: - if (!baton->rotateBeforePreExtract && - (rotation == VIPS_ANGLE_D90 || rotation == VIPS_ANGLE_D270)) { - std::swap(xfactor, yfactor); - } - break; - } - } else if (baton->width > 0) { - // Fixed width - xfactor = static_cast<double>(inputWidth) / static_cast<double>(baton->width); - if (baton->canvas == Canvas::IGNORE_ASPECT) { - targetResizeHeight = baton->height = inputHeight; - } else { - // Auto height - yfactor = xfactor; - targetResizeHeight = baton->height = static_cast<int>(round(static_cast<double>(inputHeight) / yfactor)); - } - } else if (baton->height > 0) { - // Fixed height - yfactor = static_cast<double>(inputHeight) / static_cast<double>(baton->height); - if (baton->canvas == Canvas::IGNORE_ASPECT) { - targetResizeWidth = baton->width = inputWidth; - } else { - // Auto width - xfactor = yfactor; - targetResizeWidth = baton->width = static_cast<int>(round(static_cast<double>(inputWidth) / xfactor)); - } - } else { - // Identity transform - baton->width = inputWidth; - baton->height = inputHeight; - } - // Calculate integral box shrink - int xshrink = std::max(1, static_cast<int>(floor(xfactor))); - int yshrink = std::max(1, static_cast<int>(floor(yfactor))); - - // Calculate residual float affine transformation - double xresidual = static_cast<double>(xshrink) / xfactor; - double yresidual = static_cast<double>(yshrink) / yfactor; - - // If integral x and y shrink are equal, try to use shrink-on-load for JPEG and WebP, - // but not when applying gamma correction, pre-resize extract or trim - int shrink_on_load = 1; - - int shrink_on_load_factor = 1; - // Leave at least a factor of two for the final resize step, when fastShrinkOnLoad: false - // for more consistent results and avoid occasional small image shifting - if (!baton->fastShrinkOnLoad) { - shrink_on_load_factor = 2; + // When auto-rotating by 90 or 270 degrees, swap the target width and + // height to ensure the behavior aligns with how it would have been if + // the rotation had taken place *before* resizing. + if (autoRotation == VIPS_ANGLE_D90 || autoRotation == VIPS_ANGLE_D270) { + std::swap(targetResizeWidth, targetResizeHeight); } - if ( - xshrink == yshrink && xshrink >= 2 * shrink_on_load_factor && - (inputImageType == sharp::ImageType::JPEG || inputImageType == sharp::ImageType::WEBP) && - baton->gamma == 0 && baton->topOffsetPre == -1 && baton->trimThreshold == 0.0 - ) { - if (xshrink >= 8 * shrink_on_load_factor) { - xfactor = xfactor / 8; - yfactor = yfactor / 8; - shrink_on_load = 8; - } else if (xshrink >= 4 * shrink_on_load_factor) { - xfactor = xfactor / 4; - yfactor = yfactor / 4; - shrink_on_load = 4; - } else if (xshrink >= 2 * shrink_on_load_factor) { - xfactor = xfactor / 2; - yfactor = yfactor / 2; - shrink_on_load = 2; + + // Shrink to pageHeight, so we work for multi-page images + std::tie(hshrink, vshrink) = sharp::ResolveShrink( + inputWidth, pageHeight, targetResizeWidth, targetResizeHeight, + baton->canvas, baton->withoutEnlargement, baton->withoutReduction); + + // The jpeg preload shrink. + int jpegShrinkOnLoad = 1; + + // WebP, PDF, SVG scale + double scale = 1.0; + + // Try to reload input using shrink-on-load for JPEG, WebP, SVG and PDF, when: + // - the width or height parameters are specified; + // - gamma correction doesn't need to be applied; + // - trimming or pre-resize extract isn't required; + // - input colourspace is not specified; + bool const shouldPreShrink = (targetResizeWidth > 0 || targetResizeHeight > 0) && + baton->gamma == 0 && baton->topOffsetPre == -1 && baton->trimThreshold < 0.0 && + baton->colourspacePipeline == VIPS_INTERPRETATION_LAST && !(shouldOrientBefore || shouldRotateBefore); + + if (shouldPreShrink) { + // The common part of the shrink: the bit by which both axes must be shrunk + double shrink = std::min(hshrink, vshrink); + + if (inputImageType == sharp::ImageType::JPEG) { + // Leave at least a factor of two for the final resize step, when fastShrinkOnLoad: false + // for more consistent results and to avoid extra sharpness to the image + int factor = baton->fastShrinkOnLoad ? 1 : 2; + if (shrink >= 8 * factor) { + jpegShrinkOnLoad = 8; + } else if (shrink >= 4 * factor) { + jpegShrinkOnLoad = 4; + } else if (shrink >= 2 * factor) { + jpegShrinkOnLoad = 2; + } + // Lower shrink-on-load for known libjpeg rounding errors + if (jpegShrinkOnLoad > 1 && static_cast<int>(shrink) == jpegShrinkOnLoad) { + jpegShrinkOnLoad /= 2; + } + } else if (inputImageType == sharp::ImageType::WEBP && baton->fastShrinkOnLoad && shrink > 1.0) { + // Avoid upscaling via webp + scale = 1.0 / shrink; + } else if (inputImageType == sharp::ImageType::SVG || + inputImageType == sharp::ImageType::PDF) { + scale = 1.0 / shrink; } } - // Help ensure a final kernel-based reduction to prevent shrink aliasing - if (shrink_on_load > 1 && (xresidual == 1.0 || yresidual == 1.0)) { - shrink_on_load = shrink_on_load / 2; - xfactor = xfactor * 2; - yfactor = yfactor * 2; - } - if (shrink_on_load > 1) { - // Reload input using shrink-on-load - vips::VOption *option = VImage::option() - ->set("access", baton->input->access) - ->set("shrink", shrink_on_load) - ->set("fail", baton->input->failOnError); + + // Reload input using shrink-on-load, it'll be an integer shrink + // factor for jpegload*, a double scale factor for webpload*, + // pdfload* and svgload* + if (jpegShrinkOnLoad > 1) { + vips::VOption *option = GetOptionsForImageType(inputImageType, baton->input)->set("shrink", jpegShrinkOnLoad); if (baton->input->buffer != nullptr) { + // Reload JPEG buffer VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength); - if (inputImageType == sharp::ImageType::JPEG) { - // Reload JPEG buffer - image = VImage::jpegload_buffer(blob, option); - } else { - // Reload WebP buffer - image = VImage::webpload_buffer(blob, option); - } + image = VImage::jpegload_buffer(blob, option); vips_area_unref(reinterpret_cast<VipsArea*>(blob)); } else { - if (inputImageType == sharp::ImageType::JPEG) { - // Reload JPEG file - image = VImage::jpegload(const_cast<char*>(baton->input->file.data()), option); + // Reload JPEG file + image = VImage::jpegload(const_cast<char*>(baton->input->file.data()), option); + } + } else if (scale != 1.0) { + vips::VOption *option = GetOptionsForImageType(inputImageType, baton->input)->set("scale", scale); + if (inputImageType == sharp::ImageType::WEBP) { + if (baton->input->buffer != nullptr) { + // Reload WebP buffer + VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength); + image = VImage::webpload_buffer(blob, option); + vips_area_unref(reinterpret_cast<VipsArea*>(blob)); } else { // Reload WebP file image = VImage::webpload(const_cast<char*>(baton->input->file.data()), option); } + } else if (inputImageType == sharp::ImageType::SVG) { + if (baton->input->buffer != nullptr) { + // Reload SVG buffer + VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength); + image = VImage::svgload_buffer(blob, option); + vips_area_unref(reinterpret_cast<VipsArea*>(blob)); + } else { + // Reload SVG file + image = VImage::svgload(const_cast<char*>(baton->input->file.data()), option); + } + sharp::SetDensity(image, baton->input->density); + if (image.width() > 32767 || image.height() > 32767) { + throw vips::VError("Input SVG image will exceed 32767x32767 pixel limit when scaled"); + } + } else if (inputImageType == sharp::ImageType::PDF) { + if (baton->input->buffer != nullptr) { + // Reload PDF buffer + VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength); + image = VImage::pdfload_buffer(blob, option); + vips_area_unref(reinterpret_cast<VipsArea*>(blob)); + } else { + // Reload PDF file + image = VImage::pdfload(const_cast<char*>(baton->input->file.data()), option); + } + sharp::SetDensity(image, baton->input->density); } - // Recalculate integral shrink and double residual - int const shrunkOnLoadWidth = image.width(); - int const shrunkOnLoadHeight = image.height(); - if (!baton->rotateBeforePreExtract && - (rotation == VIPS_ANGLE_D90 || rotation == VIPS_ANGLE_D270)) { - // Swap when rotating by 90 or 270 degrees - xfactor = static_cast<double>(shrunkOnLoadWidth) / static_cast<double>(targetResizeHeight); - yfactor = static_cast<double>(shrunkOnLoadHeight) / static_cast<double>(targetResizeWidth); - } else { - xfactor = static_cast<double>(shrunkOnLoadWidth) / static_cast<double>(targetResizeWidth); - yfactor = static_cast<double>(shrunkOnLoadHeight) / static_cast<double>(targetResizeHeight); + } else { + if (inputImageType == sharp::ImageType::SVG && (image.width() > 32767 || image.height() > 32767)) { + throw vips::VError("Input SVG image exceeds 32767x32767 pixel limit"); } } + if (baton->input->autoOrient) { + image = sharp::RemoveExifOrientation(image); + } + + // Any pre-shrinking may already have been done + inputWidth = image.width(); + inputHeight = image.height(); + + // After pre-shrink, but before the main shrink stage + // Reuse the initial pageHeight if we didn't pre-shrink + if (shouldPreShrink) { + pageHeight = sharp::GetPageHeight(image); + } + + // Shrink to pageHeight, so we work for multi-page images + std::tie(hshrink, vshrink) = sharp::ResolveShrink( + inputWidth, pageHeight, targetResizeWidth, targetResizeHeight, + baton->canvas, baton->withoutEnlargement, baton->withoutReduction); + + int targetHeight = static_cast<int>(std::rint(static_cast<double>(pageHeight) / vshrink)); + int targetPageHeight = targetHeight; + + // In toilet-roll mode, we must adjust vshrink so that we exactly hit + // pageHeight or we'll have pixels straddling pixel boundaries + if (inputHeight > pageHeight) { + targetHeight *= nPages; + vshrink = static_cast<double>(inputHeight) / targetHeight; + } // Ensure we're using a device-independent colour space + std::pair<char*, size_t> inputProfile(nullptr, 0); + if ((baton->keepMetadata & VIPS_FOREIGN_KEEP_ICC) && baton->withIccProfile.empty()) { + // Cache input profile for use with output + inputProfile = sharp::GetProfile(image); + baton->input->ignoreIcc = true; + } + char const *processingProfile = image.interpretation() == VIPS_INTERPRETATION_RGB16 ? "p3" : "srgb"; if ( sharp::HasProfile(image) && image.interpretation() != VIPS_INTERPRETATION_LABS && - image.interpretation() != VIPS_INTERPRETATION_GREY16 + image.interpretation() != VIPS_INTERPRETATION_GREY16 && + baton->colourspacePipeline != VIPS_INTERPRETATION_CMYK && + !baton->input->ignoreIcc ) { - // Convert to sRGB using embedded profile + // Convert to sRGB/P3 using embedded profile try { - image = image.icc_transform("srgb", VImage::option() - ->set("embedded", TRUE) - ->set("depth", image.interpretation() == VIPS_INTERPRETATION_RGB16 ? 16 : 8) + image = image.icc_transform(processingProfile, VImage::option() + ->set("embedded", true) + ->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8) ->set("intent", VIPS_INTENT_PERCEPTUAL)); } catch(...) { - // Ignore failure of embedded profile + sharp::VipsWarningCallback(nullptr, G_LOG_LEVEL_WARNING, "Invalid embedded profile", nullptr); } - } else if (image.interpretation() == VIPS_INTERPRETATION_CMYK) { - image = image.icc_transform("srgb", VImage::option() + } else if ( + image.interpretation() == VIPS_INTERPRETATION_CMYK && + baton->colourspacePipeline != VIPS_INTERPRETATION_CMYK + ) { + image = image.icc_transform(processingProfile, VImage::option() ->set("input_profile", "cmyk") ->set("intent", VIPS_INTENT_PERCEPTUAL)); } // Flatten image to remove alpha channel - if (baton->flatten && sharp::HasAlpha(image)) { - // Scale up 8-bit values to match 16-bit input image - double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0; - // Background colour - std::vector<double> background { - baton->flattenBackground[0] * multiplier, - baton->flattenBackground[1] * multiplier, - baton->flattenBackground[2] * multiplier - }; - image = image.flatten(VImage::option() - ->set("background", background)); - } - - // Negate the colours in the image - if (baton->negate) { - image = image.invert(); + if (baton->flatten && image.has_alpha()) { + image = sharp::Flatten(image, baton->flattenBackground); } // Gamma encoding (darken) @@ -337,190 +370,241 @@ class PipelineWorker : public Napi::AsyncWorker { image = image.colourspace(VIPS_INTERPRETATION_B_W); } - bool const shouldResize = xfactor != 1.0 || yfactor != 1.0; + bool const shouldResize = hshrink != 1.0 || vshrink != 1.0; bool const shouldBlur = baton->blurSigma != 0.0; bool const shouldConv = baton->convKernelWidth * baton->convKernelHeight > 0; bool const shouldSharpen = baton->sharpenSigma != 0.0; - bool const shouldApplyMedian = baton->medianSize > 0; bool const shouldComposite = !baton->composite.empty(); - bool const shouldModulate = baton->brightness != 1.0 || baton->saturation != 1.0 || baton->hue != 0.0; - if (shouldComposite && !sharp::HasAlpha(image)) { - image = sharp::EnsureAlpha(image); + if (shouldComposite && !image.has_alpha()) { + image = sharp::EnsureAlpha(image, 1); } - bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) && - (shouldResize || shouldBlur || shouldConv || shouldSharpen || shouldComposite); + VipsBandFormat premultiplyFormat = image.format(); + bool const shouldPremultiplyAlpha = image.has_alpha() && + (shouldResize || shouldBlur || shouldConv || shouldSharpen); - // Premultiply image alpha channel before all transformations to avoid - // dark fringing around bright pixels - // See: http://entropymine.com/imageworsener/resizealpha/ if (shouldPremultiplyAlpha) { - image = image.premultiply(); + image = image.premultiply().cast(premultiplyFormat); } // Resize if (shouldResize) { - VipsKernel kernel = static_cast<VipsKernel>( - vips_enum_from_nick(nullptr, VIPS_TYPE_KERNEL, baton->kernel.data())); - if ( - kernel != VIPS_KERNEL_NEAREST && kernel != VIPS_KERNEL_CUBIC && kernel != VIPS_KERNEL_LANCZOS2 && - kernel != VIPS_KERNEL_LANCZOS3 && kernel != VIPS_KERNEL_MITCHELL - ) { - throw vips::VError("Unknown kernel"); - } - // Ensure shortest edge is at least 1 pixel - if (image.width() / xfactor < 0.5) { - xfactor = 2 * image.width(); - baton->width = 1; - } - if (image.height() / yfactor < 0.5) { - yfactor = 2 * image.height(); - baton->height = 1; - } - image = image.resize(1.0 / xfactor, VImage::option() - ->set("vscale", 1.0 / yfactor) - ->set("kernel", kernel)); + image = image.resize(1.0 / hshrink, VImage::option() + ->set("vscale", 1.0 / vshrink) + ->set("kernel", baton->kernel)); } - // Rotate post-extract 90-angle - if (!baton->rotateBeforePreExtract && rotation != VIPS_ANGLE_D0) { - image = image.rot(rotation); - image = sharp::RemoveExifOrientation(image); + image = sharp::StaySequential(image, + autoRotation != VIPS_ANGLE_D0 || + baton->flip || + rotation != VIPS_ANGLE_D0); + // Auto-rotate post-extract + if (autoRotation != VIPS_ANGLE_D0) { + if (autoRotation != VIPS_ANGLE_D180) { + MultiPageUnsupported(nPages, "Rotate"); + } + image = image.rot(autoRotation); } - - - // Flip (mirror about Y axis) + // Mirror vertically (up-down) about the x-axis if (baton->flip) { image = image.flip(VIPS_DIRECTION_VERTICAL); - image = sharp::RemoveExifOrientation(image); } - - // Flop (mirror about X axis) - if (baton->flop) { + // Mirror horizontally (left-right) about the y-axis + if (baton->flop != autoFlop) { image = image.flip(VIPS_DIRECTION_HORIZONTAL); - image = sharp::RemoveExifOrientation(image); + } + // Rotate post-extract 90-angle + if (rotation != VIPS_ANGLE_D0) { + if (rotation != VIPS_ANGLE_D180) { + MultiPageUnsupported(nPages, "Rotate"); + } + image = image.rot(rotation); } // Join additional color channels to the image - if (baton->joinChannelIn.size() > 0) { + if (!baton->joinChannelIn.empty()) { VImage joinImage; sharp::ImageType joinImageType = sharp::ImageType::UNKNOWN; for (unsigned int i = 0; i < baton->joinChannelIn.size(); i++) { + baton->joinChannelIn[i]->access = access; std::tie(joinImage, joinImageType) = sharp::OpenInput(baton->joinChannelIn[i]); + joinImage = sharp::EnsureColourspace(joinImage, baton->colourspacePipeline); image = image.bandjoin(joinImage); } image = image.copy(VImage::option()->set("interpretation", baton->colourspace)); + image = sharp::RemoveGifPalette(image); + } + + inputWidth = image.width(); + inputHeight = nPages > 1 ? targetPageHeight : image.height(); + + // Resolve dimensions + if (baton->width <= 0) { + baton->width = inputWidth; + } + if (baton->height <= 0) { + baton->height = inputHeight; } // Crop/embed - if (image.width() != baton->width || image.height() != baton->height) { - if (baton->canvas == Canvas::EMBED) { + if (inputWidth != baton->width || inputHeight != baton->height) { + if (baton->canvas == sharp::Canvas::EMBED) { std::vector<double> background; - std::tie(image, background) = sharp::ApplyAlpha(image, baton->resizeBackground); + std::tie(image, background) = sharp::ApplyAlpha(image, baton->resizeBackground, shouldPremultiplyAlpha); // Embed + const auto& [left, top] = sharp::CalculateEmbedPosition( + inputWidth, inputHeight, baton->width, baton->height, baton->position); + const int width = std::max(inputWidth, baton->width); + const int height = std::max(inputHeight, baton->height); + + image = nPages > 1 + ? sharp::EmbedMultiPage(image, + left, top, width, height, VIPS_EXTEND_BACKGROUND, background, nPages, &targetPageHeight) + : image.embed(left, top, width, height, VImage::option() + ->set("extend", VIPS_EXTEND_BACKGROUND) + ->set("background", background)); + } else if (baton->canvas == sharp::Canvas::CROP) { + if (baton->width > inputWidth) { + baton->width = inputWidth; + } + if (baton->height > inputHeight) { + baton->height = inputHeight; + } - // Calculate where to position the embeded image if gravity specified, else center. - int left; - int top; - - left = static_cast<int>(round((baton->width - image.width()) / 2)); - top = static_cast<int>(round((baton->height - image.height()) / 2)); - - int width = std::max(image.width(), baton->width); - int height = std::max(image.height(), baton->height); - std::tie(left, top) = sharp::CalculateEmbedPosition( - image.width(), image.height(), baton->width, baton->height, baton->position); - - image = image.embed(left, top, width, height, VImage::option() - ->set("extend", VIPS_EXTEND_BACKGROUND) - ->set("background", background)); - - } else if ( - baton->canvas != Canvas::IGNORE_ASPECT && - (image.width() > baton->width || image.height() > baton->height) - ) { - // Crop/max/min + // Crop if (baton->position < 9) { // Gravity-based crop - int left; - int top; - std::tie(left, top) = sharp::CalculateCrop( - image.width(), image.height(), baton->width, baton->height, baton->position); - int width = std::min(image.width(), baton->width); - int height = std::min(image.height(), baton->height); - image = image.extract_area(left, top, width, height); + const auto& [left, top] = sharp::CalculateCrop( + inputWidth, inputHeight, baton->width, baton->height, baton->position); + const int width = std::min(inputWidth, baton->width); + const int height = std::min(inputHeight, baton->height); + + image = nPages > 1 + ? sharp::CropMultiPage(image, + left, top, width, height, nPages, &targetPageHeight) + : image.extract_area(left, top, width, height); } else { + int attention_x; + int attention_y; + // Attention-based or Entropy-based crop - if (baton->width > image.width()) { - baton->width = image.width(); - } - if (baton->height > image.height()) { - baton->height = image.height(); - } - image = image.tilecache(VImage::option() - ->set("access", VIPS_ACCESS_RANDOM) - ->set("threaded", TRUE)); + MultiPageUnsupported(nPages, "Resize strategy"); + image = sharp::StaySequential(image); image = image.smartcrop(baton->width, baton->height, VImage::option() - ->set("interesting", baton->position == 16 ? VIPS_INTERESTING_ENTROPY : VIPS_INTERESTING_ATTENTION)); + ->set("interesting", baton->position == 16 ? VIPS_INTERESTING_ENTROPY : VIPS_INTERESTING_ATTENTION) + ->set("premultiplied", shouldPremultiplyAlpha) + ->set("attention_x", &attention_x) + ->set("attention_y", &attention_y)); baton->hasCropOffset = true; baton->cropOffsetLeft = static_cast<int>(image.xoffset()); baton->cropOffsetTop = static_cast<int>(image.yoffset()); + baton->hasAttentionCenter = true; + baton->attentionX = static_cast<int>(attention_x * jpegShrinkOnLoad / scale); + baton->attentionY = static_cast<int>(attention_y * jpegShrinkOnLoad / scale); } } } // Rotate post-extract non-90 angle - if (!baton->rotateBeforePreExtract && baton->rotationAngle != 0.0) { + if (!baton->rotateBefore && baton->rotationAngle != 0.0) { + MultiPageUnsupported(nPages, "Rotate"); + image = sharp::StaySequential(image); std::vector<double> background; - std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground); + std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground, shouldPremultiplyAlpha); image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background)); } // Post extraction if (baton->topOffsetPost != -1) { - image = image.extract_area( - baton->leftOffsetPost, baton->topOffsetPost, baton->widthPost, baton->heightPost); + if (nPages > 1) { + image = sharp::CropMultiPage(image, + baton->leftOffsetPost, baton->topOffsetPost, baton->widthPost, baton->heightPost, + nPages, &targetPageHeight); + + // heightPost is used in the info object, so update to reflect the number of pages + baton->heightPost *= nPages; + } else { + image = image.extract_area( + baton->leftOffsetPost, baton->topOffsetPost, baton->widthPost, baton->heightPost); + } } // Affine transform - if (baton->affineMatrix.size() > 0) { + if (!baton->affineMatrix.empty()) { + MultiPageUnsupported(nPages, "Affine"); + image = sharp::StaySequential(image); std::vector<double> background; - std::tie(image, background) = sharp::ApplyAlpha(image, baton->affineBackground); + std::tie(image, background) = sharp::ApplyAlpha(image, baton->affineBackground, shouldPremultiplyAlpha); + vips::VInterpolate interp = vips::VInterpolate::new_from_name( + const_cast<char*>(baton->affineInterpolator.data())); 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)); + ->set("interpolate", interp)); } // Extend edges if (baton->extendTop > 0 || baton->extendBottom > 0 || baton->extendLeft > 0 || baton->extendRight > 0) { - std::vector<double> background; - std::tie(image, background) = sharp::ApplyAlpha(image, baton->extendBackground); - // Embed baton->width = image.width() + baton->extendLeft + baton->extendRight; - baton->height = image.height() + baton->extendTop + baton->extendBottom; + baton->height = (nPages > 1 ? targetPageHeight : image.height()) + baton->extendTop + baton->extendBottom; - image = image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height, - VImage::option()->set("extend", VIPS_EXTEND_BACKGROUND)->set("background", background)); + if (baton->extendWith == VIPS_EXTEND_BACKGROUND) { + std::vector<double> background; + std::tie(image, background) = sharp::ApplyAlpha(image, baton->extendBackground, shouldPremultiplyAlpha); + + image = sharp::StaySequential(image, nPages > 1); + image = nPages > 1 + ? sharp::EmbedMultiPage(image, + baton->extendLeft, baton->extendTop, baton->width, baton->height, + baton->extendWith, background, nPages, &targetPageHeight) + : image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height, + VImage::option()->set("extend", baton->extendWith)->set("background", background)); + } else { + std::vector<double> ignoredBackground(1); + image = sharp::StaySequential(image); + image = nPages > 1 + ? sharp::EmbedMultiPage(image, + baton->extendLeft, baton->extendTop, baton->width, baton->height, + baton->extendWith, ignoredBackground, nPages, &targetPageHeight) + : image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height, + VImage::option()->set("extend", baton->extendWith)); + } } // Median - must happen before blurring, due to the utility of blurring after thresholding - if (shouldApplyMedian) { + if (baton->medianSize > 0) { image = image.median(baton->medianSize); } + // Threshold - must happen before blurring, due to the utility of blurring after thresholding + // Threshold - must happen before unflatten to enable non-white unflattening if (baton->threshold != 0) { image = sharp::Threshold(image, baton->threshold, baton->thresholdGrayscale); } + // Dilate - must happen before blurring, due to the utility of dilating after thresholding + if (baton->dilateWidth != 0) { + image = sharp::Dilate(image, baton->dilateWidth); + } + + // Erode - must happen before blurring, due to the utility of eroding after thresholding + if (baton->erodeWidth != 0) { + image = sharp::Erode(image, baton->erodeWidth); + } + // Blur if (shouldBlur) { - image = sharp::Blur(image, baton->blurSigma); + image = sharp::Blur(image, baton->blurSigma, baton->precision, baton->minAmpl); + } + + // Unflatten the image + if (baton->unflatten) { + image = sharp::Unflatten(image); } // Convolve @@ -532,25 +616,55 @@ class PipelineWorker : public Napi::AsyncWorker { } // Recomb - if (baton->recombMatrix != NULL) { + if (!baton->recombMatrix.empty()) { image = sharp::Recomb(image, baton->recombMatrix); } - if (shouldModulate) { - image = sharp::Modulate(image, baton->brightness, baton->saturation, baton->hue); + // Modulate + if (baton->brightness != 1.0 || baton->saturation != 1.0 || baton->hue != 0.0 || baton->lightness != 0.0) { + image = sharp::Modulate(image, baton->brightness, baton->saturation, baton->hue, baton->lightness); } // Sharpen if (shouldSharpen) { - image = sharp::Sharpen(image, baton->sharpenSigma, baton->sharpenFlat, baton->sharpenJagged); + image = sharp::Sharpen(image, baton->sharpenSigma, baton->sharpenM1, baton->sharpenM2, + baton->sharpenX1, baton->sharpenY2, baton->sharpenY3); + } + + // Reverse premultiplication after all transformations + if (shouldPremultiplyAlpha) { + image = image.unpremultiply().cast(premultiplyFormat); } + baton->premultiplied = shouldPremultiplyAlpha; // Composite if (shouldComposite) { + std::vector<VImage> images = { image }; + std::vector<int> modes, xs, ys; for (Composite *composite : baton->composite) { VImage compositeImage; sharp::ImageType compositeImageType = sharp::ImageType::UNKNOWN; - std::tie(compositeImage, compositeImageType) = OpenInput(composite->input); + composite->input->access = access; + std::tie(compositeImage, compositeImageType) = sharp::OpenInput(composite->input); + + if (composite->input->autoOrient) { + // Respect EXIF Orientation + VipsAngle compositeAutoRotation = VIPS_ANGLE_D0; + bool compositeAutoFlop = false; + std::tie(compositeAutoRotation, compositeAutoFlop) = + CalculateExifRotationAndFlop(sharp::ExifOrientation(compositeImage)); + + compositeImage = sharp::RemoveExifOrientation(compositeImage); + compositeImage = sharp::StaySequential(compositeImage, compositeAutoRotation != VIPS_ANGLE_D0); + + if (compositeAutoRotation != VIPS_ANGLE_D0) { + compositeImage = compositeImage.rot(compositeAutoRotation); + } + if (compositeAutoFlop) { + compositeImage = compositeImage.flip(VIPS_DIRECTION_HORIZONTAL); + } + } + // Verify within current dimensions if (compositeImage.width() > image.width() || compositeImage.height() > image.height()) { throw vips::VError("Image to composite must have same dimensions or smaller"); @@ -560,14 +674,14 @@ class PipelineWorker : public Napi::AsyncWorker { int across = 0; int down = 0; // Use gravity in overlay - if (compositeImage.width() <= baton->width) { + if (compositeImage.width() <= image.width()) { across = static_cast<int>(ceil(static_cast<double>(image.width()) / compositeImage.width())); // Ensure odd number of tiles across when gravity is centre, north or south if (composite->gravity == 0 || composite->gravity == 1 || composite->gravity == 3) { across |= 1; } } - if (compositeImage.height() <= baton->height) { + if (compositeImage.height() <= image.height()) { down = static_cast<int>(ceil(static_cast<double>(image.height()) / compositeImage.height())); // Ensure odd number of tiles down when gravity is centre, east or west if (composite->gravity == 0 || composite->gravity == 2 || composite->gravity == 4) { @@ -577,7 +691,7 @@ class PipelineWorker : public Napi::AsyncWorker { if (across != 0 || down != 0) { int left; int top; - compositeImage = compositeImage.replicate(across, down); + compositeImage = sharp::StaySequential(compositeImage).replicate(across, down); if (composite->hasOffset) { std::tie(left, top) = sharp::CalculateCrop( compositeImage.width(), compositeImage.height(), image.width(), image.height(), @@ -591,12 +705,9 @@ class PipelineWorker : public Napi::AsyncWorker { // gravity was used for extract_area, set it back to its default value of 0 composite->gravity = 0; } - // Ensure image to composite is sRGB with premultiplied alpha - compositeImage = compositeImage.colourspace(VIPS_INTERPRETATION_sRGB); - if (!sharp::HasAlpha(compositeImage)) { - compositeImage = sharp::EnsureAlpha(compositeImage); - } - if (!composite->premultiplied) compositeImage = compositeImage.premultiply(); + // Ensure image to composite is with unpremultiplied alpha + compositeImage = sharp::EnsureAlpha(compositeImage, 1); + if (composite->premultiplied) compositeImage = compositeImage.unpremultiply(); // Calculate position int left; int top; @@ -614,25 +725,19 @@ class PipelineWorker : public Napi::AsyncWorker { std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(), compositeImage.width(), compositeImage.height(), composite->gravity); } - // Composite - image = image.composite2(compositeImage, composite->mode, VImage::option() - ->set("premultiplied", TRUE) - ->set("x", left) - ->set("y", top)); - } - } - - // Reverse premultiplication after all transformations: - if (shouldPremultiplyAlpha) { - image = image.unpremultiply(); - // Cast pixel values to integer - if (sharp::Is16Bit(image.interpretation())) { - image = image.cast(VIPS_FORMAT_USHORT); - } else { - image = image.cast(VIPS_FORMAT_UCHAR); + images.push_back(compositeImage); + modes.push_back(composite->mode); + xs.push_back(left); + ys.push_back(top); } + image = VImage::composite(images, modes, VImage::option() + ->set("compositing_space", baton->colourspacePipeline == VIPS_INTERPRETATION_LAST + ? VIPS_INTERPRETATION_sRGB + : baton->colourspacePipeline) + ->set("x", xs) + ->set("y", ys)); + image = sharp::RemoveGifPalette(image); } - baton->premultiplied = shouldPremultiplyAlpha; // Gamma decoding (brighten) if (baton->gammaOut >= 1 && baton->gammaOut <= 3) { @@ -640,21 +745,31 @@ class PipelineWorker : public Napi::AsyncWorker { } // Linear adjustment (a * in + b) - if (baton->linearA != 1.0 || baton->linearB != 0.0) { + if (!baton->linearA.empty()) { image = sharp::Linear(image, baton->linearA, baton->linearB); } // Apply normalisation - stretch luminance to cover full dynamic range if (baton->normalise) { - image = sharp::Normalise(image); + image = sharp::StaySequential(image); + image = sharp::Normalise(image, baton->normaliseLower, baton->normaliseUpper); + } + + // Apply contrast limiting adaptive histogram equalization (CLAHE) + if (baton->claheWidth != 0 && baton->claheHeight != 0) { + image = sharp::StaySequential(image); + image = sharp::Clahe(image, baton->claheWidth, baton->claheHeight, baton->claheMaxSlope); } // Apply bitwise boolean operation between images if (baton->boolean != nullptr) { VImage booleanImage; sharp::ImageType booleanImageType = sharp::ImageType::UNKNOWN; + baton->boolean->access = access; std::tie(booleanImage, booleanImageType) = sharp::OpenInput(baton->boolean); + booleanImage = sharp::EnsureColourspace(booleanImage, baton->colourspacePipeline); image = sharp::Boolean(image, booleanImage, baton->booleanOp); + image = sharp::RemoveGifPalette(image); } // Apply per-channel Bandbool bitwise operations after all other operations @@ -663,26 +778,8 @@ class PipelineWorker : public Napi::AsyncWorker { } // Tint the image - if (baton->tintA < 128.0 || baton->tintB < 128.0) { - image = sharp::Tint(image, baton->tintA, baton->tintB); - } - - // Extract an image channel (aka vips band) - if (baton->extractChannel > -1) { - if (baton->extractChannel >= image.bands()) { - if (baton->extractChannel == 3 && sharp::HasAlpha(image)) { - baton->extractChannel = image.bands() - 1; - } else { - (baton->err).append("Cannot extract channel from image. Too few channels in image."); - return Error(); - } - } - VipsInterpretation const interpretation = sharp::Is16Bit(image.interpretation()) - ? VIPS_INTERPRETATION_GREY16 - : VIPS_INTERPRETATION_B_W; - image = image - .extract_band(baton->extractChannel) - .copy(VImage::option()->set("interpretation", interpretation)); + if (baton->tint[0] >= 0.0) { + image = sharp::Tint(image, baton->tint); } // Remove alpha channel, if any @@ -691,65 +788,110 @@ class PipelineWorker : public Napi::AsyncWorker { } // Ensure alpha channel, if missing - if (baton->ensureAlpha) { - image = sharp::EnsureAlpha(image); + if (baton->ensureAlpha != -1) { + image = sharp::EnsureAlpha(image, baton->ensureAlpha); } - // Convert image to sRGB, if not already + // Ensure output colour space if (sharp::Is16Bit(image.interpretation())) { image = image.cast(VIPS_FORMAT_USHORT); } if (image.interpretation() != baton->colourspace) { - // Convert colourspace, pass the current known interpretation so libvips doesn't have to guess image = image.colourspace(baton->colourspace, VImage::option()->set("source_space", image.interpretation())); - // Transform colours from embedded profile to output profile - if (baton->withMetadata && sharp::HasProfile(image)) { - image = image.icc_transform(vips_enum_nick(VIPS_TYPE_INTERPRETATION, baton->colourspace), - VImage::option()->set("embedded", TRUE)); + if (inputProfile.first != nullptr && baton->withIccProfile.empty()) { + image = sharp::SetProfile(image, inputProfile); } } + // Extract channel + if (baton->extractChannel > -1) { + if (baton->extractChannel >= image.bands()) { + if (baton->extractChannel == 3 && image.has_alpha()) { + baton->extractChannel = image.bands() - 1; + } else { + (baton->err) + .append("Cannot extract channel ").append(std::to_string(baton->extractChannel)) + .append(" from image with channels 0-").append(std::to_string(image.bands() - 1)); + return Error(); + } + } + VipsInterpretation colourspace = sharp::Is16Bit(image.interpretation()) + ? VIPS_INTERPRETATION_GREY16 + : VIPS_INTERPRETATION_B_W; + image = image + .extract_band(baton->extractChannel) + .copy(VImage::option()->set("interpretation", colourspace)); + } + // Apply output ICC profile - if (!baton->withMetadataIcc.empty()) { - image = image.icc_transform( - const_cast<char*>(baton->withMetadataIcc.data()), - VImage::option() - ->set("input_profile", "srgb") + if (!baton->withIccProfile.empty()) { + try { + image = image.icc_transform(const_cast<char*>(baton->withIccProfile.data()), VImage::option() + ->set("input_profile", processingProfile) + ->set("embedded", true) + ->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8) ->set("intent", VIPS_INTENT_PERCEPTUAL)); + } catch(...) { + sharp::VipsWarningCallback(nullptr, G_LOG_LEVEL_WARNING, "Invalid profile", nullptr); + } + } + + // Negate the colours in the image + if (baton->negate) { + image = sharp::Negate(image, baton->negateAlpha); } // Override EXIF Orientation tag - if (baton->withMetadata && baton->withMetadataOrientation != -1) { + if (baton->withMetadataOrientation != -1) { image = sharp::SetExifOrientation(image, baton->withMetadataOrientation); } - + // Override pixel density + if (baton->withMetadataDensity > 0) { + image = sharp::SetDensity(image, baton->withMetadataDensity); + } + // EXIF key/value pairs + if (baton->keepMetadata & VIPS_FOREIGN_KEEP_EXIF) { + image = image.copy(); + if (!baton->withExifMerge) { + image = sharp::RemoveExif(image); + } + for (const auto& [key, value] : baton->withExif) { + image.set(key.c_str(), value.c_str()); + } + } + // XMP buffer + if ((baton->keepMetadata & VIPS_FOREIGN_KEEP_XMP) && !baton->withXmp.empty()) { + image = image.copy(); + image.set(VIPS_META_XMP_NAME, nullptr, + const_cast<void*>(static_cast<void const*>(baton->withXmp.c_str())), baton->withXmp.size()); + } // Number of channels used in output image baton->channels = image.bands(); baton->width = image.width(); baton->height = image.height(); - bool const supportsGifOutput = vips_type_find("VipsOperation", "magicksave") != 0 && - vips_type_find("VipsOperation", "magicksave_buffer") != 0; - image = sharp::SetAnimationProperties( - image, - baton->pageHeight, - baton->delay, - baton->loop); + image, nPages, targetPageHeight, baton->delay, baton->loop); + + if (image.get_typeof(VIPS_META_PAGE_HEIGHT) == G_TYPE_INT) { + baton->pageHeightOut = image.get_int(VIPS_META_PAGE_HEIGHT); + baton->pagesOut = image.get_int(VIPS_META_N_PAGES); + } // Output + sharp::SetTimeout(image, baton->timeoutSeconds); if (baton->fileOut.empty()) { // Buffer output if (baton->formatOut == "jpeg" || (baton->formatOut == "input" && inputImageType == sharp::ImageType::JPEG)) { // Write JPEG to buffer sharp::AssertImageTypeDimensions(image, sharp::ImageType::JPEG); VipsArea *area = reinterpret_cast<VipsArea*>(image.jpegsave_buffer(VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->jpegQuality) ->set("interlace", baton->jpegProgressive) ->set("subsample_mode", baton->jpegChromaSubsampling == "4:4:4" - ? VIPS_FOREIGN_JPEG_SUBSAMPLE_OFF - : VIPS_FOREIGN_JPEG_SUBSAMPLE_ON) + ? VIPS_FOREIGN_SUBSAMPLE_OFF + : VIPS_FOREIGN_SUBSAMPLE_ON) ->set("trellis_quant", baton->jpegTrellisQuantisation) ->set("quant_table", baton->jpegQuantisationTable) ->set("overshoot_deringing", baton->jpegOvershootDeringing) @@ -765,19 +907,35 @@ class PipelineWorker : public Napi::AsyncWorker { } else { baton->channels = std::min(baton->channels, 3); } + } else if (baton->formatOut == "jp2" || (baton->formatOut == "input" + && inputImageType == sharp::ImageType::JP2)) { + // Write JP2 to Buffer + sharp::AssertImageTypeDimensions(image, sharp::ImageType::JP2); + VipsArea *area = reinterpret_cast<VipsArea*>(image.jp2ksave_buffer(VImage::option() + ->set("Q", baton->jp2Quality) + ->set("lossless", baton->jp2Lossless) + ->set("subsample_mode", baton->jp2ChromaSubsampling == "4:4:4" + ? VIPS_FOREIGN_SUBSAMPLE_OFF : VIPS_FOREIGN_SUBSAMPLE_ON) + ->set("tile_height", baton->jp2TileHeight) + ->set("tile_width", baton->jp2TileWidth))); + baton->bufferOut = static_cast<char*>(area->data); + baton->bufferOutLength = area->length; + area->free_fn = nullptr; + vips_area_unref(area); + baton->formatOut = "jp2"; } else if (baton->formatOut == "png" || (baton->formatOut == "input" && - (inputImageType == sharp::ImageType::PNG || (inputImageType == sharp::ImageType::GIF && !supportsGifOutput) || - inputImageType == sharp::ImageType::SVG))) { + (inputImageType == sharp::ImageType::PNG || inputImageType == sharp::ImageType::SVG))) { // Write PNG to buffer sharp::AssertImageTypeDimensions(image, sharp::ImageType::PNG); VipsArea *area = reinterpret_cast<VipsArea*>(image.pngsave_buffer(VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("interlace", baton->pngProgressive) ->set("compression", baton->pngCompressionLevel) ->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE) ->set("palette", baton->pngPalette) ->set("Q", baton->pngQuality) - ->set("colours", baton->pngColours) + ->set("effort", baton->pngEffort) + ->set("bitdepth", sharp::Is16Bit(image.interpretation()) ? 16 : baton->pngBitdepth) ->set("dither", baton->pngDither))); baton->bufferOut = static_cast<char*>(area->data); baton->bufferOutLength = area->length; @@ -789,12 +947,16 @@ class PipelineWorker : public Napi::AsyncWorker { // Write WEBP to buffer sharp::AssertImageTypeDimensions(image, sharp::ImageType::WEBP); VipsArea *area = reinterpret_cast<VipsArea*>(image.webpsave_buffer(VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->webpQuality) ->set("lossless", baton->webpLossless) ->set("near_lossless", baton->webpNearLossless) ->set("smart_subsample", baton->webpSmartSubsample) - ->set("reduction_effort", baton->webpReductionEffort) + ->set("smart_deblock", baton->webpSmartDeblock) + ->set("preset", baton->webpPreset) + ->set("effort", baton->webpEffort) + ->set("min_size", baton->webpMinSize) + ->set("mixed", baton->webpMixed) ->set("alpha_q", baton->webpAlphaQuality))); baton->bufferOut = static_cast<char*>(area->data); baton->bufferOutLength = area->length; @@ -802,14 +964,19 @@ class PipelineWorker : public Napi::AsyncWorker { vips_area_unref(area); baton->formatOut = "webp"; } else if (baton->formatOut == "gif" || - (baton->formatOut == "input" && inputImageType == sharp::ImageType::GIF && supportsGifOutput)) { + (baton->formatOut == "input" && inputImageType == sharp::ImageType::GIF)) { // Write GIF to buffer sharp::AssertImageTypeDimensions(image, sharp::ImageType::GIF); - 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"))); + VipsArea *area = reinterpret_cast<VipsArea*>(image.gifsave_buffer(VImage::option() + ->set("keep", baton->keepMetadata) + ->set("bitdepth", baton->gifBitdepth) + ->set("effort", baton->gifEffort) + ->set("reuse", baton->gifReuse) + ->set("interlace", baton->gifProgressive) + ->set("interframe_maxerror", baton->gifInterFrameMaxError) + ->set("interpalette_maxerror", baton->gifInterPaletteMaxError) + ->set("keep_duplicate_frames", baton->gifKeepDuplicateFrames) + ->set("dither", baton->gifDither))); baton->bufferOut = static_cast<char*>(area->data); baton->bufferOutLength = area->length; area->free_fn = nullptr; @@ -827,17 +994,20 @@ class PipelineWorker : public Napi::AsyncWorker { image = image.cast(VIPS_FORMAT_FLOAT); } VipsArea *area = reinterpret_cast<VipsArea*>(image.tiffsave_buffer(VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->tiffQuality) ->set("bitdepth", baton->tiffBitdepth) ->set("compression", baton->tiffCompression) + ->set("bigtiff", baton->tiffBigtiff) + ->set("miniswhite", baton->tiffMiniswhite) ->set("predictor", baton->tiffPredictor) ->set("pyramid", baton->tiffPyramid) ->set("tile", baton->tiffTile) ->set("tile_height", baton->tiffTileHeight) ->set("tile_width", baton->tiffTileWidth) ->set("xres", baton->tiffXres) - ->set("yres", baton->tiffYres))); + ->set("yres", baton->tiffYres) + ->set("resunit", baton->tiffResolutionUnit))); baton->bufferOut = static_cast<char*>(area->data); baton->bufferOutLength = area->length; area->free_fn = nullptr; @@ -846,21 +1016,51 @@ class PipelineWorker : public Napi::AsyncWorker { } else if (baton->formatOut == "heif" || (baton->formatOut == "input" && inputImageType == sharp::ImageType::HEIF)) { // Write HEIF to buffer + sharp::AssertImageTypeDimensions(image, sharp::ImageType::HEIF); + image = sharp::RemoveAnimationProperties(image); VipsArea *area = reinterpret_cast<VipsArea*>(image.heifsave_buffer(VImage::option() - ->set("strip", !baton->withMetadata) - ->set("compression", baton->heifCompression) + ->set("keep", baton->keepMetadata) ->set("Q", baton->heifQuality) - ->set("speed", baton->heifSpeed) -#if defined(VIPS_TYPE_FOREIGN_SUBSAMPLE) + ->set("compression", baton->heifCompression) + ->set("effort", baton->heifEffort) + ->set("bitdepth", baton->heifBitdepth) ->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; area->free_fn = nullptr; vips_area_unref(area); baton->formatOut = "heif"; + } else if (baton->formatOut == "dz") { + // Write DZ to buffer + baton->tileContainer = VIPS_FOREIGN_DZ_CONTAINER_ZIP; + if (!image.has_alpha()) { + baton->tileBackground.pop_back(); + } + image = sharp::StaySequential(image, baton->tileAngle != 0); + vips::VOption *options = BuildOptionsDZ(baton); + VipsArea *area = reinterpret_cast<VipsArea*>(image.dzsave_buffer(options)); + baton->bufferOut = static_cast<char*>(area->data); + baton->bufferOutLength = area->length; + area->free_fn = nullptr; + vips_area_unref(area); + baton->formatOut = "dz"; + } else if (baton->formatOut == "jxl" || + (baton->formatOut == "input" && inputImageType == sharp::ImageType::JXL)) { + // Write JXL to buffer + image = sharp::RemoveAnimationProperties(image); + VipsArea *area = reinterpret_cast<VipsArea*>(image.jxlsave_buffer(VImage::option() + ->set("keep", baton->keepMetadata) + ->set("distance", baton->jxlDistance) + ->set("tier", baton->jxlDecodingTier) + ->set("effort", baton->jxlEffort) + ->set("lossless", baton->jxlLossless))); + baton->bufferOut = static_cast<char*>(area->data); + baton->bufferOutLength = area->length; + area->free_fn = nullptr; + vips_area_unref(area); + baton->formatOut = "jxl"; } else if (baton->formatOut == "raw" || (baton->formatOut == "input" && inputImageType == sharp::ImageType::RAW)) { // Write raw, uncompressed image data to buffer @@ -869,9 +1069,9 @@ class PipelineWorker : public Napi::AsyncWorker { image = image[0]; baton->channels = 1; } - if (image.format() != VIPS_FORMAT_UCHAR) { - // Cast pixels to uint8 (unsigned char) - image = image.cast(VIPS_FORMAT_UCHAR); + if (image.format() != baton->rawDepth) { + // Cast pixels to requested format + image = image.cast(baton->rawDepth); } // Get raw image data baton->bufferOut = static_cast<char*>(image.write_to_memory(&baton->bufferOutLength)); @@ -884,6 +1084,7 @@ class PipelineWorker : public Napi::AsyncWorker { // Unsupported output format (baton->err).append("Unsupported output format "); if (baton->formatOut == "input") { + (baton->err).append("when trying to match input format of "); (baton->err).append(ImageTypeId(inputImageType)); } else { (baton->err).append(baton->formatOut); @@ -897,25 +1098,27 @@ class PipelineWorker : public Napi::AsyncWorker { bool const isWebp = sharp::IsWebp(baton->fileOut); bool const isGif = sharp::IsGif(baton->fileOut); bool const isTiff = sharp::IsTiff(baton->fileOut); + bool const isJp2 = sharp::IsJp2(baton->fileOut); bool const isHeif = sharp::IsHeif(baton->fileOut); + bool const isJxl = sharp::IsJxl(baton->fileOut); bool const isDz = sharp::IsDz(baton->fileOut); bool const isDzZip = sharp::IsDzZip(baton->fileOut); bool const isV = sharp::IsV(baton->fileOut); bool const mightMatchInput = baton->formatOut == "input"; bool const willMatchInput = mightMatchInput && - !(isJpeg || isPng || isWebp || isGif || isTiff || isHeif || isDz || isDzZip || isV); + !(isJpeg || isPng || isWebp || isGif || isTiff || isJp2 || isHeif || isDz || isDzZip || isV); if (baton->formatOut == "jpeg" || (mightMatchInput && isJpeg) || (willMatchInput && inputImageType == sharp::ImageType::JPEG)) { // Write JPEG to file sharp::AssertImageTypeDimensions(image, sharp::ImageType::JPEG); image.jpegsave(const_cast<char*>(baton->fileOut.data()), VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->jpegQuality) ->set("interlace", baton->jpegProgressive) ->set("subsample_mode", baton->jpegChromaSubsampling == "4:4:4" - ? VIPS_FOREIGN_JPEG_SUBSAMPLE_OFF - : VIPS_FOREIGN_JPEG_SUBSAMPLE_ON) + ? VIPS_FOREIGN_SUBSAMPLE_OFF + : VIPS_FOREIGN_SUBSAMPLE_ON) ->set("trellis_quant", baton->jpegTrellisQuantisation) ->set("quant_table", baton->jpegQuantisationTable) ->set("overshoot_deringing", baton->jpegOvershootDeringing) @@ -923,19 +1126,31 @@ class PipelineWorker : public Napi::AsyncWorker { ->set("optimize_coding", baton->jpegOptimiseCoding)); baton->formatOut = "jpeg"; baton->channels = std::min(baton->channels, 3); + } else if (baton->formatOut == "jp2" || (mightMatchInput && isJp2) || + (willMatchInput && (inputImageType == sharp::ImageType::JP2))) { + // Write JP2 to file + sharp::AssertImageTypeDimensions(image, sharp::ImageType::JP2); + image.jp2ksave(const_cast<char*>(baton->fileOut.data()), VImage::option() + ->set("Q", baton->jp2Quality) + ->set("lossless", baton->jp2Lossless) + ->set("subsample_mode", baton->jp2ChromaSubsampling == "4:4:4" + ? VIPS_FOREIGN_SUBSAMPLE_OFF : VIPS_FOREIGN_SUBSAMPLE_ON) + ->set("tile_height", baton->jp2TileHeight) + ->set("tile_width", baton->jp2TileWidth)); + baton->formatOut = "jp2"; } else if (baton->formatOut == "png" || (mightMatchInput && isPng) || (willMatchInput && - (inputImageType == sharp::ImageType::PNG || (inputImageType == sharp::ImageType::GIF && !supportsGifOutput) || - inputImageType == sharp::ImageType::SVG))) { + (inputImageType == sharp::ImageType::PNG || inputImageType == sharp::ImageType::SVG))) { // Write PNG to file sharp::AssertImageTypeDimensions(image, sharp::ImageType::PNG); image.pngsave(const_cast<char*>(baton->fileOut.data()), VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("interlace", baton->pngProgressive) ->set("compression", baton->pngCompressionLevel) ->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE) ->set("palette", baton->pngPalette) ->set("Q", baton->pngQuality) - ->set("colours", baton->pngColours) + ->set("bitdepth", sharp::Is16Bit(image.interpretation()) ? 16 : baton->pngBitdepth) + ->set("effort", baton->pngEffort) ->set("dither", baton->pngDither)); baton->formatOut = "png"; } else if (baton->formatOut == "webp" || (mightMatchInput && isWebp) || @@ -943,23 +1158,32 @@ class PipelineWorker : public Napi::AsyncWorker { // Write WEBP to file sharp::AssertImageTypeDimensions(image, sharp::ImageType::WEBP); image.webpsave(const_cast<char*>(baton->fileOut.data()), VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->webpQuality) ->set("lossless", baton->webpLossless) ->set("near_lossless", baton->webpNearLossless) ->set("smart_subsample", baton->webpSmartSubsample) - ->set("reduction_effort", baton->webpReductionEffort) + ->set("smart_deblock", baton->webpSmartDeblock) + ->set("preset", baton->webpPreset) + ->set("effort", baton->webpEffort) + ->set("min_size", baton->webpMinSize) + ->set("mixed", baton->webpMixed) ->set("alpha_q", baton->webpAlphaQuality)); baton->formatOut = "webp"; } else if (baton->formatOut == "gif" || (mightMatchInput && isGif) || - (willMatchInput && inputImageType == sharp::ImageType::GIF && supportsGifOutput)) { + (willMatchInput && inputImageType == sharp::ImageType::GIF)) { // Write GIF to file 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")); + image.gifsave(const_cast<char*>(baton->fileOut.data()), VImage::option() + ->set("keep", baton->keepMetadata) + ->set("bitdepth", baton->gifBitdepth) + ->set("effort", baton->gifEffort) + ->set("reuse", baton->gifReuse) + ->set("interlace", baton->gifProgressive) + ->set("interframe_maxerror", baton->gifInterFrameMaxError) + ->set("interpalette_maxerror", baton->gifInterPaletteMaxError) + ->set("keep_duplicate_frames", baton->gifKeepDuplicateFrames) + ->set("dither", baton->gifDither)); baton->formatOut = "gif"; } else if (baton->formatOut == "tiff" || (mightMatchInput && isTiff) || (willMatchInput && inputImageType == sharp::ImageType::TIFF)) { @@ -973,98 +1197,64 @@ class PipelineWorker : public Napi::AsyncWorker { image = image.cast(VIPS_FORMAT_FLOAT); } image.tiffsave(const_cast<char*>(baton->fileOut.data()), VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->tiffQuality) ->set("bitdepth", baton->tiffBitdepth) ->set("compression", baton->tiffCompression) + ->set("bigtiff", baton->tiffBigtiff) + ->set("miniswhite", baton->tiffMiniswhite) ->set("predictor", baton->tiffPredictor) ->set("pyramid", baton->tiffPyramid) ->set("tile", baton->tiffTile) ->set("tile_height", baton->tiffTileHeight) ->set("tile_width", baton->tiffTileWidth) ->set("xres", baton->tiffXres) - ->set("yres", baton->tiffYres)); + ->set("yres", baton->tiffYres) + ->set("resunit", baton->tiffResolutionUnit)); baton->formatOut = "tiff"; } else if (baton->formatOut == "heif" || (mightMatchInput && isHeif) || (willMatchInput && inputImageType == sharp::ImageType::HEIF)) { // Write HEIF to file + sharp::AssertImageTypeDimensions(image, sharp::ImageType::HEIF); + image = sharp::RemoveAnimationProperties(image); image.heifsave(const_cast<char*>(baton->fileOut.data()), VImage::option() - ->set("strip", !baton->withMetadata) + ->set("keep", baton->keepMetadata) ->set("Q", baton->heifQuality) ->set("compression", baton->heifCompression) - ->set("speed", baton->heifSpeed) -#if defined(VIPS_TYPE_FOREIGN_SUBSAMPLE) + ->set("effort", baton->heifEffort) + ->set("bitdepth", baton->heifBitdepth) ->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 == "jxl" || (mightMatchInput && isJxl) || + (willMatchInput && inputImageType == sharp::ImageType::JXL)) { + // Write JXL to file + image = sharp::RemoveAnimationProperties(image); + image.jxlsave(const_cast<char*>(baton->fileOut.data()), VImage::option() + ->set("keep", baton->keepMetadata) + ->set("distance", baton->jxlDistance) + ->set("tier", baton->jxlDecodingTier) + ->set("effort", baton->jxlEffort) + ->set("lossless", baton->jxlLossless)); + baton->formatOut = "jxl"; } else if (baton->formatOut == "dz" || isDz || isDzZip) { + // Write DZ to file if (isDzZip) { baton->tileContainer = VIPS_FOREIGN_DZ_CONTAINER_ZIP; } - // Forward format options through suffix - std::string suffix; - if (baton->tileFormat == "png") { - std::vector<std::pair<std::string, std::string>> options { - {"interlace", baton->pngProgressive ? "TRUE" : "FALSE"}, - {"compression", std::to_string(baton->pngCompressionLevel)}, - {"filter", baton->pngAdaptiveFiltering ? "all" : "none"} - }; - suffix = AssembleSuffixString(".png", options); - } else if (baton->tileFormat == "webp") { - std::vector<std::pair<std::string, std::string>> options { - {"Q", std::to_string(baton->webpQuality)}, - {"alpha_q", std::to_string(baton->webpAlphaQuality)}, - {"lossless", baton->webpLossless ? "TRUE" : "FALSE"}, - {"near_lossless", baton->webpNearLossless ? "TRUE" : "FALSE"}, - {"smart_subsample", baton->webpSmartSubsample ? "TRUE" : "FALSE"}, - {"reduction_effort", std::to_string(baton->webpReductionEffort)} - }; - suffix = AssembleSuffixString(".webp", options); - } else { - std::vector<std::pair<std::string, std::string>> options { - {"Q", std::to_string(baton->jpegQuality)}, - {"interlace", baton->jpegProgressive ? "TRUE" : "FALSE"}, - {"subsample_mode", baton->jpegChromaSubsampling == "4:4:4" ? "off" : "on"}, - {"trellis_quant", baton->jpegTrellisQuantisation ? "TRUE" : "FALSE"}, - {"quant_table", std::to_string(baton->jpegQuantisationTable)}, - {"overshoot_deringing", baton->jpegOvershootDeringing ? "TRUE": "FALSE"}, - {"optimize_scans", baton->jpegOptimiseScans ? "TRUE": "FALSE"}, - {"optimize_coding", baton->jpegOptimiseCoding ? "TRUE": "FALSE"} - }; - std::string extname = baton->tileLayout == VIPS_FOREIGN_DZ_LAYOUT_DZ ? ".jpeg" : ".jpg"; - suffix = AssembleSuffixString(extname, options); - } - // Remove alpha channel from tile background if image does not contain an alpha channel - if (!sharp::HasAlpha(image)) { + if (!image.has_alpha()) { baton->tileBackground.pop_back(); } - // Write DZ to file - vips::VOption *options = VImage::option() - ->set("strip", !baton->withMetadata) - ->set("tile_size", baton->tileSize) - ->set("overlap", baton->tileOverlap) - ->set("container", baton->tileContainer) - ->set("layout", baton->tileLayout) - ->set("suffix", const_cast<char*>(suffix.data())) - ->set("angle", CalculateAngleRotation(baton->tileAngle)) - ->set("background", baton->tileBackground) - ->set("centre", baton->tileCentre) - ->set("id", const_cast<char*>(baton->tileId.data())) - ->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 - if (baton->tileDepth < VIPS_FOREIGN_DZ_DEPTH_LAST) { - options->set("depth", baton->tileDepth); - } + image = sharp::StaySequential(image, baton->tileAngle != 0); + vips::VOption *options = BuildOptionsDZ(baton); image.dzsave(const_cast<char*>(baton->fileOut.data()), options); baton->formatOut = "dz"; } else if (baton->formatOut == "v" || (mightMatchInput && isV) || (willMatchInput && inputImageType == sharp::ImageType::VIPS)) { // Write V to file image.vipssave(const_cast<char*>(baton->fileOut.data()), VImage::option() - ->set("strip", !baton->withMetadata)); + ->set("keep", baton->keepMetadata)); baton->formatOut = "v"; } else { // Unsupported output format @@ -1077,7 +1267,12 @@ class PipelineWorker : public Napi::AsyncWorker { if (what && what[0]) { (baton->err).append(what); } else { - (baton->err).append("Unknown error"); + if (baton->input->failOn == VIPS_FAIL_ON_WARNING) { + (baton->err).append("Warning treated as error due to failOn setting"); + baton->errUseWarning = true; + } else { + (baton->err).append("Unknown error"); + } } } // Clean up libvips' per-request data and threads @@ -1092,7 +1287,11 @@ class PipelineWorker : public Napi::AsyncWorker { // Handle warnings std::string warning = sharp::VipsWarningPop(); while (!warning.empty()) { - debuglog.Call({ Napi::String::New(env, warning) }); + if (baton->errUseWarning) { + (baton->err).append("\n").append(warning); + } else { + debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) }); + } warning = sharp::VipsWarningPop(); } @@ -1113,33 +1312,50 @@ class PipelineWorker : public Napi::AsyncWorker { info.Set("width", static_cast<uint32_t>(width)); info.Set("height", static_cast<uint32_t>(height)); info.Set("channels", static_cast<uint32_t>(baton->channels)); + if (baton->formatOut == "raw") { + info.Set("depth", vips_enum_nick(VIPS_TYPE_BAND_FORMAT, baton->rawDepth)); + } info.Set("premultiplied", baton->premultiplied); if (baton->hasCropOffset) { info.Set("cropOffsetLeft", static_cast<int32_t>(baton->cropOffsetLeft)); info.Set("cropOffsetTop", static_cast<int32_t>(baton->cropOffsetTop)); } - if (baton->trimThreshold > 0.0) { + if (baton->hasAttentionCenter) { + info.Set("attentionX", static_cast<int32_t>(baton->attentionX)); + info.Set("attentionY", static_cast<int32_t>(baton->attentionY)); + } + if (baton->trimThreshold >= 0.0) { info.Set("trimOffsetLeft", static_cast<int32_t>(baton->trimOffsetLeft)); info.Set("trimOffsetTop", static_cast<int32_t>(baton->trimOffsetTop)); } + if (baton->input->textAutofitDpi) { + info.Set("textAutofitDpi", static_cast<uint32_t>(baton->input->textAutofitDpi)); + } + if (baton->pageHeightOut) { + info.Set("pageHeight", static_cast<int32_t>(baton->pageHeightOut)); + info.Set("pages", static_cast<int32_t>(baton->pagesOut)); + } if (baton->bufferOutLength > 0) { // Add buffer size to info info.Set("size", static_cast<uint32_t>(baton->bufferOutLength)); // Pass ownership of output data to Buffer instance - Napi::Buffer<char> data = Napi::Buffer<char>::New(env, static_cast<char*>(baton->bufferOut), + Napi::Buffer<char> data = Napi::Buffer<char>::NewOrCopy(env, static_cast<char*>(baton->bufferOut), baton->bufferOutLength, sharp::FreeCallback); - Callback().MakeCallback(Receiver().Value(), { env.Null(), data, info }); + Callback().Call(Receiver().Value(), { env.Null(), data, info }); } else { // Add file size to info - struct STAT64_STRUCT st; - if (STAT64_FUNCTION(baton->fileOut.data(), &st) == 0) { - info.Set("size", static_cast<uint32_t>(st.st_size)); + if (baton->formatOut != "dz" || sharp::IsDzZip(baton->fileOut)) { + try { + uint32_t const size = static_cast<uint32_t>( + std::filesystem::file_size(std::filesystem::u8path(baton->fileOut))); + info.Set("size", size); + } catch (...) {} } - Callback().MakeCallback(Receiver().Value(), { env.Null(), info }); + Callback().Call(Receiver().Value(), { env.Null(), info }); } } else { - Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, baton->err).Value() }); + Callback().Call(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() }); } // Delete baton @@ -1152,11 +1368,14 @@ class PipelineWorker : public Napi::AsyncWorker { for (sharp::InputDescriptor *input : baton->joinChannelIn) { delete input; } + for (sharp::InputDescriptor *input : baton->join) { + delete input; + } delete baton; // Decrement processing task counter - g_atomic_int_dec_and_test(&sharp::counterProcess); - Napi::Number queueLength = Napi::Number::New(env, static_cast<double>(sharp::counterQueue)); + sharp::counterProcess--; + Napi::Number queueLength = Napi::Number::New(env, static_cast<int>(sharp::counterQueue)); queueListener.Call(Receiver().Value(), { queueLength }); } @@ -1165,25 +1384,30 @@ class PipelineWorker : public Napi::AsyncWorker { Napi::FunctionReference debuglog; Napi::FunctionReference queueListener; + void MultiPageUnsupported(int const pages, std::string op) { + if (pages > 1) { + throw vips::VError(op + " is not supported for multi-page images"); + } + } + /* Calculate the angle of rotation and need-to-flip for the given Exif orientation By default, returns zero, i.e. no rotation. */ - std::tuple<VipsAngle, bool, bool> - CalculateExifRotationAndFlip(int const exifOrientation) { + std::tuple<VipsAngle, bool> + CalculateExifRotationAndFlop(int const exifOrientation) { VipsAngle rotate = VIPS_ANGLE_D0; - bool flip = FALSE; - bool flop = FALSE; + bool flop = false; switch (exifOrientation) { case 6: rotate = VIPS_ANGLE_D90; break; case 3: rotate = VIPS_ANGLE_D180; break; case 8: rotate = VIPS_ANGLE_D270; break; - case 2: flop = TRUE; break; // flop 1 - case 7: flip = TRUE; rotate = VIPS_ANGLE_D90; break; // flip 6 - case 4: flop = TRUE; rotate = VIPS_ANGLE_D180; break; // flop 3 - case 5: flip = TRUE; rotate = VIPS_ANGLE_D270; break; // flip 8 + case 2: flop = true; break; + case 7: flop = true; rotate = VIPS_ANGLE_D270; break; + case 4: flop = true; rotate = VIPS_ANGLE_D180; break; + case 5: flop = true; rotate = VIPS_ANGLE_D90; break; } - return std::make_tuple(rotate, flip, flop); + return std::make_tuple(rotate, flop); } /* @@ -1205,21 +1429,84 @@ class PipelineWorker : public Napi::AsyncWorker { /* Assemble the suffix argument to dzsave, which is the format (by extname) - alongisde comma-separated arguments to the corresponding `formatsave` vips + alongside comma-separated arguments to the corresponding `formatsave` vips action. */ std::string AssembleSuffixString(std::string extname, std::vector<std::pair<std::string, std::string>> options) { std::string argument; - for (auto const &option : options) { + for (const auto& [key, value] : options) { if (!argument.empty()) { argument += ","; } - argument += option.first + "=" + option.second; + argument += key + "=" + value; } return extname + "[" + argument + "]"; } + /* + Build VOption for dzsave + */ + vips::VOption* + BuildOptionsDZ(PipelineBaton *baton) { + // Forward format options through suffix + std::string suffix; + if (baton->tileFormat == "png") { + std::vector<std::pair<std::string, std::string>> options { + {"interlace", baton->pngProgressive ? "true" : "false"}, + {"compression", std::to_string(baton->pngCompressionLevel)}, + {"filter", baton->pngAdaptiveFiltering ? "all" : "none"} + }; + suffix = AssembleSuffixString(".png", options); + } else if (baton->tileFormat == "webp") { + std::vector<std::pair<std::string, std::string>> options { + {"Q", std::to_string(baton->webpQuality)}, + {"alpha_q", std::to_string(baton->webpAlphaQuality)}, + {"lossless", baton->webpLossless ? "true" : "false"}, + {"near_lossless", baton->webpNearLossless ? "true" : "false"}, + {"smart_subsample", baton->webpSmartSubsample ? "true" : "false"}, + {"smart_deblock", baton->webpSmartDeblock ? "true" : "false"}, + {"preset", vips_enum_nick(VIPS_TYPE_FOREIGN_WEBP_PRESET, baton->webpPreset)}, + {"min_size", baton->webpMinSize ? "true" : "false"}, + {"mixed", baton->webpMixed ? "true" : "false"}, + {"effort", std::to_string(baton->webpEffort)} + }; + suffix = AssembleSuffixString(".webp", options); + } else { + std::vector<std::pair<std::string, std::string>> options { + {"Q", std::to_string(baton->jpegQuality)}, + {"interlace", baton->jpegProgressive ? "true" : "false"}, + {"subsample_mode", baton->jpegChromaSubsampling == "4:4:4" ? "off" : "on"}, + {"trellis_quant", baton->jpegTrellisQuantisation ? "true" : "false"}, + {"quant_table", std::to_string(baton->jpegQuantisationTable)}, + {"overshoot_deringing", baton->jpegOvershootDeringing ? "true": "false"}, + {"optimize_scans", baton->jpegOptimiseScans ? "true": "false"}, + {"optimize_coding", baton->jpegOptimiseCoding ? "true": "false"} + }; + std::string extname = baton->tileLayout == VIPS_FOREIGN_DZ_LAYOUT_DZ ? ".jpeg" : ".jpg"; + suffix = AssembleSuffixString(extname, options); + } + vips::VOption *options = VImage::option() + ->set("keep", baton->keepMetadata) + ->set("tile_size", baton->tileSize) + ->set("overlap", baton->tileOverlap) + ->set("container", baton->tileContainer) + ->set("layout", baton->tileLayout) + ->set("suffix", const_cast<char*>(suffix.data())) + ->set("angle", CalculateAngleRotation(baton->tileAngle)) + ->set("background", baton->tileBackground) + ->set("centre", baton->tileCentre) + ->set("id", const_cast<char*>(baton->tileId.data())) + ->set("skip_blanks", baton->tileSkipBlanks); + if (baton->tileDepth < VIPS_FOREIGN_DZ_DEPTH_LAST) { + options->set("depth", baton->tileDepth); + } + if (!baton->tileBasename.empty()) { + options->set("basename", const_cast<char*>(baton->tileBasename.data())); + } + return options; + } + /* Clear all thread-local data. */ @@ -1236,10 +1523,18 @@ class PipelineWorker : public Napi::AsyncWorker { Napi::Value pipeline(const Napi::CallbackInfo& info) { // V8 objects are converted to non-V8 types held in the baton struct PipelineBaton *baton = new PipelineBaton; - Napi::Object options = info[0].As<Napi::Object>(); + Napi::Object options = info[size_t(0)].As<Napi::Object>(); // Input baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>()); + // Join images together + if (sharp::HasAttr(options, "join")) { + Napi::Array join = options.Get("join").As<Napi::Array>(); + for (unsigned int i = 0; i < join.Length(); i++) { + baton->join.push_back( + sharp::CreateInputDescriptor(join.Get(i).As<Napi::Object>())); + } + } // Extract image options baton->topOffsetPre = sharp::AttrAsInt32(options, "topOffsetPre"); baton->leftOffsetPre = sharp::AttrAsInt32(options, "leftOffsetPre"); @@ -1255,27 +1550,23 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { // Canvas option std::string canvas = sharp::AttrAsStr(options, "canvas"); if (canvas == "crop") { - baton->canvas = Canvas::CROP; + baton->canvas = sharp::Canvas::CROP; } else if (canvas == "embed") { - baton->canvas = Canvas::EMBED; + baton->canvas = sharp::Canvas::EMBED; } else if (canvas == "max") { - baton->canvas = Canvas::MAX; + baton->canvas = sharp::Canvas::MAX; } else if (canvas == "min") { - baton->canvas = Canvas::MIN; + baton->canvas = sharp::Canvas::MIN; } else if (canvas == "ignore_aspect") { - baton->canvas = Canvas::IGNORE_ASPECT; + baton->canvas = sharp::Canvas::IGNORE_ASPECT; } - // Tint chroma - baton->tintA = sharp::AttrAsDouble(options, "tintA"); - baton->tintB = sharp::AttrAsDouble(options, "tintB"); // Composite Napi::Array compositeArray = options.Get("composite").As<Napi::Array>(); for (unsigned int i = 0; i < compositeArray.Length(); i++) { Napi::Object compositeObject = compositeArray.Get(i).As<Napi::Object>(); Composite *composite = new Composite; composite->input = sharp::CreateInputDescriptor(compositeObject.Get("input").As<Napi::Object>()); - composite->mode = static_cast<VipsBlendMode>( - vips_enum_from_nick(nullptr, VIPS_TYPE_BLEND_MODE, sharp::AttrAsStr(compositeObject, "blend").data())); + composite->mode = sharp::AttrAsEnum<VipsBlendMode>(compositeObject, "blend", VIPS_TYPE_BLEND_MODE); composite->gravity = sharp::AttrAsUint32(compositeObject, "gravity"); composite->left = sharp::AttrAsInt32(compositeObject, "left"); composite->top = sharp::AttrAsInt32(compositeObject, "top"); @@ -1286,9 +1577,10 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { } // Resize options baton->withoutEnlargement = sharp::AttrAsBool(options, "withoutEnlargement"); + baton->withoutReduction = sharp::AttrAsBool(options, "withoutReduction"); baton->position = sharp::AttrAsInt32(options, "position"); baton->resizeBackground = sharp::AttrAsVectorOfDouble(options, "resizeBackground"); - baton->kernel = sharp::AttrAsStr(options, "kernel"); + baton->kernel = sharp::AttrAsEnum<VipsKernel>(options, "kernel", VIPS_TYPE_KERNEL); baton->fastShrinkOnLoad = sharp::AttrAsBool(options, "fastShrinkOnLoad"); // Join Channel Options if (options.Has("joinChannelIn")) { @@ -1301,29 +1593,47 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { // Operators baton->flatten = sharp::AttrAsBool(options, "flatten"); baton->flattenBackground = sharp::AttrAsVectorOfDouble(options, "flattenBackground"); + baton->unflatten = sharp::AttrAsBool(options, "unflatten"); baton->negate = sharp::AttrAsBool(options, "negate"); + baton->negateAlpha = sharp::AttrAsBool(options, "negateAlpha"); baton->blurSigma = sharp::AttrAsDouble(options, "blurSigma"); + baton->precision = sharp::AttrAsEnum<VipsPrecision>(options, "precision", VIPS_TYPE_PRECISION); + baton->minAmpl = sharp::AttrAsDouble(options, "minAmpl"); baton->brightness = sharp::AttrAsDouble(options, "brightness"); baton->saturation = sharp::AttrAsDouble(options, "saturation"); baton->hue = sharp::AttrAsInt32(options, "hue"); + baton->lightness = sharp::AttrAsDouble(options, "lightness"); baton->medianSize = sharp::AttrAsUint32(options, "medianSize"); baton->sharpenSigma = sharp::AttrAsDouble(options, "sharpenSigma"); - baton->sharpenFlat = sharp::AttrAsDouble(options, "sharpenFlat"); - baton->sharpenJagged = sharp::AttrAsDouble(options, "sharpenJagged"); + baton->sharpenM1 = sharp::AttrAsDouble(options, "sharpenM1"); + baton->sharpenM2 = sharp::AttrAsDouble(options, "sharpenM2"); + baton->sharpenX1 = sharp::AttrAsDouble(options, "sharpenX1"); + baton->sharpenY2 = sharp::AttrAsDouble(options, "sharpenY2"); + baton->sharpenY3 = sharp::AttrAsDouble(options, "sharpenY3"); baton->threshold = sharp::AttrAsInt32(options, "threshold"); baton->thresholdGrayscale = sharp::AttrAsBool(options, "thresholdGrayscale"); + baton->trimBackground = sharp::AttrAsVectorOfDouble(options, "trimBackground"); baton->trimThreshold = sharp::AttrAsDouble(options, "trimThreshold"); + baton->trimLineArt = sharp::AttrAsBool(options, "trimLineArt"); baton->gamma = sharp::AttrAsDouble(options, "gamma"); baton->gammaOut = sharp::AttrAsDouble(options, "gammaOut"); - baton->linearA = sharp::AttrAsDouble(options, "linearA"); - baton->linearB = sharp::AttrAsDouble(options, "linearB"); + baton->linearA = sharp::AttrAsVectorOfDouble(options, "linearA"); + baton->linearB = sharp::AttrAsVectorOfDouble(options, "linearB"); + baton->dilateWidth = sharp::AttrAsUint32(options, "dilateWidth"); + baton->erodeWidth = sharp::AttrAsUint32(options, "erodeWidth"); baton->greyscale = sharp::AttrAsBool(options, "greyscale"); baton->normalise = sharp::AttrAsBool(options, "normalise"); - baton->useExifOrientation = sharp::AttrAsBool(options, "useExifOrientation"); + baton->normaliseLower = sharp::AttrAsUint32(options, "normaliseLower"); + baton->normaliseUpper = sharp::AttrAsUint32(options, "normaliseUpper"); + baton->tint = sharp::AttrAsVectorOfDouble(options, "tint"); + baton->claheWidth = sharp::AttrAsUint32(options, "claheWidth"); + baton->claheHeight = sharp::AttrAsUint32(options, "claheHeight"); + baton->claheMaxSlope = sharp::AttrAsUint32(options, "claheMaxSlope"); baton->angle = sharp::AttrAsInt32(options, "angle"); baton->rotationAngle = sharp::AttrAsDouble(options, "rotationAngle"); baton->rotationBackground = sharp::AttrAsVectorOfDouble(options, "rotationBackground"); - baton->rotateBeforePreExtract = sharp::AttrAsBool(options, "rotateBeforePreExtract"); + baton->rotateBefore = sharp::AttrAsBool(options, "rotateBefore"); + baton->orientBefore = sharp::AttrAsBool(options, "orientBefore"); baton->flip = sharp::AttrAsBool(options, "flip"); baton->flop = sharp::AttrAsBool(options, "flop"); baton->extendTop = sharp::AttrAsInt32(options, "extendTop"); @@ -1331,6 +1641,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { baton->extendLeft = sharp::AttrAsInt32(options, "extendLeft"); baton->extendRight = sharp::AttrAsInt32(options, "extendRight"); baton->extendBackground = sharp::AttrAsVectorOfDouble(options, "extendBackground"); + baton->extendWith = sharp::AttrAsEnum<VipsExtend>(options, "extendWith", VIPS_TYPE_EXTEND); baton->extractChannel = sharp::AttrAsInt32(options, "extractChannel"); baton->affineMatrix = sharp::AttrAsVectorOfDouble(options, "affineMatrix"); baton->affineBackground = sharp::AttrAsVectorOfDouble(options, "affineBackground"); @@ -1338,16 +1649,15 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { 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->affineInterpolator = sharp::AttrAsStr(options, "affineInterpolator"); baton->removeAlpha = sharp::AttrAsBool(options, "removeAlpha"); - baton->ensureAlpha = sharp::AttrAsBool(options, "ensureAlpha"); + baton->ensureAlpha = sharp::AttrAsDouble(options, "ensureAlpha"); if (options.Has("boolean")) { baton->boolean = sharp::CreateInputDescriptor(options.Get("boolean").As<Napi::Object>()); - baton->booleanOp = sharp::GetBooleanOperation(sharp::AttrAsStr(options, "booleanOp")); + baton->booleanOp = sharp::AttrAsEnum<VipsOperationBoolean>(options, "booleanOp", VIPS_TYPE_OPERATION_BOOLEAN); } if (options.Has("bandBoolOp")) { - baton->bandBoolOp = sharp::GetBooleanOperation(sharp::AttrAsStr(options, "bandBoolOp")); + baton->bandBoolOp = sharp::AttrAsEnum<VipsOperationBoolean>(options, "bandBoolOp", VIPS_TYPE_OPERATION_BOOLEAN); } if (options.Has("convKernel")) { Napi::Object kernel = options.Get("convKernel").As<Napi::Object>(); @@ -1356,29 +1666,49 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { baton->convKernelScale = sharp::AttrAsDouble(kernel, "scale"); baton->convKernelOffset = sharp::AttrAsDouble(kernel, "offset"); size_t const kernelSize = static_cast<size_t>(baton->convKernelWidth * baton->convKernelHeight); - baton->convKernel = std::unique_ptr<double[]>(new double[kernelSize]); + baton->convKernel.resize(kernelSize); Napi::Array kdata = kernel.Get("kernel").As<Napi::Array>(); for (unsigned int i = 0; i < kernelSize; i++) { baton->convKernel[i] = sharp::AttrAsDouble(kdata, i); } } if (options.Has("recombMatrix")) { - baton->recombMatrix = std::unique_ptr<double[]>(new double[9]); Napi::Array recombMatrix = options.Get("recombMatrix").As<Napi::Array>(); - for (unsigned int i = 0; i < 9; i++) { - baton->recombMatrix[i] = sharp::AttrAsDouble(recombMatrix, i); + unsigned int matrixElements = recombMatrix.Length(); + baton->recombMatrix.resize(matrixElements); + for (unsigned int i = 0; i < matrixElements; i++) { + baton->recombMatrix[i] = sharp::AttrAsDouble(recombMatrix, i); } } - baton->colourspace = sharp::GetInterpretation(sharp::AttrAsStr(options, "colourspace")); + baton->colourspacePipeline = sharp::AttrAsEnum<VipsInterpretation>( + options, "colourspacePipeline", VIPS_TYPE_INTERPRETATION); + if (baton->colourspacePipeline == VIPS_INTERPRETATION_ERROR) { + baton->colourspacePipeline = VIPS_INTERPRETATION_LAST; + } + baton->colourspace = sharp::AttrAsEnum<VipsInterpretation>(options, "colourspace", VIPS_TYPE_INTERPRETATION); if (baton->colourspace == VIPS_INTERPRETATION_ERROR) { baton->colourspace = VIPS_INTERPRETATION_sRGB; } // Output baton->formatOut = sharp::AttrAsStr(options, "formatOut"); baton->fileOut = sharp::AttrAsStr(options, "fileOut"); - baton->withMetadata = sharp::AttrAsBool(options, "withMetadata"); + baton->keepMetadata = sharp::AttrAsUint32(options, "keepMetadata"); baton->withMetadataOrientation = sharp::AttrAsUint32(options, "withMetadataOrientation"); - baton->withMetadataIcc = sharp::AttrAsStr(options, "withMetadataIcc"); + baton->withMetadataDensity = sharp::AttrAsDouble(options, "withMetadataDensity"); + baton->withIccProfile = sharp::AttrAsStr(options, "withIccProfile"); + Napi::Object withExif = options.Get("withExif").As<Napi::Object>(); + Napi::Array withExifKeys = withExif.GetPropertyNames(); + for (unsigned int i = 0; i < withExifKeys.Length(); i++) { + std::string k = sharp::AttrAsStr(withExifKeys, i); + if (withExif.HasOwnProperty(k)) { + baton->withExif.insert(std::make_pair(k, sharp::AttrAsStr(withExif, k))); + } + } + baton->withExifMerge = sharp::AttrAsBool(options, "withExifMerge"); + baton->withXmp = sharp::AttrAsStr(options, "withXmp"); + baton->timeoutSeconds = sharp::AttrAsUint32(options, "timeoutSeconds"); + baton->loop = sharp::AttrAsUint32(options, "loop"); + baton->delay = sharp::AttrAsInt32Vector(options, "delay"); // Format-specific baton->jpegQuality = sharp::AttrAsUint32(options, "jpegQuality"); baton->jpegProgressive = sharp::AttrAsBool(options, "jpegProgressive"); @@ -1393,80 +1723,76 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { baton->pngAdaptiveFiltering = sharp::AttrAsBool(options, "pngAdaptiveFiltering"); baton->pngPalette = sharp::AttrAsBool(options, "pngPalette"); baton->pngQuality = sharp::AttrAsUint32(options, "pngQuality"); - baton->pngColours = sharp::AttrAsUint32(options, "pngColours"); + baton->pngEffort = sharp::AttrAsUint32(options, "pngEffort"); + baton->pngBitdepth = sharp::AttrAsUint32(options, "pngBitdepth"); baton->pngDither = sharp::AttrAsDouble(options, "pngDither"); + baton->jp2Quality = sharp::AttrAsUint32(options, "jp2Quality"); + baton->jp2Lossless = sharp::AttrAsBool(options, "jp2Lossless"); + baton->jp2TileHeight = sharp::AttrAsUint32(options, "jp2TileHeight"); + baton->jp2TileWidth = sharp::AttrAsUint32(options, "jp2TileWidth"); + baton->jp2ChromaSubsampling = sharp::AttrAsStr(options, "jp2ChromaSubsampling"); baton->webpQuality = sharp::AttrAsUint32(options, "webpQuality"); baton->webpAlphaQuality = sharp::AttrAsUint32(options, "webpAlphaQuality"); baton->webpLossless = sharp::AttrAsBool(options, "webpLossless"); baton->webpNearLossless = sharp::AttrAsBool(options, "webpNearLossless"); baton->webpSmartSubsample = sharp::AttrAsBool(options, "webpSmartSubsample"); - baton->webpReductionEffort = sharp::AttrAsUint32(options, "webpReductionEffort"); + baton->webpSmartDeblock = sharp::AttrAsBool(options, "webpSmartDeblock"); + baton->webpPreset = sharp::AttrAsEnum<VipsForeignWebpPreset>(options, "webpPreset", VIPS_TYPE_FOREIGN_WEBP_PRESET); + baton->webpEffort = sharp::AttrAsUint32(options, "webpEffort"); + baton->webpMinSize = sharp::AttrAsBool(options, "webpMinSize"); + baton->webpMixed = sharp::AttrAsBool(options, "webpMixed"); + baton->gifBitdepth = sharp::AttrAsUint32(options, "gifBitdepth"); + baton->gifEffort = sharp::AttrAsUint32(options, "gifEffort"); + baton->gifDither = sharp::AttrAsDouble(options, "gifDither"); + baton->gifInterFrameMaxError = sharp::AttrAsDouble(options, "gifInterFrameMaxError"); + baton->gifInterPaletteMaxError = sharp::AttrAsDouble(options, "gifInterPaletteMaxError"); + baton->gifKeepDuplicateFrames = sharp::AttrAsBool(options, "gifKeepDuplicateFrames"); + baton->gifReuse = sharp::AttrAsBool(options, "gifReuse"); + baton->gifProgressive = sharp::AttrAsBool(options, "gifProgressive"); baton->tiffQuality = sharp::AttrAsUint32(options, "tiffQuality"); + baton->tiffBigtiff = sharp::AttrAsBool(options, "tiffBigtiff"); baton->tiffPyramid = sharp::AttrAsBool(options, "tiffPyramid"); + baton->tiffMiniswhite = sharp::AttrAsBool(options, "tiffMiniswhite"); baton->tiffBitdepth = sharp::AttrAsUint32(options, "tiffBitdepth"); baton->tiffTile = sharp::AttrAsBool(options, "tiffTile"); baton->tiffTileWidth = sharp::AttrAsUint32(options, "tiffTileWidth"); baton->tiffTileHeight = sharp::AttrAsUint32(options, "tiffTileHeight"); baton->tiffXres = sharp::AttrAsDouble(options, "tiffXres"); baton->tiffYres = sharp::AttrAsDouble(options, "tiffYres"); - // tiff compression options - baton->tiffCompression = static_cast<VipsForeignTiffCompression>( - vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_COMPRESSION, - sharp::AttrAsStr(options, "tiffCompression").data())); - baton->tiffPredictor = static_cast<VipsForeignTiffPredictor>( - vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_PREDICTOR, - sharp::AttrAsStr(options, "tiffPredictor").data())); + if (baton->tiffXres == 1.0 && baton->tiffYres == 1.0 && baton->withMetadataDensity > 0) { + baton->tiffXres = baton->tiffYres = baton->withMetadataDensity / 25.4; + } + baton->tiffCompression = sharp::AttrAsEnum<VipsForeignTiffCompression>( + options, "tiffCompression", VIPS_TYPE_FOREIGN_TIFF_COMPRESSION); + baton->tiffPredictor = sharp::AttrAsEnum<VipsForeignTiffPredictor>( + options, "tiffPredictor", VIPS_TYPE_FOREIGN_TIFF_PREDICTOR); + baton->tiffResolutionUnit = sharp::AttrAsEnum<VipsForeignTiffResunit>( + options, "tiffResolutionUnit", VIPS_TYPE_FOREIGN_TIFF_RESUNIT); baton->heifQuality = sharp::AttrAsUint32(options, "heifQuality"); baton->heifLossless = sharp::AttrAsBool(options, "heifLossless"); - 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->heifCompression = sharp::AttrAsEnum<VipsForeignHeifCompression>( + options, "heifCompression", VIPS_TYPE_FOREIGN_HEIF_COMPRESSION); + baton->heifEffort = sharp::AttrAsUint32(options, "heifEffort"); baton->heifChromaSubsampling = sharp::AttrAsStr(options, "heifChromaSubsampling"); - - // Animated output - if (sharp::HasAttr(options, "pageHeight")) { - baton->pageHeight = sharp::AttrAsUint32(options, "pageHeight"); - } - if (sharp::HasAttr(options, "loop")) { - baton->loop = sharp::AttrAsUint32(options, "loop"); - } - if (sharp::HasAttr(options, "delay")) { - baton->delay = sharp::AttrAsInt32Vector(options, "delay"); - } - - // Tile output + baton->heifBitdepth = sharp::AttrAsUint32(options, "heifBitdepth"); + baton->jxlDistance = sharp::AttrAsDouble(options, "jxlDistance"); + baton->jxlDecodingTier = sharp::AttrAsUint32(options, "jxlDecodingTier"); + baton->jxlEffort = sharp::AttrAsUint32(options, "jxlEffort"); + baton->jxlLossless = sharp::AttrAsBool(options, "jxlLossless"); + baton->rawDepth = sharp::AttrAsEnum<VipsBandFormat>(options, "rawDepth", VIPS_TYPE_BAND_FORMAT); baton->tileSize = sharp::AttrAsUint32(options, "tileSize"); baton->tileOverlap = sharp::AttrAsUint32(options, "tileOverlap"); baton->tileAngle = sharp::AttrAsInt32(options, "tileAngle"); 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, - sharp::AttrAsStr(options, "tileContainer").data())); - baton->tileLayout = static_cast<VipsForeignDzLayout>( - vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_DZ_LAYOUT, - sharp::AttrAsStr(options, "tileLayout").data())); + baton->tileContainer = sharp::AttrAsEnum<VipsForeignDzContainer>( + options, "tileContainer", VIPS_TYPE_FOREIGN_DZ_CONTAINER); + baton->tileLayout = sharp::AttrAsEnum<VipsForeignDzLayout>(options, "tileLayout", VIPS_TYPE_FOREIGN_DZ_LAYOUT); baton->tileFormat = sharp::AttrAsStr(options, "tileFormat"); - baton->tileDepth = static_cast<VipsForeignDzDepth>( - vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_DZ_DEPTH, - sharp::AttrAsStr(options, "tileDepth").data())); + baton->tileDepth = sharp::AttrAsEnum<VipsForeignDzDepth>(options, "tileDepth", VIPS_TYPE_FOREIGN_DZ_DEPTH); baton->tileCentre = sharp::AttrAsBool(options, "tileCentre"); baton->tileId = sharp::AttrAsStr(options, "tileId"); - - // Force random access for certain operations - if (baton->input->access == VIPS_ACCESS_SEQUENTIAL) { - if ( - baton->trimThreshold > 0.0 || - baton->normalise || - baton->position == 16 || baton->position == 17 || - baton->angle % 360 != 0 || - fmod(baton->rotationAngle, 360.0) != 0.0 || - baton->useExifOrientation - ) { - baton->input->access = VIPS_ACCESS_RANDOM; - } - } + baton->tileBasename = sharp::AttrAsStr(options, "tileBasename"); // Function to notify of libvips warnings Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>(); @@ -1475,14 +1801,13 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { Napi::Function queueListener = options.Get("queueListener").As<Napi::Function>(); // Join queue for worker thread - Napi::Function callback = info[1].As<Napi::Function>(); + Napi::Function callback = info[size_t(1)].As<Napi::Function>(); PipelineWorker *worker = new PipelineWorker(callback, baton, debuglog, queueListener); worker->Receiver().Set("options", options); worker->Queue(); // Increment queued task counter - g_atomic_int_inc(&sharp::counterQueue); - Napi::Number queueLength = Napi::Number::New(info.Env(), static_cast<double>(sharp::counterQueue)); + Napi::Number queueLength = Napi::Number::New(info.Env(), static_cast<int>(++sharp::counterQueue)); queueListener.Call(info.This(), { queueLength }); return info.Env().Undefined(); diff --git a/src/pipeline.h b/src/pipeline.h index a479bdf47..ff9465987 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -1,22 +1,14 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #ifndef SRC_PIPELINE_H_ #define SRC_PIPELINE_H_ #include <memory> #include <string> +#include <unordered_map> #include <vector> #include <napi.h> @@ -26,14 +18,6 @@ Napi::Value pipeline(const Napi::CallbackInfo& info); -enum class Canvas { - CROP, - EMBED, - MAX, - MIN, - IGNORE_ASPECT -}; - struct Composite { sharp::InputDescriptor *input; VipsBlendMode mode; @@ -57,10 +41,13 @@ struct Composite { struct PipelineBaton { sharp::InputDescriptor *input; + std::vector<sharp::InputDescriptor *> join; std::string formatOut; std::string fileOut; void *bufferOut; size_t bufferOutLength; + int pageHeightOut; + int pagesOut; std::vector<Composite *> composite; std::vector<sharp::InputDescriptor *> joinChannelIn; int topOffsetPre; @@ -74,45 +61,64 @@ struct PipelineBaton { int width; int height; int channels; - Canvas canvas; + VipsKernel kernel; + sharp::Canvas canvas; int position; std::vector<double> resizeBackground; bool hasCropOffset; int cropOffsetLeft; int cropOffsetTop; + bool hasAttentionCenter; + int attentionX; + int attentionY; bool premultiplied; bool tileCentre; - std::string kernel; bool fastShrinkOnLoad; - double tintA; - double tintB; + std::vector<double> tint; bool flatten; std::vector<double> flattenBackground; + bool unflatten; bool negate; + bool negateAlpha; double blurSigma; + VipsPrecision precision; + double minAmpl; double brightness; double saturation; int hue; + double lightness; int medianSize; double sharpenSigma; - double sharpenFlat; - double sharpenJagged; + double sharpenM1; + double sharpenM2; + double sharpenX1; + double sharpenY2; + double sharpenY3; int threshold; bool thresholdGrayscale; + std::vector<double> trimBackground; double trimThreshold; + bool trimLineArt; int trimOffsetLeft; int trimOffsetTop; - double linearA; - double linearB; + std::vector<double> linearA; + std::vector<double> linearB; + int dilateWidth; + int erodeWidth; double gamma; double gammaOut; bool greyscale; bool normalise; - bool useExifOrientation; + int normaliseLower; + int normaliseUpper; + int claheWidth; + int claheHeight; + int claheMaxSlope; int angle; double rotationAngle; std::vector<double> rotationBackground; - bool rotateBeforePreExtract; + bool rotateBefore; + bool orientBefore; bool flip; bool flop; int extendTop; @@ -120,14 +126,16 @@ struct PipelineBaton { int extendLeft; int extendRight; std::vector<double> extendBackground; + VipsExtend extendWith; bool withoutEnlargement; + bool withoutReduction; std::vector<double> affineMatrix; std::vector<double> affineBackground; double affineIdx; double affineIdy; double affineOdx; double affineOdy; - vips::VInterpolate affineInterpolator; + std::string affineInterpolator; int jpegQuality; bool jpegProgressive; std::string jpegChromaSubsampling; @@ -141,34 +149,67 @@ struct PipelineBaton { bool pngAdaptiveFiltering; bool pngPalette; int pngQuality; - int pngColours; + int pngEffort; + int pngBitdepth; double pngDither; + int jp2Quality; + bool jp2Lossless; + int jp2TileHeight; + int jp2TileWidth; + std::string jp2ChromaSubsampling; int webpQuality; int webpAlphaQuality; bool webpNearLossless; bool webpLossless; bool webpSmartSubsample; - int webpReductionEffort; + bool webpSmartDeblock; + VipsForeignWebpPreset webpPreset; + int webpEffort; + bool webpMinSize; + bool webpMixed; + int gifBitdepth; + int gifEffort; + double gifDither; + double gifInterFrameMaxError; + double gifInterPaletteMaxError; + bool gifKeepDuplicateFrames; + bool gifReuse; + bool gifProgressive; int tiffQuality; VipsForeignTiffCompression tiffCompression; + bool tiffBigtiff; VipsForeignTiffPredictor tiffPredictor; bool tiffPyramid; int tiffBitdepth; + bool tiffMiniswhite; bool tiffTile; int tiffTileHeight; int tiffTileWidth; double tiffXres; double tiffYres; + VipsForeignTiffResunit tiffResolutionUnit; int heifQuality; VipsForeignHeifCompression heifCompression; - int heifSpeed; + int heifEffort; std::string heifChromaSubsampling; bool heifLossless; + int heifBitdepth; + double jxlDistance; + int jxlDecodingTier; + int jxlEffort; + bool jxlLossless; + VipsBandFormat rawDepth; std::string err; - bool withMetadata; + bool errUseWarning; + int keepMetadata; int withMetadataOrientation; - std::string withMetadataIcc; - std::unique_ptr<double[]> convKernel; + double withMetadataDensity; + std::string withIccProfile; + std::unordered_map<std::string, std::string> withExif; + bool withExifMerge; + std::string withXmp; + int timeoutSeconds; + std::vector<double> convKernel; int convKernelWidth; int convKernelHeight; double convKernelScale; @@ -178,9 +219,9 @@ struct PipelineBaton { VipsOperationBoolean bandBoolOp; int extractChannel; bool removeAlpha; - bool ensureAlpha; + double ensureAlpha; + VipsInterpretation colourspacePipeline; VipsInterpretation colourspace; - int pageHeight; std::vector<int> delay; int loop; int tileSize; @@ -193,45 +234,65 @@ struct PipelineBaton { int tileSkipBlanks; VipsForeignDzDepth tileDepth; std::string tileId; - std::unique_ptr<double[]> recombMatrix; + std::string tileBasename; + std::vector<double> recombMatrix; PipelineBaton(): input(nullptr), bufferOutLength(0), + pageHeightOut(0), + pagesOut(0), topOffsetPre(-1), topOffsetPost(-1), channels(0), - canvas(Canvas::CROP), + kernel(VIPS_KERNEL_LANCZOS3), + canvas(sharp::Canvas::CROP), position(0), resizeBackground{ 0.0, 0.0, 0.0, 255.0 }, hasCropOffset(false), cropOffsetLeft(0), cropOffsetTop(0), + hasAttentionCenter(false), + attentionX(0), + attentionY(0), premultiplied(false), - tintA(128.0), - tintB(128.0), + tint{ -1.0, 0.0, 0.0, 0.0 }, flatten(false), flattenBackground{ 0.0, 0.0, 0.0 }, + unflatten(false), negate(false), + negateAlpha(true), blurSigma(0.0), brightness(1.0), saturation(1.0), hue(0), + lightness(0), medianSize(0), sharpenSigma(0.0), - sharpenFlat(1.0), - sharpenJagged(2.0), + sharpenM1(1.0), + sharpenM2(2.0), + sharpenX1(2.0), + sharpenY2(10.0), + sharpenY3(20.0), threshold(0), thresholdGrayscale(true), - trimThreshold(0.0), + trimBackground{}, + trimThreshold(-1.0), + trimLineArt(false), trimOffsetLeft(0), trimOffsetTop(0), - linearA(1.0), - linearB(0.0), + linearA{}, + linearB{}, + dilateWidth(0), + erodeWidth(0), gamma(0.0), greyscale(false), normalise(false), - useExifOrientation(false), + normaliseLower(1), + normaliseUpper(99), + claheWidth(0), + claheHeight(0), + claheMaxSlope(3), angle(0), rotationAngle(0.0), rotationBackground{ 0.0, 0.0, 0.0, 255.0 }, @@ -242,14 +303,16 @@ struct PipelineBaton { extendLeft(0), extendRight(0), extendBackground{ 0.0, 0.0, 0.0, 255.0 }, + extendWith(VIPS_EXTEND_BACKGROUND), withoutEnlargement(false), + withoutReduction(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")), + affineInterpolator("bicubic"), jpegQuality(80), jpegProgressive(false), jpegChromaSubsampling("4:2:0"), @@ -263,31 +326,62 @@ struct PipelineBaton { pngAdaptiveFiltering(false), pngPalette(false), pngQuality(100), - pngColours(256), + pngEffort(7), + pngBitdepth(8), pngDither(1.0), + jp2Quality(80), + jp2Lossless(false), + jp2TileHeight(512), + jp2TileWidth(512), + jp2ChromaSubsampling("4:4:4"), webpQuality(80), webpAlphaQuality(100), webpNearLossless(false), webpLossless(false), webpSmartSubsample(false), - webpReductionEffort(4), + webpSmartDeblock(false), + webpPreset(VIPS_FOREIGN_WEBP_PRESET_DEFAULT), + webpEffort(4), + webpMinSize(false), + webpMixed(false), + gifBitdepth(8), + gifEffort(7), + gifDither(1.0), + gifInterFrameMaxError(0.0), + gifInterPaletteMaxError(3.0), + gifKeepDuplicateFrames(false), + gifReuse(true), + gifProgressive(false), tiffQuality(80), tiffCompression(VIPS_FOREIGN_TIFF_COMPRESSION_JPEG), + tiffBigtiff(false), tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL), tiffPyramid(false), tiffBitdepth(8), + tiffMiniswhite(false), tiffTile(false), tiffTileHeight(256), tiffTileWidth(256), tiffXres(1.0), tiffYres(1.0), + tiffResolutionUnit(VIPS_FOREIGN_TIFF_RESUNIT_INCH), heifQuality(50), heifCompression(VIPS_FOREIGN_HEIF_COMPRESSION_AV1), - heifSpeed(5), - heifChromaSubsampling("4:2:0"), + heifEffort(4), + heifChromaSubsampling("4:4:4"), heifLossless(false), - withMetadata(false), + heifBitdepth(8), + jxlDistance(1.0), + jxlDecodingTier(0), + jxlEffort(7), + jxlLossless(false), + rawDepth(VIPS_FORMAT_UCHAR), + errUseWarning(false), + keepMetadata(0), withMetadataOrientation(-1), + withMetadataDensity(0.0), + withExifMerge(true), + timeoutSeconds(0), convKernelWidth(0), convKernelHeight(0), convKernelScale(0.0), @@ -297,10 +391,9 @@ struct PipelineBaton { bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST), extractChannel(-1), removeAlpha(false), - ensureAlpha(false), + ensureAlpha(-1.0), + colourspacePipeline(VIPS_INTERPRETATION_LAST), colourspace(VIPS_INTERPRETATION_LAST), - pageHeight(0), - delay{-1}, loop(-1), tileSize(256), tileOverlap(0), diff --git a/src/sharp.cc b/src/sharp.cc index f76a5cb07..7678975c9 100644 --- a/src/sharp.cc +++ b/src/sharp.cc @@ -1,35 +1,24 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +#include <mutex> #include <napi.h> #include <vips/vips8> -#include "common.h" -#include "metadata.h" -#include "pipeline.h" -#include "utilities.h" -#include "stats.h" - -static void* sharp_vips_init(void*) { - g_setenv("VIPS_MIN_STACK_SIZE", "2m", FALSE); - vips_init("sharp"); - return nullptr; -} +#include "./common.h" +#include "./metadata.h" +#include "./pipeline.h" +#include "./stats.h" +#include "./utilities.h" Napi::Object init(Napi::Env env, Napi::Object exports) { - static GOnce sharp_vips_init_once = G_ONCE_INIT; - g_once(&sharp_vips_init_once, static_cast<GThreadFunc>(sharp_vips_init), nullptr); + static std::once_flag sharp_vips_init_once; + std::call_once(sharp_vips_init_once, []() { + vips_init("sharp"); + }); g_log_set_handler("VIPS", static_cast<GLogLevelFlags>(G_LOG_LEVEL_WARNING), static_cast<GLogFunc>(sharp::VipsWarningCallback), nullptr); @@ -43,8 +32,10 @@ Napi::Object init(Napi::Env env, Napi::Object exports) { exports.Set("simd", Napi::Function::New(env, simd)); exports.Set("libvipsVersion", Napi::Function::New(env, libvipsVersion)); exports.Set("format", Napi::Function::New(env, format)); + exports.Set("block", Napi::Function::New(env, block)); exports.Set("_maxColourDistance", Napi::Function::New(env, _maxColourDistance)); exports.Set("_isUsingJemalloc", Napi::Function::New(env, _isUsingJemalloc)); + exports.Set("_isUsingX64V2", Napi::Function::New(env, _isUsingX64V2)); exports.Set("stats", Napi::Function::New(env, stats)); return exports; } diff --git a/src/stats.cc b/src/stats.cc index 1d7942b17..b1fd27a7c 100644 --- a/src/stats.cc +++ b/src/stats.cc @@ -1,26 +1,18 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ +#include <iostream> #include <numeric> +#include <string> #include <vector> -#include <iostream> #include <napi.h> #include <vips/vips8> -#include "common.h" -#include "stats.h" +#include "./common.h" +#include "./stats.h" class StatsWorker : public Napi::AsyncWorker { public: @@ -41,7 +33,7 @@ class StatsWorker : public Napi::AsyncWorker { void Execute() { // Decrement queued task counter - g_atomic_int_dec_and_test(&sharp::counterQueue); + sharp::counterQueue--; vips::VImage image; sharp::ImageType imageType = sharp::ImageType::UNKNOWN; @@ -69,9 +61,9 @@ class StatsWorker : public Napi::AsyncWorker { baton->channelStats.push_back(cStats); } // Image is not opaque when alpha layer is present and contains a non-mamixa value - if (sharp::HasAlpha(image)) { + if (image.has_alpha()) { double const minAlpha = static_cast<double>(stats.getpoint(STAT_MIN_INDEX, bands).front()); - if (minAlpha != sharp::MaximumImageAlpha(image.interpretation())) { + if (minAlpha != vips_interpretation_max_alpha(image.interpretation())) { baton->isOpaque = false; } } @@ -117,7 +109,7 @@ class StatsWorker : public Napi::AsyncWorker { // Handle warnings std::string warning = sharp::VipsWarningPop(); while (!warning.empty()) { - debuglog.Call({ Napi::String::New(env, warning) }); + debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) }); warning = sharp::VipsWarningPop(); } @@ -152,9 +144,9 @@ class StatsWorker : public Napi::AsyncWorker { dominant.Set("g", baton->dominantGreen); dominant.Set("b", baton->dominantBlue); info.Set("dominant", dominant); - Callback().MakeCallback(Receiver().Value(), { env.Null(), info }); + Callback().Call(Receiver().Value(), { env.Null(), info }); } else { - Callback().MakeCallback(Receiver().Value(), { Napi::Error::New(env, baton->err).Value() }); + Callback().Call(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() }); } delete baton->input; @@ -172,22 +164,23 @@ class StatsWorker : public Napi::AsyncWorker { Napi::Value stats(const Napi::CallbackInfo& info) { // V8 objects are converted to non-V8 types held in the baton struct StatsBaton *baton = new StatsBaton; - Napi::Object options = info[0].As<Napi::Object>(); + Napi::Object options = info[size_t(0)].As<Napi::Object>(); // Input baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>()); + baton->input->access = VIPS_ACCESS_RANDOM; // Function to notify of libvips warnings Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>(); // Join queue for worker thread - Napi::Function callback = info[1].As<Napi::Function>(); + Napi::Function callback = info[size_t(1)].As<Napi::Function>(); StatsWorker *worker = new StatsWorker(callback, baton, debuglog); worker->Receiver().Set("options", options); worker->Queue(); // Increment queued task counter - g_atomic_int_inc(&sharp::counterQueue); + sharp::counterQueue++; return info.Env().Undefined(); } diff --git a/src/stats.h b/src/stats.h index 8d9dca5fd..88e13c60c 100644 --- a/src/stats.h +++ b/src/stats.h @@ -1,21 +1,13 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #ifndef SRC_STATS_H_ #define SRC_STATS_H_ #include <string> +#include <vector> #include <napi.h> #include "./common.h" @@ -35,7 +27,7 @@ struct ChannelStats { ChannelStats(int minVal, int maxVal, double sumVal, double squaresSumVal, double meanVal, double stdevVal, int minXVal, int minYVal, int maxXVal, int maxYVal): - min(minVal), max(maxVal), sum(sumVal), squaresSum(squaresSumVal), + min(minVal), max(maxVal), sum(sumVal), squaresSum(squaresSumVal), // NOLINT(build/include_what_you_use) mean(meanVal), stdev(stdevVal), minX(minXVal), minY(minYVal), maxX(maxXVal), maxY(maxYVal) {} }; diff --git a/src/utilities.cc b/src/utilities.cc index ed36d76d5..4154c08ac 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -1,27 +1,19 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #include <cmath> +#include <cstdio> #include <string> #include <napi.h> #include <vips/vips8> #include <vips/vector.h> -#include "common.h" -#include "operations.h" -#include "utilities.h" +#include "./common.h" +#include "./operations.h" +#include "./utilities.h" /* Get and set cache limits @@ -30,16 +22,16 @@ Napi::Value cache(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); // Set memory limit - if (info[0].IsNumber()) { - vips_cache_set_max_mem(info[0].As<Napi::Number>().Int32Value() * 1048576); + if (info[size_t(0)].IsNumber()) { + vips_cache_set_max_mem(info[size_t(0)].As<Napi::Number>().Int32Value() * 1048576); } // Set file limit - if (info[1].IsNumber()) { - vips_cache_set_max_files(info[1].As<Napi::Number>().Int32Value()); + if (info[size_t(1)].IsNumber()) { + vips_cache_set_max_files(info[size_t(1)].As<Napi::Number>().Int32Value()); } // Set items limit - if (info[2].IsNumber()) { - vips_cache_set_max(info[2].As<Napi::Number>().Int32Value()); + if (info[size_t(2)].IsNumber()) { + vips_cache_set_max(info[size_t(2)].As<Napi::Number>().Int32Value()); } // Get memory stats @@ -69,8 +61,8 @@ Napi::Value cache(const Napi::CallbackInfo& info) { */ Napi::Value concurrency(const Napi::CallbackInfo& info) { // Set concurrency - if (info[0].IsNumber()) { - vips_concurrency_set(info[0].As<Napi::Number>().Int32Value()); + if (info[size_t(0)].IsNumber()) { + vips_concurrency_set(info[size_t(0)].As<Napi::Number>().Int32Value()); } // Get concurrency return Napi::Number::New(info.Env(), vips_concurrency_get()); @@ -81,8 +73,8 @@ Napi::Value concurrency(const Napi::CallbackInfo& info) { */ Napi::Value counters(const Napi::CallbackInfo& info) { Napi::Object counters = Napi::Object::New(info.Env()); - counters.Set("queue", sharp::counterQueue); - counters.Set("process", sharp::counterProcess); + counters.Set("queue", static_cast<int>(sharp::counterQueue)); + counters.Set("process", static_cast<int>(sharp::counterProcess)); return counters; } @@ -91,8 +83,8 @@ Napi::Value counters(const Napi::CallbackInfo& info) { */ Napi::Value simd(const Napi::CallbackInfo& info) { // Set state - if (info[0].IsBoolean()) { - vips_vector_set_enabled(info[0].As<Napi::Boolean>().Value()); + if (info[size_t(0)].IsBoolean()) { + vips_vector_set_enabled(info[size_t(0)].As<Napi::Boolean>().Value()); } // Get state return Napi::Boolean::New(info.Env(), vips_vector_isenabled()); @@ -102,9 +94,23 @@ Napi::Value simd(const Napi::CallbackInfo& info) { Get libvips version */ Napi::Value libvipsVersion(const Napi::CallbackInfo& info) { - char version[9]; - g_snprintf(version, sizeof(version), "%d.%d.%d", vips_version(0), vips_version(1), vips_version(2)); - return Napi::String::New(info.Env(), version); + Napi::Env env = info.Env(); + Napi::Object version = Napi::Object::New(env); + + char semver[9]; + std::snprintf(semver, sizeof(semver), "%d.%d.%d", vips_version(0), vips_version(1), vips_version(2)); + version.Set("semver", Napi::String::New(env, semver)); +#ifdef SHARP_USE_GLOBAL_LIBVIPS + version.Set("isGlobal", Napi::Boolean::New(env, true)); +#else + version.Set("isGlobal", Napi::Boolean::New(env, false)); +#endif +#ifdef __EMSCRIPTEN__ + version.Set("isWasm", Napi::Boolean::New(env, true)); +#else + version.Set("isWasm", Napi::Boolean::New(env, false)); +#endif + return version; } /* @@ -115,17 +121,28 @@ Napi::Value format(const Napi::CallbackInfo& info) { Napi::Object format = Napi::Object::New(env); for (std::string const f : { "jpeg", "png", "webp", "tiff", "magick", "openslide", "dz", - "ppm", "fits", "gif", "svg", "heif", "pdf", "vips" + "ppm", "fits", "gif", "svg", "heif", "pdf", "vips", "jp2k", "jxl", "rad", "dcraw" }) { // Input - Napi::Boolean hasInputFile = - Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "load").c_str())); + const VipsObjectClass *oc = vips_class_find("VipsOperation", (f + "load").c_str()); + Napi::Boolean hasInputFile = Napi::Boolean::New(env, oc); Napi::Boolean hasInputBuffer = Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "load_buffer").c_str())); Napi::Object input = Napi::Object::New(env); input.Set("file", hasInputFile); input.Set("buffer", hasInputBuffer); input.Set("stream", hasInputBuffer); + if (hasInputFile) { + const VipsForeignClass *fc = VIPS_FOREIGN_CLASS(oc); + if (fc->suffs) { + Napi::Array fileSuffix = Napi::Array::New(env); + const char **suffix = fc->suffs; + for (int i = 0; *suffix; i++, suffix++) { + fileSuffix.Set(i, Napi::String::New(env, *suffix)); + } + input.Set("fileSuffix", fileSuffix); + } + } // Output Napi::Boolean hasOutputFile = Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "save").c_str())); @@ -164,6 +181,17 @@ Napi::Value format(const Napi::CallbackInfo& info) { return format; } +/* + (Un)block libvips operations at runtime. +*/ +void block(const Napi::CallbackInfo& info) { + Napi::Array ops = info[size_t(0)].As<Napi::Array>(); + bool const state = info[size_t(1)].As<Napi::Boolean>().Value(); + for (unsigned int i = 0; i < ops.Length(); i++) { + vips_operation_block_set(ops.Get(i).As<Napi::String>().Utf8Value().c_str(), state); + } +} + /* Synchronous, internal-only method used by some of the functional tests. Calculates the maximum colour distance using the DE2000 algorithm @@ -174,10 +202,10 @@ Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) { // Open input files VImage image1; - sharp::ImageType imageType1 = sharp::DetermineImageType(info[0].As<Napi::String>().Utf8Value().data()); + sharp::ImageType imageType1 = sharp::DetermineImageType(info[size_t(0)].As<Napi::String>().Utf8Value().data()); if (imageType1 != sharp::ImageType::UNKNOWN) { try { - image1 = VImage::new_from_file(info[0].As<Napi::String>().Utf8Value().c_str()); + image1 = VImage::new_from_file(info[size_t(0)].As<Napi::String>().Utf8Value().c_str()); } catch (...) { throw Napi::Error::New(env, "Input file 1 has corrupt header"); } @@ -185,10 +213,10 @@ Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) { throw Napi::Error::New(env, "Input file 1 is of an unsupported image format"); } VImage image2; - sharp::ImageType imageType2 = sharp::DetermineImageType(info[1].As<Napi::String>().Utf8Value().data()); + sharp::ImageType imageType2 = sharp::DetermineImageType(info[size_t(1)].As<Napi::String>().Utf8Value().data()); if (imageType2 != sharp::ImageType::UNKNOWN) { try { - image2 = VImage::new_from_file(info[1].As<Napi::String>().Utf8Value().c_str()); + image2 = VImage::new_from_file(info[size_t(1)].As<Napi::String>().Utf8Value().c_str()); } catch (...) { throw Napi::Error::New(env, "Input file 2 has corrupt header"); } @@ -207,10 +235,10 @@ Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) { double maxColourDistance; try { // Premultiply and remove alpha - if (sharp::HasAlpha(image1)) { + if (image1.has_alpha()) { image1 = image1.premultiply().extract_band(1, VImage::option()->set("n", image1.bands() - 1)); } - if (sharp::HasAlpha(image2)) { + if (image2.has_alpha()) { image2 = image2.premultiply().extract_band(1, VImage::option()->set("n", image2.bands() - 1)); } // Calculate colour distance @@ -241,3 +269,20 @@ Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info) { return Napi::Boolean::New(env, false); } #endif + +#if defined(__GNUC__) && defined(__x86_64__) +// Are SSE 4.2 intrinsics available at runtime? +Napi::Value _isUsingX64V2(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + unsigned int eax, ebx, ecx, edx; + __asm__ __volatile__("cpuid" + : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) + : "a"(1)); + return Napi::Boolean::New(env, (ecx & 1U << 20) != 0); +} +#else +Napi::Value _isUsingX64V2(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + return Napi::Boolean::New(env, false); +} +#endif diff --git a/src/utilities.h b/src/utilities.h index 5659feea2..a1719fa23 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -1,16 +1,7 @@ -// Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ #ifndef SRC_UTILITIES_H_ #define SRC_UTILITIES_H_ @@ -23,7 +14,9 @@ Napi::Value counters(const Napi::CallbackInfo& info); Napi::Value simd(const Napi::CallbackInfo& info); Napi::Value libvipsVersion(const Napi::CallbackInfo& info); Napi::Value format(const Napi::CallbackInfo& info); +void block(const Napi::CallbackInfo& info); Napi::Value _maxColourDistance(const Napi::CallbackInfo& info); Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info); +Napi::Value _isUsingX64V2(const Napi::CallbackInfo& info); #endif // SRC_UTILITIES_H_ diff --git a/test/bench/Dockerfile b/test/bench/Dockerfile new file mode 100644 index 000000000..290db857a --- /dev/null +++ b/test/bench/Dockerfile @@ -0,0 +1,28 @@ +FROM ubuntu:25.04 +ARG BRANCH=main + +# Install basic dependencies +RUN apt-get -y update && apt-get install -y build-essential curl git ca-certificates gnupg + +# Install latest Node.js LTS +RUN curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh +RUN bash nodesource_setup.sh +RUN apt-get install -y nodejs + +# Install benchmark dependencies +RUN apt-get install -y imagemagick libmagick++-dev graphicsmagick + +# Install sharp +RUN mkdir /tmp/sharp +RUN cd /tmp && git clone --single-branch --branch $BRANCH https://github.com/lovell/sharp.git +RUN cd /tmp/sharp && npm install && npm run build + +# Install benchmark test +RUN cd /tmp/sharp/test/bench && npm install --omit optional + +RUN cat /etc/os-release | grep VERSION= +RUN node -v + +WORKDIR /tmp/sharp/test/bench + +CMD [ "node", "perf" ] diff --git a/test/bench/package.json b/test/bench/package.json index b6eca9a63..e6a97a4f8 100644 --- a/test/bench/package.json +++ b/test/bench/package.json @@ -7,17 +7,16 @@ "scripts": { "test": "node perf && node random && node parallel" }, - "devDependencies": { - "async": "3.2.0", + "dependencies": { + "async": "3.2.6", "benchmark": "2.1.4", - "gm": "1.23.1", + "gm": "1.25.1", "imagemagick": "0.1.3", - "jimp": "0.16.1", - "mapnik": "4.5.6", - "semver": "7.3.4" + "jimp": "1.6.0" }, - "license": "Apache-2.0", - "engines": { - "node": "14" - } + "optionalDependencies": { + "@tensorflow/tfjs-node": "4.22.0", + "mapnik": "4.5.9" + }, + "license": "Apache-2.0" } diff --git a/test/bench/parallel.js b/test/bench/parallel.js index bff210422..88d7d4bac 100644 --- a/test/bench/parallel.js +++ b/test/bench/parallel.js @@ -1,8 +1,11 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ process.env.UV_THREADPOOL_SIZE = 64; -const assert = require('assert'); +const assert = require('node:assert'); const async = require('async'); const sharp = require('../../'); @@ -12,34 +15,30 @@ const width = 720; const height = 480; sharp.concurrency(1); -sharp.simd(true); -const timer = setInterval(function () { +const timer = setInterval(() => { console.dir(sharp.counters()); }, 100); -async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64], function (parallelism, next) { - const start = new Date().getTime(); +async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64], (parallelism, next) => { + const start = Date.now(); async.times(parallelism, - function (id, callback) { - /* jslint unused: false */ - sharp(fixtures.inputJpg).resize(width, height).toBuffer(function (err, buffer) { + (_id, callback) => { + sharp(fixtures.inputJpg).resize(width, height).toBuffer((err, buffer) => { buffer = null; - callback(err, new Date().getTime() - start); + callback(err, Date.now() - start); }); }, - function (err, ids) { + (err, ids) => { assert(!err); assert(ids.length === parallelism); ids.sort(); - const mean = ids.reduce(function (a, b) { - return a + b; - }) / ids.length; - console.log(parallelism + ' parallel calls: fastest=' + ids[0] + 'ms slowest=' + ids[ids.length - 1] + 'ms mean=' + mean + 'ms'); + const mean = ids.reduce((a, b) => a + b) / ids.length; + console.log(`${parallelism} parallel calls: fastest=${ids[0]}ms slowest=${ids[ids.length - 1]}ms mean=${mean}ms`); next(); } ); -}, function () { +}, () => { clearInterval(timer); console.dir(sharp.counters()); }); diff --git a/test/bench/perf.js b/test/bench/perf.js index 27fff04f1..375006ef0 100644 --- a/test/bench/perf.js +++ b/test/bench/perf.js @@ -1,18 +1,30 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const os = require('os'); -const fs = require('fs'); +const fs = require('node:fs'); +const { execSync } = require('node:child_process'); const async = require('async'); -const assert = require('assert'); const Benchmark = require('benchmark'); +const safeRequire = (name) => { + try { + return require(name); + } catch (_err) {} + return null; +}; + // Contenders const sharp = require('../../'); const gm = require('gm'); const imagemagick = require('imagemagick'); -const mapnik = require('mapnik'); -const jimp = require('jimp'); +const mapnik = safeRequire('mapnik'); +const { Jimp, JimpMime } = require('jimp'); + +process.env.TF_CPP_MIN_LOG_LEVEL = 1; +const tfjs = safeRequire('@tensorflow/tfjs-node'); const fixtures = require('../fixtures'); @@ -22,70 +34,51 @@ const outputWebP = fixtures.path('output.webp'); const width = 720; const height = 588; +const heightPng = 540; // Disable libvips cache to ensure tests are as fair as they can be sharp.cache(false); -// Spawn one thread per CPU -sharp.concurrency(os.cpus().length); +// Spawn one thread per physical CPU core +const physicalCores = Number(execSync('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l', { encoding: 'utf-8' }).trim()); +console.log(`Detected ${physicalCores} physical cores`); +sharp.concurrency(physicalCores); async.series({ - jpeg: function (callback) { + jpeg: (callback) => { const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const jpegSuite = new Benchmark.Suite('jpeg'); // jimp jpegSuite.add('jimp-buffer-buffer', { defer: true, - fn: function (deferred) { - jimp.read(inputJpgBuffer, function (err, image) { - if (err) { - throw err; - } else { - image - .resize(width, height, jimp.RESIZE_BICUBIC) - .quality(80) - .getBuffer(jimp.MIME_JPEG, function (err) { - if (err) { - throw err; - } else { - deferred.resolve(); - } - }); - } - }); + fn: async (deferred) => { + const image = await Jimp.read(inputJpgBuffer); + await image + .resize({ w: width, h: height, mode: Jimp.RESIZE_BICUBIC }) + .getBuffer(JimpMime.jpeg, { quality: 80 }); + deferred.resolve(); } }).add('jimp-file-file', { defer: true, - fn: function (deferred) { - jimp.read(fixtures.inputJpg, function (err, image) { - if (err) { - throw err; - } else { - image - .resize(width, height, jimp.RESIZE_BICUBIC) - .quality(80) - .write(outputJpg, function (err) { - if (err) { - throw err; - } else { - deferred.resolve(); - } - }); - } - }); + fn: async (deferred) => { + const image = await Jimp.read(fixtures.inputJpg); + await image + .resize({ w: width, h: height, mode: Jimp.RESIZE_BICUBIC }) + .getBuffer(JimpMime.jpeg, { quality: 80 }); + deferred.resolve(); } }); // mapnik - jpegSuite.add('mapnik-file-file', { + mapnik && jpegSuite.add('mapnik-file-file', { defer: true, - fn: function (deferred) { - mapnik.Image.open(fixtures.inputJpg, function (err, img) { + fn: (deferred) => { + mapnik.Image.open(fixtures.inputJpg, (err, img) => { if (err) throw err; img .resize(width, height, { scaling_method: mapnik.imageScaling.lanczos }) - .save(outputJpg, 'jpeg:quality=80', function (err) { + .save(outputJpg, 'jpeg:quality=80', (err) => { if (err) throw err; deferred.resolve(); }); @@ -93,14 +86,14 @@ async.series({ } }).add('mapnik-buffer-buffer', { defer: true, - fn: function (deferred) { - mapnik.Image.fromBytes(inputJpgBuffer, { max_size: 3000 }, function (err, img) { + fn: (deferred) => { + mapnik.Image.fromBytes(inputJpgBuffer, { max_size: 3000 }, (err, img) => { if (err) throw err; img .resize(width, height, { scaling_method: mapnik.imageScaling.lanczos }) - .encode('jpeg:quality=80', function (err) { + .encode('jpeg:quality=80', (err) => { if (err) throw err; deferred.resolve(); }); @@ -110,16 +103,16 @@ async.series({ // imagemagick jpegSuite.add('imagemagick-file-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { imagemagick.resize({ srcPath: fixtures.inputJpg, dstPath: outputJpg, quality: 0.8, - width: width, - height: height, + width, + height, format: 'jpg', filter: 'Lanczos' - }, function (err) { + }, (err) => { if (err) { throw err; } else { @@ -131,12 +124,12 @@ async.series({ // gm jpegSuite.add('gm-buffer-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(inputJpgBuffer) .filter('Lanczos') .resize(width, height) .quality(80) - .write(outputJpg, function (err) { + .write(outputJpg, (err) => { if (err) { throw err; } else { @@ -146,28 +139,27 @@ async.series({ } }).add('gm-buffer-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(inputJpgBuffer) .filter('Lanczos') .resize(width, height) .quality(80) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('gm-file-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(fixtures.inputJpg) .filter('Lanczos') .resize(width, height) .quality(80) - .write(outputJpg, function (err) { + .write(outputJpg, (err) => { if (err) { throw err; } else { @@ -177,28 +169,45 @@ async.series({ } }).add('gm-file-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(fixtures.inputJpg) .filter('Lanczos') .resize(width, height) .quality(80) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }); + // tfjs + tfjs && jpegSuite.add('tfjs-node-buffer-buffer', { + defer: true, + fn: (deferred) => { + const decoded = tfjs.node.decodeJpeg(inputJpgBuffer); + const resized = tfjs.image.resizeBilinear(decoded, [height, width]); + tfjs + .node + .encodeJpeg(resized, 'rgb', 80) + .then(() => { + deferred.resolve(); + tfjs.disposeVariables(); + }) + .catch((err) => { + throw err; + }); + } + }); // sharp jpegSuite.add('sharp-buffer-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) - .toFile(outputJpg, function (err) { + .toFile(outputJpg, (err) => { if (err) { throw err; } else { @@ -208,24 +217,23 @@ async.series({ } }).add('sharp-buffer-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-file-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputJpg) .resize(width, height) - .toFile(outputJpg, function (err) { + .toFile(outputJpg, (err) => { if (err) { throw err; } else { @@ -235,10 +243,10 @@ async.series({ } }).add('sharp-stream-stream', { defer: true, - fn: function (deferred) { + fn: (deferred) => { const readable = fs.createReadStream(fixtures.inputJpg); const writable = fs.createWriteStream(outputJpg); - writable.on('finish', function () { + writable.on('finish', () => { deferred.resolve(); }); const pipeline = sharp() @@ -247,393 +255,375 @@ async.series({ } }).add('sharp-file-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputJpg) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-promise', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .toBuffer() - .then(function (buffer) { - assert.notStrictEqual(null, buffer); + .then(() => { deferred.resolve(); }) - .catch(function (err) { + .catch((err) => { throw err; }); } - }).on('cycle', function (event) { - console.log('jpeg ' + String(event.target)); + }).on('cycle', (event) => { + console.log(`jpeg ${String(event.target)}`); }).on('complete', function () { callback(null, this.filter('fastest').map('name')); }).run(); }, // Effect of applying operations - operations: function (callback) { + operations: (callback) => { const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const operationsSuite = new Benchmark.Suite('operations'); operationsSuite.add('sharp-sharpen-mild', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .sharpen() - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-sharpen-radius', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .sharpen(3, 1, 3) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-blur-mild', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .blur() - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-blur-radius', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .blur(3) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-gamma', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .gamma() - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-normalise', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .normalise() - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-greyscale', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .greyscale() - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-greyscale-gamma', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .gamma() .greyscale() - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-progressive', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .jpeg({ progressive: true }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-without-chroma-subsampling', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height) .jpeg({ chromaSubsampling: '4:4:4' }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-rotate', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .rotate(90) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-without-simd', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp.simd(false); sharp(inputJpgBuffer) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { sharp.simd(true); if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } - }).add('sharp-sequentialRead', { + }).add('sharp-random-access-read', { defer: true, - fn: function (deferred) { - sharp(inputJpgBuffer, { sequentialRead: true }) + fn: (deferred) => { + sharp(inputJpgBuffer, { sequentialRead: false }) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-crop-entropy', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height, { fit: 'cover', position: sharp.strategy.entropy }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-crop-attention', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height, { fit: 'cover', position: sharp.strategy.attention }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } - }).on('cycle', function (event) { - console.log('operations ' + String(event.target)); + }).on('cycle', (event) => { + console.log(`operations ${String(event.target)}`); }).on('complete', function () { callback(null, this.filter('fastest').map('name')); }).run(); }, // Comparative speed of kernels - kernels: function (callback) { + kernels: (callback) => { const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); (new Benchmark.Suite('kernels')).add('sharp-cubic', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height, { kernel: 'cubic' }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-lanczos2', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height, { kernel: 'lanczos2' }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-lanczos3', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputJpgBuffer) .resize(width, height, { kernel: 'lanczos3' }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { + if (err) { + throw err; + } else { + deferred.resolve(); + } + }); + } + }).add('sharp-mks2013', { + defer: true, + fn: (deferred) => { + sharp(inputJpgBuffer) + .resize(width, height, { kernel: 'mks2013' }) + .toBuffer((err) => { + if (err) { + throw err; + } else { + deferred.resolve(); + } + }); + } + }).add('sharp-mks2021', { + defer: true, + fn: (deferred) => { + sharp(inputJpgBuffer) + .resize(width, height, { kernel: 'mks2021' }) + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } - }).on('cycle', function (event) { - console.log('kernels ' + String(event.target)); + }).on('cycle', (event) => { + console.log(`kernels ${String(event.target)}`); }).on('complete', function () { callback(null, this.filter('fastest').map('name')); }).run(); }, // PNG - png: function (callback) { + png: (callback) => { const inputPngBuffer = fs.readFileSync(fixtures.inputPngAlphaPremultiplicationLarge); const pngSuite = new Benchmark.Suite('png'); const minSamples = 64; // jimp pngSuite.add('jimp-buffer-buffer', { defer: true, - fn: function (deferred) { - jimp.read(inputPngBuffer, function (err, image) { - if (err) { - throw err; - } else { - image - .resize(width, height) - .deflateLevel(6) - .filterType(0) - .getBuffer(jimp.MIME_PNG, function (err) { - if (err) { - throw err; - } else { - deferred.resolve(); - } - }); - } - }); + fn: async (deferred) => { + const image = await Jimp.read(inputPngBuffer); + await image + .resize({ w: width, h: heightPng, mode: Jimp.RESIZE_BICUBIC }) + .getBuffer(JimpMime.png, { deflateLevel: 6, filterType: 0 }); + deferred.resolve(); } }).add('jimp-file-file', { defer: true, - fn: function (deferred) { - jimp.read(fixtures.inputPngAlphaPremultiplicationLarge, function (err, image) { - if (err) { - throw err; - } else { - image - .resize(width, height) - .deflateLevel(6) - .filterType(0) - .write(outputPng, function (err) { - if (err) { - throw err; - } else { - deferred.resolve(); - } - }); - } - }); + fn: async (deferred) => { + const image = await Jimp.read(fixtures.inputPngAlphaPremultiplicationLarge); + await image + .resize({ w: width, h: heightPng, mode: Jimp.RESIZE_BICUBIC }) + .write(outputPng, { deflateLevel: 6, filterType: 0 }); + deferred.resolve(); } }); // mapnik - pngSuite.add('mapnik-file-file', { + mapnik && pngSuite.add('mapnik-file-file', { defer: true, - fn: function (deferred) { - mapnik.Image.open(fixtures.inputPngAlphaPremultiplicationLarge, function (err, img) { + fn: (deferred) => { + mapnik.Image.open(fixtures.inputPngAlphaPremultiplicationLarge, (err, img) => { if (err) throw err; - img.premultiply(function (err, img) { + img.premultiply((err, img) => { if (err) throw err; - img.resize(width, height, { + img.resize(width, heightPng, { scaling_method: mapnik.imageScaling.lanczos - }, function (err, img) { + }, (err, img) => { if (err) throw err; - img.demultiply(function (err, img) { + img.demultiply((err, img) => { if (err) throw err; - img.save(outputPng, 'png', function (err) { + img.save(outputPng, 'png32:f=no:z=6', (err) => { if (err) throw err; deferred.resolve(); }); @@ -644,18 +634,18 @@ async.series({ } }).add('mapnik-buffer-buffer', { defer: true, - fn: function (deferred) { - mapnik.Image.fromBytes(inputPngBuffer, { max_size: 3000 }, function (err, img) { + fn: (deferred) => { + mapnik.Image.fromBytes(inputPngBuffer, { max_size: 3000 }, (err, img) => { if (err) throw err; - img.premultiply(function (err, img) { + img.premultiply((err, img) => { if (err) throw err; - img.resize(width, height, { + img.resize(width, heightPng, { scaling_method: mapnik.imageScaling.lanczos - }, function (err, img) { + }, (err, img) => { if (err) throw err; - img.demultiply(function (err, img) { + img.demultiply((err, img) => { if (err) throw err; - img.encode('png', function (err) { + img.encode('png32:f=no:z=6', (err) => { if (err) throw err; deferred.resolve(); }); @@ -668,18 +658,18 @@ async.series({ // imagemagick pngSuite.add('imagemagick-file-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { imagemagick.resize({ srcPath: fixtures.inputPngAlphaPremultiplicationLarge, dstPath: outputPng, - width: width, - height: height, + width, + height: heightPng, filter: 'Lanczos', customArgs: [ '-define', 'PNG:compression-level=6', '-define', 'PNG:compression-filter=0' ] - }, function (err) { + }, (err) => { if (err) { throw err; } else { @@ -691,13 +681,13 @@ async.series({ // gm pngSuite.add('gm-file-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(fixtures.inputPngAlphaPremultiplicationLarge) .filter('Lanczos') - .resize(width, height) + .resize(width, heightPng) .define('PNG:compression-level=6') .define('PNG:compression-filter=0') - .write(outputPng, function (err) { + .write(outputPng, (err) => { if (err) { throw err; } else { @@ -707,17 +697,16 @@ async.series({ } }).add('gm-file-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(fixtures.inputPngAlphaPremultiplicationLarge) .filter('Lanczos') - .resize(width, height) + .resize(width, heightPng) .define('PNG:compression-level=6') .define('PNG:compression-filter=0') - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); @@ -727,11 +716,11 @@ async.series({ pngSuite.add('sharp-buffer-file', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(inputPngBuffer) - .resize(width, height) + .resize(width, heightPng) .png({ compressionLevel: 6 }) - .toFile(outputPng, function (err) { + .toFile(outputPng, (err) => { if (err) { throw err; } else { @@ -742,15 +731,14 @@ async.series({ }).add('sharp-buffer-buffer', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(inputPngBuffer) - .resize(width, height) + .resize(width, heightPng) .png({ compressionLevel: 6 }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); @@ -758,11 +746,11 @@ async.series({ }).add('sharp-file-file', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputPngAlphaPremultiplicationLarge) - .resize(width, height) + .resize(width, heightPng) .png({ compressionLevel: 6 }) - .toFile(outputPng, function (err) { + .toFile(outputPng, (err) => { if (err) { throw err; } else { @@ -773,15 +761,14 @@ async.series({ }).add('sharp-file-buffer', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputPngAlphaPremultiplicationLarge) - .resize(width, height) + .resize(width, heightPng) .png({ compressionLevel: 6 }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); @@ -789,15 +776,14 @@ async.series({ }).add('sharp-progressive', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(inputPngBuffer) - .resize(width, height) + .resize(width, heightPng) .png({ compressionLevel: 6, progressive: true }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); @@ -805,15 +791,14 @@ async.series({ }).add('sharp-adaptiveFiltering', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(inputPngBuffer) - .resize(width, height) + .resize(width, heightPng) .png({ adaptiveFiltering: true, compressionLevel: 6 }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); @@ -821,35 +806,34 @@ async.series({ }).add('sharp-compressionLevel=9', { defer: true, minSamples, - fn: function (deferred) { + fn: (deferred) => { sharp(inputPngBuffer) - .resize(width, height) + .resize(width, heightPng) .png({ compressionLevel: 9 }) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }); - pngSuite.on('cycle', function (event) { - console.log(' png ' + String(event.target)); + pngSuite.on('cycle', (event) => { + console.log(` png ${String(event.target)}`); }).on('complete', function () { callback(null, this.filter('fastest').map('name')); }).run(); }, // WebP - webp: function (callback) { + webp: (callback) => { const inputWebPBuffer = fs.readFileSync(fixtures.inputWebP); (new Benchmark.Suite('webp')).add('sharp-buffer-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputWebPBuffer) .resize(width, height) - .toFile(outputWebP, function (err) { + .toFile(outputWebP, (err) => { if (err) { throw err; } else { @@ -859,24 +843,23 @@ async.series({ } }).add('sharp-buffer-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(inputWebPBuffer) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } }).add('sharp-file-file', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputWebP) .resize(width, height) - .toFile(outputWebP, function (err) { + .toFile(outputWebP, (err) => { if (err) { throw err; } else { @@ -886,29 +869,30 @@ async.series({ } }).add('sharp-file-buffer', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputWebP) .resize(width, height) - .toBuffer(function (err, buffer) { + .toBuffer((err) => { if (err) { throw err; } else { - assert.notStrictEqual(null, buffer); deferred.resolve(); } }); } - }).on('cycle', function (event) { - console.log('webp ' + String(event.target)); + }).on('cycle', (event) => { + console.log(`webp ${String(event.target)}`); }).on('complete', function () { callback(null, this.filter('fastest').map('name')); }).run(); } -}, function (err, results) { - assert(!err, err); - Object.keys(results).forEach(function (format) { +}, (err, results) => { + if (err) { + throw err; + } + Object.keys(results).forEach((format) => { if (results[format].toString().substr(0, 5) !== 'sharp') { - console.log('sharp was slower than ' + results[format] + ' for ' + format); + console.log(`sharp was slower than ${results[format]} for ${format}`); } }); console.dir(sharp.cache()); diff --git a/test/bench/random.js b/test/bench/random.js index b092d218a..33a2198da 100644 --- a/test/bench/random.js +++ b/test/bench/random.js @@ -1,26 +1,26 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const imagemagick = require('imagemagick'); const gm = require('gm'); -const assert = require('assert'); +const assert = require('node:assert'); const Benchmark = require('benchmark'); const sharp = require('../../'); const fixtures = require('../fixtures'); sharp.cache(false); -sharp.simd(true); const min = 320; const max = 960; -const randomDimension = function () { - return Math.ceil((Math.random() * (max - min)) + min); -}; +const randomDimension = () => Math.ceil((Math.random() * (max - min)) + min); new Benchmark.Suite('random').add('imagemagick', { defer: true, - fn: function (deferred) { + fn: (deferred) => { imagemagick.resize({ srcPath: fixtures.inputJpg, dstPath: fixtures.path('output.jpg'), @@ -29,7 +29,7 @@ new Benchmark.Suite('random').add('imagemagick', { height: randomDimension(), format: 'jpg', filter: 'Lanczos' - }, function (err) { + }, (err) => { if (err) { throw err; } else { @@ -39,12 +39,12 @@ new Benchmark.Suite('random').add('imagemagick', { } }).add('gm', { defer: true, - fn: function (deferred) { + fn: (deferred) => { gm(fixtures.inputJpg) .resize(randomDimension(), randomDimension()) .filter('Lanczos') .quality(80) - .toBuffer(function (err, buffer) { + .toBuffer((err, buffer) => { if (err) { throw err; } else { @@ -55,10 +55,10 @@ new Benchmark.Suite('random').add('imagemagick', { } }).add('sharp', { defer: true, - fn: function (deferred) { + fn: (deferred) => { sharp(fixtures.inputJpg) .resize(randomDimension(), randomDimension()) - .toBuffer(function (err, buffer) { + .toBuffer((err, buffer) => { if (err) { throw err; } else { @@ -67,9 +67,9 @@ new Benchmark.Suite('random').add('imagemagick', { } }); } -}).on('cycle', function (event) { +}).on('cycle', (event) => { console.log(String(event.target)); }).on('complete', function () { const winner = this.filter('fastest').map('name'); - assert.strictEqual('sharp', String(winner), 'sharp was slower than ' + winner); + assert.strictEqual('sharp', String(winner), `sharp was slower than ${winner}`); }).run(); diff --git a/test/bench/run-with-docker.sh b/test/bench/run-with-docker.sh new file mode 100755 index 000000000..d28975bfb --- /dev/null +++ b/test/bench/run-with-docker.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +if ! type docker >/dev/null; then + echo "Please install docker" + exit 1 +fi + +BRANCH=$(git branch --show-current) +echo "Running sharp performance tests using $BRANCH branch" + +docker build --build-arg "BRANCH=$BRANCH" -t sharp-test-bench . +docker run --rm -it sharp-test-bench diff --git a/test/coverage/report.sh b/test/coverage/report.sh deleted file mode 100755 index 1c0b5459e..000000000 --- a/test/coverage/report.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -CPPFLAGS="--coverage" LDFLAGS="--coverage" npm rebuild -npm test -geninfo --no-external --base-directory src --output-file coverage/sharp.info build/Release/obj.target/sharp/src -genhtml --title sharp --demangle-cpp --output-directory coverage/sharp coverage/*.info diff --git a/test/fixtures/65536-uint32-limit.png b/test/fixtures/65536-uint32-limit.png new file mode 100644 index 000000000..e86f608ed Binary files /dev/null and b/test/fixtures/65536-uint32-limit.png differ diff --git a/test/fixtures/Flag_of_the_Netherlands-16bit.png b/test/fixtures/Flag_of_the_Netherlands-16bit.png new file mode 100644 index 000000000..61ee36d53 Binary files /dev/null and b/test/fixtures/Flag_of_the_Netherlands-16bit.png differ diff --git a/test/fixtures/Flag_of_the_Netherlands-alpha.png b/test/fixtures/Flag_of_the_Netherlands-alpha.png new file mode 100644 index 000000000..35a21b4ac Binary files /dev/null and b/test/fixtures/Flag_of_the_Netherlands-alpha.png differ diff --git a/test/fixtures/Flag_of_the_Netherlands.png b/test/fixtures/Flag_of_the_Netherlands.png new file mode 100644 index 000000000..c336a3e29 Binary files /dev/null and b/test/fixtures/Flag_of_the_Netherlands.png differ diff --git a/test/fixtures/XCMYK 2017.icc b/test/fixtures/XCMYK 2017.icc new file mode 100644 index 000000000..ec9aae7d4 Binary files /dev/null and b/test/fixtures/XCMYK 2017.icc differ diff --git a/test/fixtures/bgbn4a08.png b/test/fixtures/bgbn4a08.png new file mode 100644 index 000000000..7cbefc3bf Binary files /dev/null and b/test/fixtures/bgbn4a08.png differ diff --git a/test/fixtures/bggn4a16.png b/test/fixtures/bggn4a16.png new file mode 100644 index 000000000..13fd85ba1 Binary files /dev/null and b/test/fixtures/bggn4a16.png differ diff --git a/test/fixtures/bonne.geo.tif b/test/fixtures/bonne.geo.tif new file mode 100644 index 000000000..1592bf86c Binary files /dev/null and b/test/fixtures/bonne.geo.tif differ diff --git a/test/fixtures/cielab-dagams.tiff b/test/fixtures/cielab-dagams.tiff index 3a7680250..452f3a026 100644 Binary files a/test/fixtures/cielab-dagams.tiff and b/test/fixtures/cielab-dagams.tiff differ diff --git a/test/fixtures/concert.jpg b/test/fixtures/concert.jpg new file mode 100644 index 000000000..7591c9199 Binary files /dev/null and b/test/fixtures/concert.jpg differ diff --git a/test/fixtures/d.png b/test/fixtures/d.png new file mode 100644 index 000000000..765420660 Binary files /dev/null and b/test/fixtures/d.png differ diff --git a/test/fixtures/dot-and-lines.png b/test/fixtures/dot-and-lines.png new file mode 100644 index 000000000..5c50d2456 Binary files /dev/null and b/test/fixtures/dot-and-lines.png differ diff --git a/test/fixtures/expected/Landscape_1_flip-out.jpg b/test/fixtures/expected/Landscape_1_flip-out.jpg new file mode 100644 index 000000000..5d9c7b1ce Binary files /dev/null and b/test/fixtures/expected/Landscape_1_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_1_flip_flop-out.jpg b/test/fixtures/expected/Landscape_1_flip_flop-out.jpg new file mode 100644 index 000000000..8df75ba73 Binary files /dev/null and b/test/fixtures/expected/Landscape_1_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_1_flop-out.jpg b/test/fixtures/expected/Landscape_1_flop-out.jpg new file mode 100644 index 000000000..f68ba94cf Binary files /dev/null and b/test/fixtures/expected/Landscape_1_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_1_rotate180-out.jpg b/test/fixtures/expected/Landscape_1_rotate180-out.jpg new file mode 100644 index 000000000..8df75ba73 Binary files /dev/null and b/test/fixtures/expected/Landscape_1_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_1_rotate270-out.jpg b/test/fixtures/expected/Landscape_1_rotate270-out.jpg new file mode 100644 index 000000000..378b021da Binary files /dev/null and b/test/fixtures/expected/Landscape_1_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_1_rotate45-out.jpg b/test/fixtures/expected/Landscape_1_rotate45-out.jpg new file mode 100644 index 000000000..e195b9d4f Binary files /dev/null and b/test/fixtures/expected/Landscape_1_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_1_rotate90-out.jpg b/test/fixtures/expected/Landscape_1_rotate90-out.jpg new file mode 100644 index 000000000..e01b9088c Binary files /dev/null and b/test/fixtures/expected/Landscape_1_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_flip-out.jpg b/test/fixtures/expected/Landscape_2_flip-out.jpg new file mode 100644 index 000000000..1a54946ff Binary files /dev/null and b/test/fixtures/expected/Landscape_2_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_flip_flop-out.jpg b/test/fixtures/expected/Landscape_2_flip_flop-out.jpg new file mode 100644 index 000000000..382b73246 Binary files /dev/null and b/test/fixtures/expected/Landscape_2_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_flop-out.jpg b/test/fixtures/expected/Landscape_2_flop-out.jpg new file mode 100644 index 000000000..5d260769f Binary files /dev/null and b/test/fixtures/expected/Landscape_2_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_rotate180-out.jpg b/test/fixtures/expected/Landscape_2_rotate180-out.jpg new file mode 100644 index 000000000..382b73246 Binary files /dev/null and b/test/fixtures/expected/Landscape_2_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_rotate270-out.jpg b/test/fixtures/expected/Landscape_2_rotate270-out.jpg new file mode 100644 index 000000000..6627376bf Binary files /dev/null and b/test/fixtures/expected/Landscape_2_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_rotate45-out.jpg b/test/fixtures/expected/Landscape_2_rotate45-out.jpg new file mode 100644 index 000000000..d46a3540a Binary files /dev/null and b/test/fixtures/expected/Landscape_2_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_2_rotate90-out.jpg b/test/fixtures/expected/Landscape_2_rotate90-out.jpg new file mode 100644 index 000000000..0600f6ebe Binary files /dev/null and b/test/fixtures/expected/Landscape_2_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_flip-out.jpg b/test/fixtures/expected/Landscape_3_flip-out.jpg new file mode 100644 index 000000000..2a9d24045 Binary files /dev/null and b/test/fixtures/expected/Landscape_3_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_flip_flop-out.jpg b/test/fixtures/expected/Landscape_3_flip_flop-out.jpg new file mode 100644 index 000000000..b3eb2e870 Binary files /dev/null and b/test/fixtures/expected/Landscape_3_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_flop-out.jpg b/test/fixtures/expected/Landscape_3_flop-out.jpg new file mode 100644 index 000000000..0a105d958 Binary files /dev/null and b/test/fixtures/expected/Landscape_3_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_rotate180-out.jpg b/test/fixtures/expected/Landscape_3_rotate180-out.jpg new file mode 100644 index 000000000..b3eb2e870 Binary files /dev/null and b/test/fixtures/expected/Landscape_3_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_rotate270-out.jpg b/test/fixtures/expected/Landscape_3_rotate270-out.jpg new file mode 100644 index 000000000..98c3bcc4f Binary files /dev/null and b/test/fixtures/expected/Landscape_3_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_rotate45-out.jpg b/test/fixtures/expected/Landscape_3_rotate45-out.jpg new file mode 100644 index 000000000..6f74fdb38 Binary files /dev/null and b/test/fixtures/expected/Landscape_3_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_3_rotate90-out.jpg b/test/fixtures/expected/Landscape_3_rotate90-out.jpg new file mode 100644 index 000000000..3026268ba Binary files /dev/null and b/test/fixtures/expected/Landscape_3_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_flip-out.jpg b/test/fixtures/expected/Landscape_4_flip-out.jpg new file mode 100644 index 000000000..6cb2e8e59 Binary files /dev/null and b/test/fixtures/expected/Landscape_4_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_flip_flop-out.jpg b/test/fixtures/expected/Landscape_4_flip_flop-out.jpg new file mode 100644 index 000000000..440c23746 Binary files /dev/null and b/test/fixtures/expected/Landscape_4_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_flop-out.jpg b/test/fixtures/expected/Landscape_4_flop-out.jpg new file mode 100644 index 000000000..aa9d3df92 Binary files /dev/null and b/test/fixtures/expected/Landscape_4_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_rotate180-out.jpg b/test/fixtures/expected/Landscape_4_rotate180-out.jpg new file mode 100644 index 000000000..440c23746 Binary files /dev/null and b/test/fixtures/expected/Landscape_4_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_rotate270-out.jpg b/test/fixtures/expected/Landscape_4_rotate270-out.jpg new file mode 100644 index 000000000..55d17a1a7 Binary files /dev/null and b/test/fixtures/expected/Landscape_4_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_rotate45-out.jpg b/test/fixtures/expected/Landscape_4_rotate45-out.jpg new file mode 100644 index 000000000..76237462d Binary files /dev/null and b/test/fixtures/expected/Landscape_4_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_4_rotate90-out.jpg b/test/fixtures/expected/Landscape_4_rotate90-out.jpg new file mode 100644 index 000000000..c3543e07c Binary files /dev/null and b/test/fixtures/expected/Landscape_4_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_flip-out.jpg b/test/fixtures/expected/Landscape_5_flip-out.jpg new file mode 100644 index 000000000..7b0474859 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_flip_flop-out.jpg b/test/fixtures/expected/Landscape_5_flip_flop-out.jpg new file mode 100644 index 000000000..06a1fb920 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_flop-out.jpg b/test/fixtures/expected/Landscape_5_flop-out.jpg new file mode 100644 index 000000000..bd59dff28 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_rotate180-out.jpg b/test/fixtures/expected/Landscape_5_rotate180-out.jpg new file mode 100644 index 000000000..06a1fb920 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_rotate270-out.jpg b/test/fixtures/expected/Landscape_5_rotate270-out.jpg new file mode 100644 index 000000000..06d772522 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_rotate45-out.jpg b/test/fixtures/expected/Landscape_5_rotate45-out.jpg new file mode 100644 index 000000000..0df79d2b8 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_5_rotate90-out.jpg b/test/fixtures/expected/Landscape_5_rotate90-out.jpg new file mode 100644 index 000000000..ad21a7d85 Binary files /dev/null and b/test/fixtures/expected/Landscape_5_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_flip-out.jpg b/test/fixtures/expected/Landscape_6_flip-out.jpg new file mode 100644 index 000000000..c8012cbea Binary files /dev/null and b/test/fixtures/expected/Landscape_6_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_flip_flop-out.jpg b/test/fixtures/expected/Landscape_6_flip_flop-out.jpg new file mode 100644 index 000000000..ad093bff0 Binary files /dev/null and b/test/fixtures/expected/Landscape_6_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_flop-out.jpg b/test/fixtures/expected/Landscape_6_flop-out.jpg new file mode 100644 index 000000000..86af2d2e6 Binary files /dev/null and b/test/fixtures/expected/Landscape_6_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_rotate180-out.jpg b/test/fixtures/expected/Landscape_6_rotate180-out.jpg new file mode 100644 index 000000000..ad093bff0 Binary files /dev/null and b/test/fixtures/expected/Landscape_6_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_rotate270-out.jpg b/test/fixtures/expected/Landscape_6_rotate270-out.jpg new file mode 100644 index 000000000..4cbbff54f Binary files /dev/null and b/test/fixtures/expected/Landscape_6_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_rotate45-out.jpg b/test/fixtures/expected/Landscape_6_rotate45-out.jpg new file mode 100644 index 000000000..07a109560 Binary files /dev/null and b/test/fixtures/expected/Landscape_6_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_6_rotate90-out.jpg b/test/fixtures/expected/Landscape_6_rotate90-out.jpg new file mode 100644 index 000000000..6fe106499 Binary files /dev/null and b/test/fixtures/expected/Landscape_6_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_flip-out.jpg b/test/fixtures/expected/Landscape_7_flip-out.jpg new file mode 100644 index 000000000..ed14166cb Binary files /dev/null and b/test/fixtures/expected/Landscape_7_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_flip_flop-out.jpg b/test/fixtures/expected/Landscape_7_flip_flop-out.jpg new file mode 100644 index 000000000..522130cc1 Binary files /dev/null and b/test/fixtures/expected/Landscape_7_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_flop-out.jpg b/test/fixtures/expected/Landscape_7_flop-out.jpg new file mode 100644 index 000000000..a792fab3d Binary files /dev/null and b/test/fixtures/expected/Landscape_7_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_rotate180-out.jpg b/test/fixtures/expected/Landscape_7_rotate180-out.jpg new file mode 100644 index 000000000..522130cc1 Binary files /dev/null and b/test/fixtures/expected/Landscape_7_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_rotate270-out.jpg b/test/fixtures/expected/Landscape_7_rotate270-out.jpg new file mode 100644 index 000000000..b7bbae5b9 Binary files /dev/null and b/test/fixtures/expected/Landscape_7_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_rotate45-out.jpg b/test/fixtures/expected/Landscape_7_rotate45-out.jpg new file mode 100644 index 000000000..76881c29d Binary files /dev/null and b/test/fixtures/expected/Landscape_7_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_7_rotate90-out.jpg b/test/fixtures/expected/Landscape_7_rotate90-out.jpg new file mode 100644 index 000000000..b33cd9cb2 Binary files /dev/null and b/test/fixtures/expected/Landscape_7_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_flip-out.jpg b/test/fixtures/expected/Landscape_8_flip-out.jpg new file mode 100644 index 000000000..7ab9fd406 Binary files /dev/null and b/test/fixtures/expected/Landscape_8_flip-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_flip_flop-out.jpg b/test/fixtures/expected/Landscape_8_flip_flop-out.jpg new file mode 100644 index 000000000..e931394c9 Binary files /dev/null and b/test/fixtures/expected/Landscape_8_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_flop-out.jpg b/test/fixtures/expected/Landscape_8_flop-out.jpg new file mode 100644 index 000000000..ea275beb6 Binary files /dev/null and b/test/fixtures/expected/Landscape_8_flop-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_rotate180-out.jpg b/test/fixtures/expected/Landscape_8_rotate180-out.jpg new file mode 100644 index 000000000..e931394c9 Binary files /dev/null and b/test/fixtures/expected/Landscape_8_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_rotate270-out.jpg b/test/fixtures/expected/Landscape_8_rotate270-out.jpg new file mode 100644 index 000000000..09beada7d Binary files /dev/null and b/test/fixtures/expected/Landscape_8_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_rotate45-out.jpg b/test/fixtures/expected/Landscape_8_rotate45-out.jpg new file mode 100644 index 000000000..cd0157665 Binary files /dev/null and b/test/fixtures/expected/Landscape_8_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Landscape_8_rotate90-out.jpg b/test/fixtures/expected/Landscape_8_rotate90-out.jpg new file mode 100644 index 000000000..d806c013b Binary files /dev/null and b/test/fixtures/expected/Landscape_8_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_flip-out.jpg b/test/fixtures/expected/Portrait_1_flip-out.jpg new file mode 100644 index 000000000..13fbbd666 Binary files /dev/null and b/test/fixtures/expected/Portrait_1_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_flip_flop-out.jpg b/test/fixtures/expected/Portrait_1_flip_flop-out.jpg new file mode 100644 index 000000000..30b748595 Binary files /dev/null and b/test/fixtures/expected/Portrait_1_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_flop-out.jpg b/test/fixtures/expected/Portrait_1_flop-out.jpg new file mode 100644 index 000000000..09453d1d0 Binary files /dev/null and b/test/fixtures/expected/Portrait_1_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_rotate180-out.jpg b/test/fixtures/expected/Portrait_1_rotate180-out.jpg new file mode 100644 index 000000000..30b748595 Binary files /dev/null and b/test/fixtures/expected/Portrait_1_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_rotate270-out.jpg b/test/fixtures/expected/Portrait_1_rotate270-out.jpg new file mode 100644 index 000000000..941ae8edd Binary files /dev/null and b/test/fixtures/expected/Portrait_1_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_rotate45-out.jpg b/test/fixtures/expected/Portrait_1_rotate45-out.jpg new file mode 100644 index 000000000..d93168893 Binary files /dev/null and b/test/fixtures/expected/Portrait_1_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_1_rotate90-out.jpg b/test/fixtures/expected/Portrait_1_rotate90-out.jpg new file mode 100644 index 000000000..cd91b58e0 Binary files /dev/null and b/test/fixtures/expected/Portrait_1_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_flip-out.jpg b/test/fixtures/expected/Portrait_2_flip-out.jpg new file mode 100644 index 000000000..af48a72c8 Binary files /dev/null and b/test/fixtures/expected/Portrait_2_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_flip_flop-out.jpg b/test/fixtures/expected/Portrait_2_flip_flop-out.jpg new file mode 100644 index 000000000..7e83d64fc Binary files /dev/null and b/test/fixtures/expected/Portrait_2_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_flop-out.jpg b/test/fixtures/expected/Portrait_2_flop-out.jpg new file mode 100644 index 000000000..f746afbfd Binary files /dev/null and b/test/fixtures/expected/Portrait_2_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_rotate180-out.jpg b/test/fixtures/expected/Portrait_2_rotate180-out.jpg new file mode 100644 index 000000000..7e83d64fc Binary files /dev/null and b/test/fixtures/expected/Portrait_2_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_rotate270-out.jpg b/test/fixtures/expected/Portrait_2_rotate270-out.jpg new file mode 100644 index 000000000..08ab320d0 Binary files /dev/null and b/test/fixtures/expected/Portrait_2_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_rotate45-out.jpg b/test/fixtures/expected/Portrait_2_rotate45-out.jpg new file mode 100644 index 000000000..81251c9bb Binary files /dev/null and b/test/fixtures/expected/Portrait_2_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_2_rotate90-out.jpg b/test/fixtures/expected/Portrait_2_rotate90-out.jpg new file mode 100644 index 000000000..db4ed9799 Binary files /dev/null and b/test/fixtures/expected/Portrait_2_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_flip-out.jpg b/test/fixtures/expected/Portrait_3_flip-out.jpg new file mode 100644 index 000000000..43aa137af Binary files /dev/null and b/test/fixtures/expected/Portrait_3_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_flip_flop-out.jpg b/test/fixtures/expected/Portrait_3_flip_flop-out.jpg new file mode 100644 index 000000000..2b7f34d17 Binary files /dev/null and b/test/fixtures/expected/Portrait_3_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_flop-out.jpg b/test/fixtures/expected/Portrait_3_flop-out.jpg new file mode 100644 index 000000000..654dbb5f5 Binary files /dev/null and b/test/fixtures/expected/Portrait_3_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_rotate180-out.jpg b/test/fixtures/expected/Portrait_3_rotate180-out.jpg new file mode 100644 index 000000000..2b7f34d17 Binary files /dev/null and b/test/fixtures/expected/Portrait_3_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_rotate270-out.jpg b/test/fixtures/expected/Portrait_3_rotate270-out.jpg new file mode 100644 index 000000000..d8cc2d087 Binary files /dev/null and b/test/fixtures/expected/Portrait_3_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_rotate45-out.jpg b/test/fixtures/expected/Portrait_3_rotate45-out.jpg new file mode 100644 index 000000000..d282dd93f Binary files /dev/null and b/test/fixtures/expected/Portrait_3_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_3_rotate90-out.jpg b/test/fixtures/expected/Portrait_3_rotate90-out.jpg new file mode 100644 index 000000000..3f668810b Binary files /dev/null and b/test/fixtures/expected/Portrait_3_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_flip-out.jpg b/test/fixtures/expected/Portrait_4_flip-out.jpg new file mode 100644 index 000000000..99d25b360 Binary files /dev/null and b/test/fixtures/expected/Portrait_4_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_flip_flop-out.jpg b/test/fixtures/expected/Portrait_4_flip_flop-out.jpg new file mode 100644 index 000000000..4ba5e0f7f Binary files /dev/null and b/test/fixtures/expected/Portrait_4_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_flop-out.jpg b/test/fixtures/expected/Portrait_4_flop-out.jpg new file mode 100644 index 000000000..aecd7d145 Binary files /dev/null and b/test/fixtures/expected/Portrait_4_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_rotate180-out.jpg b/test/fixtures/expected/Portrait_4_rotate180-out.jpg new file mode 100644 index 000000000..4ba5e0f7f Binary files /dev/null and b/test/fixtures/expected/Portrait_4_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_rotate270-out.jpg b/test/fixtures/expected/Portrait_4_rotate270-out.jpg new file mode 100644 index 000000000..5e3833c30 Binary files /dev/null and b/test/fixtures/expected/Portrait_4_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_rotate45-out.jpg b/test/fixtures/expected/Portrait_4_rotate45-out.jpg new file mode 100644 index 000000000..f110605dd Binary files /dev/null and b/test/fixtures/expected/Portrait_4_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_4_rotate90-out.jpg b/test/fixtures/expected/Portrait_4_rotate90-out.jpg new file mode 100644 index 000000000..913626d78 Binary files /dev/null and b/test/fixtures/expected/Portrait_4_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_flip-out.jpg b/test/fixtures/expected/Portrait_5_flip-out.jpg new file mode 100644 index 000000000..e15c97f6c Binary files /dev/null and b/test/fixtures/expected/Portrait_5_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_flip_flop-out.jpg b/test/fixtures/expected/Portrait_5_flip_flop-out.jpg new file mode 100644 index 000000000..82c999c8d Binary files /dev/null and b/test/fixtures/expected/Portrait_5_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_flop-out.jpg b/test/fixtures/expected/Portrait_5_flop-out.jpg new file mode 100644 index 000000000..fdea1e992 Binary files /dev/null and b/test/fixtures/expected/Portrait_5_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_rotate180-out.jpg b/test/fixtures/expected/Portrait_5_rotate180-out.jpg new file mode 100644 index 000000000..82c999c8d Binary files /dev/null and b/test/fixtures/expected/Portrait_5_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_rotate270-out.jpg b/test/fixtures/expected/Portrait_5_rotate270-out.jpg new file mode 100644 index 000000000..ff7044004 Binary files /dev/null and b/test/fixtures/expected/Portrait_5_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_rotate45-out.jpg b/test/fixtures/expected/Portrait_5_rotate45-out.jpg new file mode 100644 index 000000000..843fd8f0a Binary files /dev/null and b/test/fixtures/expected/Portrait_5_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_5_rotate90-out.jpg b/test/fixtures/expected/Portrait_5_rotate90-out.jpg new file mode 100644 index 000000000..5547e7df4 Binary files /dev/null and b/test/fixtures/expected/Portrait_5_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_flip-out.jpg b/test/fixtures/expected/Portrait_6_flip-out.jpg new file mode 100644 index 000000000..a7bd24c23 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_flip_flop-out.jpg b/test/fixtures/expected/Portrait_6_flip_flop-out.jpg new file mode 100644 index 000000000..c35bbb351 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_flop-out.jpg b/test/fixtures/expected/Portrait_6_flop-out.jpg new file mode 100644 index 000000000..35e7d14e6 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_rotate180-out.jpg b/test/fixtures/expected/Portrait_6_rotate180-out.jpg new file mode 100644 index 000000000..c35bbb351 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_rotate270-out.jpg b/test/fixtures/expected/Portrait_6_rotate270-out.jpg new file mode 100644 index 000000000..d7e093d81 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_rotate45-out.jpg b/test/fixtures/expected/Portrait_6_rotate45-out.jpg new file mode 100644 index 000000000..713bb3ee0 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_6_rotate90-out.jpg b/test/fixtures/expected/Portrait_6_rotate90-out.jpg new file mode 100644 index 000000000..cb6c108d2 Binary files /dev/null and b/test/fixtures/expected/Portrait_6_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_flip-out.jpg b/test/fixtures/expected/Portrait_7_flip-out.jpg new file mode 100644 index 000000000..3c89dc8b9 Binary files /dev/null and b/test/fixtures/expected/Portrait_7_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_flip_flop-out.jpg b/test/fixtures/expected/Portrait_7_flip_flop-out.jpg new file mode 100644 index 000000000..ee99439d4 Binary files /dev/null and b/test/fixtures/expected/Portrait_7_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_flop-out.jpg b/test/fixtures/expected/Portrait_7_flop-out.jpg new file mode 100644 index 000000000..2dba39f88 Binary files /dev/null and b/test/fixtures/expected/Portrait_7_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_rotate180-out.jpg b/test/fixtures/expected/Portrait_7_rotate180-out.jpg new file mode 100644 index 000000000..ee99439d4 Binary files /dev/null and b/test/fixtures/expected/Portrait_7_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_rotate270-out.jpg b/test/fixtures/expected/Portrait_7_rotate270-out.jpg new file mode 100644 index 000000000..6756e61be Binary files /dev/null and b/test/fixtures/expected/Portrait_7_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_rotate45-out.jpg b/test/fixtures/expected/Portrait_7_rotate45-out.jpg new file mode 100644 index 000000000..f8d60b808 Binary files /dev/null and b/test/fixtures/expected/Portrait_7_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_7_rotate90-out.jpg b/test/fixtures/expected/Portrait_7_rotate90-out.jpg new file mode 100644 index 000000000..879d2c705 Binary files /dev/null and b/test/fixtures/expected/Portrait_7_rotate90-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_flip-out.jpg b/test/fixtures/expected/Portrait_8_flip-out.jpg new file mode 100644 index 000000000..e46300273 Binary files /dev/null and b/test/fixtures/expected/Portrait_8_flip-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_flip_flop-out.jpg b/test/fixtures/expected/Portrait_8_flip_flop-out.jpg new file mode 100644 index 000000000..2f4f2ce27 Binary files /dev/null and b/test/fixtures/expected/Portrait_8_flip_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_flop-out.jpg b/test/fixtures/expected/Portrait_8_flop-out.jpg new file mode 100644 index 000000000..83e5e9d04 Binary files /dev/null and b/test/fixtures/expected/Portrait_8_flop-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_rotate180-out.jpg b/test/fixtures/expected/Portrait_8_rotate180-out.jpg new file mode 100644 index 000000000..2f4f2ce27 Binary files /dev/null and b/test/fixtures/expected/Portrait_8_rotate180-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_rotate270-out.jpg b/test/fixtures/expected/Portrait_8_rotate270-out.jpg new file mode 100644 index 000000000..b0bb7296a Binary files /dev/null and b/test/fixtures/expected/Portrait_8_rotate270-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_rotate45-out.jpg b/test/fixtures/expected/Portrait_8_rotate45-out.jpg new file mode 100644 index 000000000..482c70ae2 Binary files /dev/null and b/test/fixtures/expected/Portrait_8_rotate45-out.jpg differ diff --git a/test/fixtures/expected/Portrait_8_rotate90-out.jpg b/test/fixtures/expected/Portrait_8_rotate90-out.jpg new file mode 100644 index 000000000..695ff0274 Binary files /dev/null and b/test/fixtures/expected/Portrait_8_rotate90-out.jpg differ diff --git a/test/fixtures/expected/alpha-layer-1-fill-linear.png b/test/fixtures/expected/alpha-layer-1-fill-linear.png index 0b92c4f6b..abd22a6e6 100644 Binary files a/test/fixtures/expected/alpha-layer-1-fill-linear.png and b/test/fixtures/expected/alpha-layer-1-fill-linear.png differ diff --git a/test/fixtures/expected/alpha-layer-1-fill-offset.png b/test/fixtures/expected/alpha-layer-1-fill-offset.png index b9602411f..9c8a398bc 100644 Binary files a/test/fixtures/expected/alpha-layer-1-fill-offset.png and b/test/fixtures/expected/alpha-layer-1-fill-offset.png differ diff --git a/test/fixtures/expected/alpha-layer-1-fill-slope.png b/test/fixtures/expected/alpha-layer-1-fill-slope.png index a02cd31ae..c9a277dcb 100644 Binary files a/test/fixtures/expected/alpha-layer-1-fill-slope.png and b/test/fixtures/expected/alpha-layer-1-fill-slope.png differ diff --git a/test/fixtures/expected/clahe-100-100-0.jpg b/test/fixtures/expected/clahe-100-100-0.jpg new file mode 100644 index 000000000..119bdd9a1 Binary files /dev/null and b/test/fixtures/expected/clahe-100-100-0.jpg differ diff --git a/test/fixtures/expected/clahe-100-50-3.jpg b/test/fixtures/expected/clahe-100-50-3.jpg new file mode 100644 index 000000000..b4c248169 Binary files /dev/null and b/test/fixtures/expected/clahe-100-50-3.jpg differ diff --git a/test/fixtures/expected/clahe-11-25-14.jpg b/test/fixtures/expected/clahe-11-25-14.jpg new file mode 100644 index 000000000..067b79d8b Binary files /dev/null and b/test/fixtures/expected/clahe-11-25-14.jpg differ diff --git a/test/fixtures/expected/clahe-5-5-0.jpg b/test/fixtures/expected/clahe-5-5-0.jpg new file mode 100644 index 000000000..7a70f8bb6 Binary files /dev/null and b/test/fixtures/expected/clahe-5-5-0.jpg differ diff --git a/test/fixtures/expected/clahe-5-5-5.jpg b/test/fixtures/expected/clahe-5-5-5.jpg new file mode 100644 index 000000000..5cea17a15 Binary files /dev/null and b/test/fixtures/expected/clahe-5-5-5.jpg differ diff --git a/test/fixtures/expected/clahe-50-50-0.jpg b/test/fixtures/expected/clahe-50-50-0.jpg new file mode 100644 index 000000000..f56f4b88e Binary files /dev/null and b/test/fixtures/expected/clahe-50-50-0.jpg differ diff --git a/test/fixtures/expected/clahe-50-50-14.jpg b/test/fixtures/expected/clahe-50-50-14.jpg new file mode 100644 index 000000000..b9d4244b7 Binary files /dev/null and b/test/fixtures/expected/clahe-50-50-14.jpg differ diff --git a/test/fixtures/expected/colourspace-gradients-gamma-resize.png b/test/fixtures/expected/colourspace-gradients-gamma-resize.png new file mode 100644 index 000000000..5fc9ca7bf Binary files /dev/null and b/test/fixtures/expected/colourspace-gradients-gamma-resize.png differ diff --git a/test/fixtures/expected/colourspace.cmyk-to-cmyk-negated.tif b/test/fixtures/expected/colourspace.cmyk-to-cmyk-negated.tif new file mode 100644 index 000000000..e1f1b60e9 Binary files /dev/null and b/test/fixtures/expected/colourspace.cmyk-to-cmyk-negated.tif differ diff --git a/test/fixtures/expected/composite-autoOrient.jpg b/test/fixtures/expected/composite-autoOrient.jpg new file mode 100644 index 000000000..75cf137f1 Binary files /dev/null and b/test/fixtures/expected/composite-autoOrient.jpg differ diff --git a/test/fixtures/expected/composite-cutout.png b/test/fixtures/expected/composite-cutout.png index 5f75e8fff..b98f2c35c 100644 Binary files a/test/fixtures/expected/composite-cutout.png and b/test/fixtures/expected/composite-cutout.png differ diff --git a/test/fixtures/expected/composite-multiple.png b/test/fixtures/expected/composite-multiple.png index d72d1eda3..18671a39b 100644 Binary files a/test/fixtures/expected/composite-multiple.png and b/test/fixtures/expected/composite-multiple.png differ diff --git a/test/fixtures/expected/composite-red-scrgb.png b/test/fixtures/expected/composite-red-scrgb.png new file mode 100644 index 000000000..fd2bdf940 Binary files /dev/null and b/test/fixtures/expected/composite-red-scrgb.png differ diff --git a/test/fixtures/expected/composite.blend.dest-over.png b/test/fixtures/expected/composite.blend.dest-over.png index bda1c9ddf..f7f7b1eb9 100644 Binary files a/test/fixtures/expected/composite.blend.dest-over.png and b/test/fixtures/expected/composite.blend.dest-over.png differ diff --git a/test/fixtures/expected/composite.blend.over.png b/test/fixtures/expected/composite.blend.over.png index ce5e0bb4e..8c58c402c 100644 Binary files a/test/fixtures/expected/composite.blend.over.png and b/test/fixtures/expected/composite.blend.over.png differ diff --git a/test/fixtures/expected/composite.blend.saturate.png b/test/fixtures/expected/composite.blend.saturate.png index ce22bf256..6c77b7fd2 100644 Binary files a/test/fixtures/expected/composite.blend.saturate.png and b/test/fixtures/expected/composite.blend.saturate.png differ diff --git a/test/fixtures/expected/composite.blend.xor.png b/test/fixtures/expected/composite.blend.xor.png index 01160d953..916eaf776 100644 Binary files a/test/fixtures/expected/composite.blend.xor.png and b/test/fixtures/expected/composite.blend.xor.png differ diff --git a/test/fixtures/expected/crop-strategy.webp b/test/fixtures/expected/crop-strategy.webp new file mode 100644 index 000000000..857a471af Binary files /dev/null and b/test/fixtures/expected/crop-strategy.webp differ diff --git a/test/fixtures/expected/d-opacity-30.png b/test/fixtures/expected/d-opacity-30.png new file mode 100644 index 000000000..d053cfa2b Binary files /dev/null and b/test/fixtures/expected/d-opacity-30.png differ diff --git a/test/fixtures/expected/dilate-1.png b/test/fixtures/expected/dilate-1.png new file mode 100644 index 000000000..947eb4b0c Binary files /dev/null and b/test/fixtures/expected/dilate-1.png differ diff --git a/test/fixtures/expected/embed-animated-height.webp b/test/fixtures/expected/embed-animated-height.webp new file mode 100644 index 000000000..a546307d9 Binary files /dev/null and b/test/fixtures/expected/embed-animated-height.webp differ diff --git a/test/fixtures/expected/embed-animated-width.webp b/test/fixtures/expected/embed-animated-width.webp new file mode 100644 index 000000000..e11efa686 Binary files /dev/null and b/test/fixtures/expected/embed-animated-width.webp differ diff --git a/test/fixtures/expected/embed-lab-into-rgba.png b/test/fixtures/expected/embed-lab-into-rgba.png index 23c5d9ae6..6e48a7213 100644 Binary files a/test/fixtures/expected/embed-lab-into-rgba.png and b/test/fixtures/expected/embed-lab-into-rgba.png differ diff --git a/test/fixtures/expected/erode-1.png b/test/fixtures/expected/erode-1.png new file mode 100644 index 000000000..54ff8035b Binary files /dev/null and b/test/fixtures/expected/erode-1.png differ diff --git a/test/fixtures/expected/extend-2channel-background.png b/test/fixtures/expected/extend-2channel-background.png new file mode 100644 index 000000000..e29a5ebac Binary files /dev/null and b/test/fixtures/expected/extend-2channel-background.png differ diff --git a/test/fixtures/expected/extend-2channel-copy.png b/test/fixtures/expected/extend-2channel-copy.png new file mode 100644 index 000000000..e29a5ebac Binary files /dev/null and b/test/fixtures/expected/extend-2channel-copy.png differ diff --git a/test/fixtures/expected/extend-2channel-mirror.png b/test/fixtures/expected/extend-2channel-mirror.png new file mode 100644 index 000000000..9a0ea8f98 Binary files /dev/null and b/test/fixtures/expected/extend-2channel-mirror.png differ diff --git a/test/fixtures/expected/extend-2channel-repeat.png b/test/fixtures/expected/extend-2channel-repeat.png new file mode 100644 index 000000000..0c4b2b5f0 Binary files /dev/null and b/test/fixtures/expected/extend-2channel-repeat.png differ diff --git a/test/fixtures/expected/extend-2channel.png b/test/fixtures/expected/extend-2channel.png deleted file mode 100644 index bd57b5a93..000000000 Binary files a/test/fixtures/expected/extend-2channel.png and /dev/null differ diff --git a/test/fixtures/expected/extend-equal.jpg b/test/fixtures/expected/extend-equal-background.jpg similarity index 100% rename from test/fixtures/expected/extend-equal.jpg rename to test/fixtures/expected/extend-equal-background.jpg diff --git a/test/fixtures/expected/extend-equal-background.webp b/test/fixtures/expected/extend-equal-background.webp new file mode 100644 index 000000000..c7898aefa Binary files /dev/null and b/test/fixtures/expected/extend-equal-background.webp differ diff --git a/test/fixtures/expected/extend-equal-copy.jpg b/test/fixtures/expected/extend-equal-copy.jpg new file mode 100644 index 000000000..160395eb8 Binary files /dev/null and b/test/fixtures/expected/extend-equal-copy.jpg differ diff --git a/test/fixtures/expected/extend-equal-copy.webp b/test/fixtures/expected/extend-equal-copy.webp new file mode 100644 index 000000000..7123beee9 Binary files /dev/null and b/test/fixtures/expected/extend-equal-copy.webp differ diff --git a/test/fixtures/expected/extend-equal-mirror.jpg b/test/fixtures/expected/extend-equal-mirror.jpg new file mode 100644 index 000000000..9ff2a3cbf Binary files /dev/null and b/test/fixtures/expected/extend-equal-mirror.jpg differ diff --git a/test/fixtures/expected/extend-equal-mirror.webp b/test/fixtures/expected/extend-equal-mirror.webp new file mode 100644 index 000000000..9b3b35187 Binary files /dev/null and b/test/fixtures/expected/extend-equal-mirror.webp differ diff --git a/test/fixtures/expected/extend-equal-repeat.jpg b/test/fixtures/expected/extend-equal-repeat.jpg new file mode 100644 index 000000000..860787da0 Binary files /dev/null and b/test/fixtures/expected/extend-equal-repeat.jpg differ diff --git a/test/fixtures/expected/extend-equal-repeat.webp b/test/fixtures/expected/extend-equal-repeat.webp new file mode 100644 index 000000000..17320d84c Binary files /dev/null and b/test/fixtures/expected/extend-equal-repeat.webp differ diff --git a/test/fixtures/expected/extend-equal-single.webp b/test/fixtures/expected/extend-equal-single.webp new file mode 100644 index 000000000..95fab3970 Binary files /dev/null and b/test/fixtures/expected/extend-equal-single.webp differ diff --git a/test/fixtures/expected/extend-unequal.png b/test/fixtures/expected/extend-unequal-background.png similarity index 100% rename from test/fixtures/expected/extend-unequal.png rename to test/fixtures/expected/extend-unequal-background.png diff --git a/test/fixtures/expected/extend-unequal-copy.png b/test/fixtures/expected/extend-unequal-copy.png new file mode 100644 index 000000000..ab324ef47 Binary files /dev/null and b/test/fixtures/expected/extend-unequal-copy.png differ diff --git a/test/fixtures/expected/extend-unequal-mirror.png b/test/fixtures/expected/extend-unequal-mirror.png new file mode 100644 index 000000000..99e35bf95 Binary files /dev/null and b/test/fixtures/expected/extend-unequal-mirror.png differ diff --git a/test/fixtures/expected/extend-unequal-repeat.png b/test/fixtures/expected/extend-unequal-repeat.png new file mode 100644 index 000000000..6f0afd4b6 Binary files /dev/null and b/test/fixtures/expected/extend-unequal-repeat.png differ diff --git a/test/fixtures/expected/extract-alpha-16bit.jpg b/test/fixtures/expected/extract-alpha-16bit.jpg deleted file mode 100644 index 58c02669b..000000000 Binary files a/test/fixtures/expected/extract-alpha-16bit.jpg and /dev/null differ diff --git a/test/fixtures/expected/extract-alpha-16bit.png b/test/fixtures/expected/extract-alpha-16bit.png new file mode 100644 index 000000000..91537410f Binary files /dev/null and b/test/fixtures/expected/extract-alpha-16bit.png differ diff --git a/test/fixtures/expected/extract-lch.jpg b/test/fixtures/expected/extract-lch.jpg deleted file mode 100644 index e922b78a6..000000000 Binary files a/test/fixtures/expected/extract-lch.jpg and /dev/null differ diff --git a/test/fixtures/expected/extract-rotate-extract.jpg b/test/fixtures/expected/extract-rotate-extract.jpg new file mode 100644 index 000000000..a9f09c641 Binary files /dev/null and b/test/fixtures/expected/extract-rotate-extract.jpg differ diff --git a/test/fixtures/expected/extract.tiff b/test/fixtures/expected/extract.tiff index 74c2cac3f..e02db48b2 100644 Binary files a/test/fixtures/expected/extract.tiff and b/test/fixtures/expected/extract.tiff differ diff --git a/test/fixtures/expected/fast-shrink-on-load-false.png b/test/fixtures/expected/fast-shrink-on-load-false.png deleted file mode 100644 index a4c7241d5..000000000 Binary files a/test/fixtures/expected/fast-shrink-on-load-false.png and /dev/null differ diff --git a/test/fixtures/expected/fast-shrink-on-load-true.png b/test/fixtures/expected/fast-shrink-on-load-true.png deleted file mode 100644 index 5033da3a5..000000000 Binary files a/test/fixtures/expected/fast-shrink-on-load-true.png and /dev/null differ diff --git a/test/fixtures/expected/fast-shrink-on-load.png b/test/fixtures/expected/fast-shrink-on-load.png new file mode 100644 index 000000000..feeb8e8b8 Binary files /dev/null and b/test/fixtures/expected/fast-shrink-on-load.png differ diff --git a/test/fixtures/expected/flatten-orange.jpg b/test/fixtures/expected/flatten-orange.jpg index 18f49bb87..5f00a736d 100644 Binary files a/test/fixtures/expected/flatten-orange.jpg and b/test/fixtures/expected/flatten-orange.jpg differ diff --git a/test/fixtures/expected/flip-and-flop.jpg b/test/fixtures/expected/flip-and-flop.jpg index ce58eadfb..44f70f15a 100644 Binary files a/test/fixtures/expected/flip-and-flop.jpg and b/test/fixtures/expected/flip-and-flop.jpg differ diff --git a/test/fixtures/expected/gravity-center-height.webp b/test/fixtures/expected/gravity-center-height.webp new file mode 100644 index 000000000..70fbd1de0 Binary files /dev/null and b/test/fixtures/expected/gravity-center-height.webp differ diff --git a/test/fixtures/expected/gravity-center-width.webp b/test/fixtures/expected/gravity-center-width.webp new file mode 100644 index 000000000..964ebe61f Binary files /dev/null and b/test/fixtures/expected/gravity-center-width.webp differ diff --git a/test/fixtures/expected/hilutite.jpg b/test/fixtures/expected/hilutite.jpg index ed6d06a6f..d11facb2f 100644 Binary files a/test/fixtures/expected/hilutite.jpg and b/test/fixtures/expected/hilutite.jpg differ diff --git a/test/fixtures/expected/icc-cmyk.jpg b/test/fixtures/expected/icc-cmyk.jpg index 248070c66..4b7cf7d4f 100644 Binary files a/test/fixtures/expected/icc-cmyk.jpg and b/test/fixtures/expected/icc-cmyk.jpg differ diff --git a/test/fixtures/expected/join2x2.png b/test/fixtures/expected/join2x2.png new file mode 100644 index 000000000..533df1284 Binary files /dev/null and b/test/fixtures/expected/join2x2.png differ diff --git a/test/fixtures/expected/linear-16bit.png b/test/fixtures/expected/linear-16bit.png new file mode 100644 index 000000000..3e7bfc525 Binary files /dev/null and b/test/fixtures/expected/linear-16bit.png differ diff --git a/test/fixtures/expected/linear-per-channel.jpg b/test/fixtures/expected/linear-per-channel.jpg new file mode 100644 index 000000000..13cef2b8a Binary files /dev/null and b/test/fixtures/expected/linear-per-channel.jpg differ diff --git a/test/fixtures/expected/median_1.jpg b/test/fixtures/expected/median_1.jpg deleted file mode 100644 index df1535927..000000000 Binary files a/test/fixtures/expected/median_1.jpg and /dev/null differ diff --git a/test/fixtures/expected/median_3.jpg b/test/fixtures/expected/median_3.jpg deleted file mode 100644 index ccb091ab6..000000000 Binary files a/test/fixtures/expected/median_3.jpg and /dev/null differ diff --git a/test/fixtures/expected/median_5.jpg b/test/fixtures/expected/median_5.jpg deleted file mode 100644 index ba536e98b..000000000 Binary files a/test/fixtures/expected/median_5.jpg and /dev/null differ diff --git a/test/fixtures/expected/median_color.jpg b/test/fixtures/expected/median_color.jpg deleted file mode 100644 index f521f0af8..000000000 Binary files a/test/fixtures/expected/median_color.jpg and /dev/null differ diff --git a/test/fixtures/expected/modulate-hue-angle-120.png b/test/fixtures/expected/modulate-hue-angle-120.png index 59ef7f682..0a6d0e9d2 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-120.png and b/test/fixtures/expected/modulate-hue-angle-120.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-150.png b/test/fixtures/expected/modulate-hue-angle-150.png index 5ad29b03e..e776fc4a0 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-150.png and b/test/fixtures/expected/modulate-hue-angle-150.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-180.png b/test/fixtures/expected/modulate-hue-angle-180.png index 95944f6e1..6b3f2e708 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-180.png and b/test/fixtures/expected/modulate-hue-angle-180.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-210.png b/test/fixtures/expected/modulate-hue-angle-210.png index d85ab59c5..ca489f0ca 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-210.png and b/test/fixtures/expected/modulate-hue-angle-210.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-240.png b/test/fixtures/expected/modulate-hue-angle-240.png index 591af1a79..8bedbbfab 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-240.png and b/test/fixtures/expected/modulate-hue-angle-240.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-270.png b/test/fixtures/expected/modulate-hue-angle-270.png index c01996a55..f2bf4f9e9 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-270.png and b/test/fixtures/expected/modulate-hue-angle-270.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-30.png b/test/fixtures/expected/modulate-hue-angle-30.png index 4cafb5a67..b7dfce148 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-30.png and b/test/fixtures/expected/modulate-hue-angle-30.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-300.png b/test/fixtures/expected/modulate-hue-angle-300.png index 05ec97c8a..69238c3b0 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-300.png and b/test/fixtures/expected/modulate-hue-angle-300.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-330.png b/test/fixtures/expected/modulate-hue-angle-330.png index 377c173f7..c42501309 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-330.png and b/test/fixtures/expected/modulate-hue-angle-330.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-360.png b/test/fixtures/expected/modulate-hue-angle-360.png index bb937aa46..dd7474135 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-360.png and b/test/fixtures/expected/modulate-hue-angle-360.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-60.png b/test/fixtures/expected/modulate-hue-angle-60.png index fa2eded86..3305d7870 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-60.png and b/test/fixtures/expected/modulate-hue-angle-60.png differ diff --git a/test/fixtures/expected/modulate-hue-angle-90.png b/test/fixtures/expected/modulate-hue-angle-90.png index 396034174..077a92a09 100644 Binary files a/test/fixtures/expected/modulate-hue-angle-90.png and b/test/fixtures/expected/modulate-hue-angle-90.png differ diff --git a/test/fixtures/expected/negate-preserve-alpha-grey.png b/test/fixtures/expected/negate-preserve-alpha-grey.png new file mode 100644 index 000000000..7e4b85df6 Binary files /dev/null and b/test/fixtures/expected/negate-preserve-alpha-grey.png differ diff --git a/test/fixtures/expected/negate-preserve-alpha-trans.png b/test/fixtures/expected/negate-preserve-alpha-trans.png new file mode 100644 index 000000000..18e98a703 Binary files /dev/null and b/test/fixtures/expected/negate-preserve-alpha-trans.png differ diff --git a/test/fixtures/expected/negate-preserve-alpha-trans.webp b/test/fixtures/expected/negate-preserve-alpha-trans.webp new file mode 100644 index 000000000..eb8ac98a4 Binary files /dev/null and b/test/fixtures/expected/negate-preserve-alpha-trans.webp differ diff --git a/test/fixtures/expected/negate-preserve-alpha.png b/test/fixtures/expected/negate-preserve-alpha.png new file mode 100644 index 000000000..1e5122a21 Binary files /dev/null and b/test/fixtures/expected/negate-preserve-alpha.png differ diff --git a/test/fixtures/expected/negate-preserve-alpha.webp b/test/fixtures/expected/negate-preserve-alpha.webp new file mode 100644 index 000000000..287e38c86 Binary files /dev/null and b/test/fixtures/expected/negate-preserve-alpha.webp differ diff --git a/test/fixtures/expected/negate-trans.png b/test/fixtures/expected/negate-trans.png index 52e79e5f6..d590068cc 100644 Binary files a/test/fixtures/expected/negate-trans.png and b/test/fixtures/expected/negate-trans.png differ diff --git a/test/fixtures/expected/overlay-gravity-center.jpg b/test/fixtures/expected/overlay-gravity-center.jpg index 8b65ebcf7..709403e7d 100644 Binary files a/test/fixtures/expected/overlay-gravity-center.jpg and b/test/fixtures/expected/overlay-gravity-center.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-centre.jpg b/test/fixtures/expected/overlay-gravity-centre.jpg index 8b65ebcf7..709403e7d 100644 Binary files a/test/fixtures/expected/overlay-gravity-centre.jpg and b/test/fixtures/expected/overlay-gravity-centre.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-east.jpg b/test/fixtures/expected/overlay-gravity-east.jpg index 756a82c11..81cb9a7fa 100644 Binary files a/test/fixtures/expected/overlay-gravity-east.jpg and b/test/fixtures/expected/overlay-gravity-east.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-north.jpg b/test/fixtures/expected/overlay-gravity-north.jpg index b61fc8375..fa5a9639b 100644 Binary files a/test/fixtures/expected/overlay-gravity-north.jpg and b/test/fixtures/expected/overlay-gravity-north.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-northeast.jpg b/test/fixtures/expected/overlay-gravity-northeast.jpg index 9523792be..1be5a1944 100644 Binary files a/test/fixtures/expected/overlay-gravity-northeast.jpg and b/test/fixtures/expected/overlay-gravity-northeast.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-northwest.jpg b/test/fixtures/expected/overlay-gravity-northwest.jpg index 030e0b124..acf269989 100644 Binary files a/test/fixtures/expected/overlay-gravity-northwest.jpg and b/test/fixtures/expected/overlay-gravity-northwest.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-south.jpg b/test/fixtures/expected/overlay-gravity-south.jpg index 96cd451d8..bd8856413 100644 Binary files a/test/fixtures/expected/overlay-gravity-south.jpg and b/test/fixtures/expected/overlay-gravity-south.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-southeast.jpg b/test/fixtures/expected/overlay-gravity-southeast.jpg index 852292d86..d46da3a0d 100644 Binary files a/test/fixtures/expected/overlay-gravity-southeast.jpg and b/test/fixtures/expected/overlay-gravity-southeast.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-southwest.jpg b/test/fixtures/expected/overlay-gravity-southwest.jpg index 8876c9fdc..755c128a5 100644 Binary files a/test/fixtures/expected/overlay-gravity-southwest.jpg and b/test/fixtures/expected/overlay-gravity-southwest.jpg differ diff --git a/test/fixtures/expected/overlay-gravity-west.jpg b/test/fixtures/expected/overlay-gravity-west.jpg index 1495500cb..4c4223616 100644 Binary files a/test/fixtures/expected/overlay-gravity-west.jpg and b/test/fixtures/expected/overlay-gravity-west.jpg differ diff --git a/test/fixtures/expected/resize-crop-extract.jpg b/test/fixtures/expected/resize-crop-extract.jpg index 166e5e967..e596b9a0a 100644 Binary files a/test/fixtures/expected/resize-crop-extract.jpg and b/test/fixtures/expected/resize-crop-extract.jpg differ diff --git a/test/fixtures/expected/rotate-extract-45.jpg b/test/fixtures/expected/rotate-extract-45.jpg index e97349bdb..5600280fc 100644 Binary files a/test/fixtures/expected/rotate-extract-45.jpg and b/test/fixtures/expected/rotate-extract-45.jpg differ diff --git a/test/fixtures/expected/rotate-mirror-extract.jpg b/test/fixtures/expected/rotate-mirror-extract.jpg new file mode 100644 index 000000000..46d5a93b6 Binary files /dev/null and b/test/fixtures/expected/rotate-mirror-extract.jpg differ diff --git a/test/fixtures/expected/svg72.png b/test/fixtures/expected/svg72.png index a7833dcf2..01d0586a9 100644 Binary files a/test/fixtures/expected/svg72.png and b/test/fixtures/expected/svg72.png differ diff --git a/test/fixtures/expected/tint-alpha.png b/test/fixtures/expected/tint-alpha.png index 01620a961..1375af948 100644 Binary files a/test/fixtures/expected/tint-alpha.png and b/test/fixtures/expected/tint-alpha.png differ diff --git a/test/fixtures/expected/tint-blue.jpg b/test/fixtures/expected/tint-blue.jpg index 897864943..780ffef52 100644 Binary files a/test/fixtures/expected/tint-blue.jpg and b/test/fixtures/expected/tint-blue.jpg differ diff --git a/test/fixtures/expected/tint-cmyk.jpg b/test/fixtures/expected/tint-cmyk.jpg index 436bc6676..1bd076da5 100644 Binary files a/test/fixtures/expected/tint-cmyk.jpg and b/test/fixtures/expected/tint-cmyk.jpg differ diff --git a/test/fixtures/expected/tint-green.jpg b/test/fixtures/expected/tint-green.jpg index e60f90230..c7ef22bef 100644 Binary files a/test/fixtures/expected/tint-green.jpg and b/test/fixtures/expected/tint-green.jpg differ diff --git a/test/fixtures/expected/tint-red.jpg b/test/fixtures/expected/tint-red.jpg index ee72cbab6..b0fdc44c7 100644 Binary files a/test/fixtures/expected/tint-red.jpg and b/test/fixtures/expected/tint-red.jpg differ diff --git a/test/fixtures/expected/tint-sepia.jpg b/test/fixtures/expected/tint-sepia.jpg index b344a4822..57fae97bb 100644 Binary files a/test/fixtures/expected/tint-sepia.jpg and b/test/fixtures/expected/tint-sepia.jpg differ diff --git a/test/fixtures/expected/unflatten-flag-white-transparent.png b/test/fixtures/expected/unflatten-flag-white-transparent.png new file mode 100644 index 000000000..afed72132 Binary files /dev/null and b/test/fixtures/expected/unflatten-flag-white-transparent.png differ diff --git a/test/fixtures/expected/unflatten-swiss.png b/test/fixtures/expected/unflatten-swiss.png new file mode 100644 index 000000000..89c8a8674 Binary files /dev/null and b/test/fixtures/expected/unflatten-swiss.png differ diff --git a/test/fixtures/expected/unflatten-white-transparent.png b/test/fixtures/expected/unflatten-white-transparent.png new file mode 100644 index 000000000..abf3ed48b Binary files /dev/null and b/test/fixtures/expected/unflatten-white-transparent.png differ diff --git a/test/fixtures/expected/webp-alpha-80.webp b/test/fixtures/expected/webp-alpha-80.webp index 739d8c00e..cb4ef6ec4 100644 Binary files a/test/fixtures/expected/webp-alpha-80.webp and b/test/fixtures/expected/webp-alpha-80.webp differ diff --git a/test/fixtures/fogra-0-100-100-0.tif b/test/fixtures/fogra-0-100-100-0.tif new file mode 100644 index 000000000..d6c3a598a Binary files /dev/null and b/test/fixtures/fogra-0-100-100-0.tif differ diff --git a/test/fixtures/gradients-rgb8.png b/test/fixtures/gradients-rgb8.png new file mode 100644 index 000000000..693272bc0 Binary files /dev/null and b/test/fixtures/gradients-rgb8.png differ diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 6d3ff5c00..2448843e2 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -1,39 +1,37 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const path = require('path'); +const path = require('node:path'); const sharp = require('../../'); -const maxColourDistance = require('../../build/Release/sharp')._maxColourDistance; +const maxColourDistance = require('../../lib/sharp')._maxColourDistance; // Helpers -const getPath = function (filename) { - return path.join(__dirname, filename); -}; +const getPath = (filename) => path.join(__dirname, filename); // Generates a 64-bit-as-binary-string image fingerprint // Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html -const fingerprint = function (image, callback) { - sharp(image) +async function fingerprint (image) { + return sharp(image) .flatten('gray') .greyscale() .normalise() .resize(9, 8, { fit: sharp.fit.fill }) .raw() - .toBuffer(function (err, data) { - if (err) { - callback(err); - } else { - let fingerprint = ''; - for (let col = 0; col < 8; col++) { - for (let row = 0; row < 8; row++) { - const left = data[(row * 8) + col]; - const right = data[(row * 8) + col + 1]; - fingerprint = fingerprint + (left < right ? '1' : '0'); - } + .toBuffer() + .then((data) => { + let fingerprint = ''; + for (let col = 0; col < 8; col++) { + for (let row = 0; row < 8; row++) { + const left = data[(row * 8) + col]; + const right = data[(row * 8) + col + 1]; + fingerprint = fingerprint + (left < right ? '1' : '0'); } - callback(null, fingerprint); } + return fingerprint; }); -}; +} module.exports = { @@ -74,11 +72,14 @@ module.exports = { inputJpgLossless: getPath('testimgl.jpg'), // Lossless JPEG from ftp://ftp.fu-berlin.de/unix/X11/graphics/ImageMagick/delegates/ljpeg-6b.tar.gz inputPng: getPath('50020484-00001.png'), // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png + inputPngGradients: getPath('gradients-rgb8.png'), inputPngWithTransparency: getPath('blackbug.png'), // public domain inputPngCompleteTransparency: getPath('full-transparent.png'), inputPngWithGreyAlpha: getPath('grey-8bit-alpha.png'), inputPngWithOneColor: getPath('2x2_fdcce6.png'), inputPngWithTransparency16bit: getPath('tbgn2c16.png'), // http://www.schaik.com/pngsuite/tbgn2c16.png + inputPng8BitGreyBackground: getPath('bgbn4a08.png'), // http://www.schaik.com/pngsuite/bgbn4a08.png + inputPng16BitGreyBackground: getPath('bggn4a16.png'), // http://www.schaik.com/pngsuite/bggn4a16.png inputPng16BitGreyAlpha: getPath('16-bit-grey-alpha.png'), // CC-BY-NC-SA florc http://www.colourlovers.com/pattern/50713/pat inputPngOverlayLayer0: getPath('alpha-layer-0-background.png'), inputPngOverlayLayer1: getPath('alpha-layer-1-fill.png'), @@ -90,6 +91,15 @@ module.exports = { inputPngEmbed: getPath('embedgravitybird.png'), // Released to sharp under a CC BY 4.0 inputPngRGBWithAlpha: getPath('2569067123_aca715a2ee_o.png'), // http://www.flickr.com/photos/grizdave/2569067123/ (same as inputJpg) inputPngImageInAlpha: getPath('image-in-alpha.png'), // https://github.com/lovell/sharp/issues/1597 + inputPngSolidAlpha: getPath('with-alpha.png'), // https://github.com/lovell/sharp/issues/1599 + inputPngP3: getPath('p3.png'), // https://github.com/lovell/sharp/issues/2862 + inputPngPalette: getPath('swiss.png'), // https://github.com/randy408/libspng/issues/188 + inputPngTrimIncludeAlpha: getPath('trim-mc.png'), // https://github.com/lovell/sharp/issues/2166 + inputPngTrimSpecificColour: getPath('Flag_of_the_Netherlands.png'), // https://commons.wikimedia.org/wiki/File:Flag_of_the_Netherlands.svg + inputPngTrimSpecificColour16bit: getPath('Flag_of_the_Netherlands-16bit.png'), // convert Flag_of_the_Netherlands.png -depth 16 Flag_of_the_Netherlands-16bit.png + inputPngTrimSpecificColourIncludeAlpha: getPath('Flag_of_the_Netherlands-alpha.png'), // convert Flag_of_the_Netherlands.png -alpha set -background none -channel A -evaluate multiply 0.5 +channel Flag_of_the_Netherlands-alpha.png + inputPngUint32Limit: getPath('65536-uint32-limit.png'), // https://alexandre.alapetite.fr/doc-alex/large-image/ + inputPngWithProPhotoProfile: getPath('prophoto.png'), inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp @@ -102,6 +112,11 @@ module.exports = { inputTiffUncompressed: getPath('uncompressed_tiff.tiff'), // https://code.google.com/archive/p/imagetestsuite/wikis/TIFFTestSuite.wiki file: 0c84d07e1b22b76f24cccc70d8788e4a.tif inputTiff8BitDepth: getPath('8bit_depth.tiff'), inputTifftagPhotoshop: getPath('tifftag-photoshop.tiff'), // https://github.com/lovell/sharp/issues/1600 + inputTiffFogra: getPath('fogra-0-100-100-0.tif'), // https://github.com/lovell/sharp/issues/4045 + inputTiffGeo: getPath('bonne.geo.tif'), // https://download.osgeo.org/geotiff/samples/intergraph + + inputJp2: getPath('relax.jp2'), // https://www.fnordware.com/j2k/relax.jp2 + inputJp2TileParts: getPath('relax_tileparts.jp2'), // kdu_expand -i relax.jp2 -o relax-tmp.tif ; kdu_compress -i relax-tmp.tif -o relax_tileparts.jp2 -jp2_space sRGB Clayers=8 -rate 1.0,0.04 Stiles='{128,128}' ORGtparts=L ; rm relax-tmp.tif inputGif: getPath('Crash_test.gif'), // http://upload.wikimedia.org/wikipedia/commons/e/e3/Crash_test.gif inputGifGreyPlusAlpha: getPath('grey-plus-alpha.gif'), // http://i.imgur.com/gZ5jlmE.gif inputGifAnimated: getPath('rotating-squares.gif'), // CC0 https://loading.io/spinner/blocks/-rotating-squares-preloader-gif @@ -113,6 +128,8 @@ module.exports = { inputJPGBig: getPath('flowers.jpeg'), + inputPngDotAndLines: getPath('dot-and-lines.png'), + inputPngStripesV: getPath('stripesV.png'), inputPngStripesH: getPath('stripesH.png'), @@ -120,67 +137,66 @@ module.exports = { inputV: getPath('vfile.v'), + inputJpgClahe: getPath('concert.jpg'), // public domain - https://www.flickr.com/photos/mars_/14389236779/ + testPattern: getPath('test-pattern.png'), + inputPngWithTransparent: getPath('d.png'), // Path for tests requiring human inspection path: getPath, // Path for expected output images - expected: function (filename) { - return getPath(path.join('expected', filename)); - }, + expected: (filename) => getPath(path.join('expected', filename)), // Verify similarity of expected vs actual images via fingerprint // Specify distance threshold using `options={threshold: 42}`, default // `threshold` is 5; - assertSimilar: function (expectedImage, actualImage, options, callback) { + assertSimilar: async (expectedImage, actualImage, options, callback) => { if (typeof options === 'function') { callback = options; options = {}; } - - if (typeof options === 'undefined' && options === null) { + if (typeof options === 'undefined' || options === null) { options = {}; } - if (options.threshold === null || typeof options.threshold === 'undefined') { options.threshold = 5; // ~7% threshold } - if (typeof options.threshold !== 'number') { throw new TypeError('`options.threshold` must be a number'); } - if (typeof callback !== 'function') { - throw new TypeError('`callback` must be a function'); - } - - fingerprint(expectedImage, function (err, expectedFingerprint) { - if (err) return callback(err); - fingerprint(actualImage, function (err, actualFingerprint) { - if (err) return callback(err); - let distance = 0; - for (let i = 0; i < 64; i++) { - if (expectedFingerprint[i] !== actualFingerprint[i]) { - distance++; - } - } - - if (distance > options.threshold) { - return callback(new Error('Expected maximum similarity distance: ' + options.threshold + '. Actual: ' + distance + '.')); + try { + const [expectedFingerprint, actualFingerprint] = await Promise.all([ + fingerprint(expectedImage), + fingerprint(actualImage) + ]); + let distance = 0; + for (let i = 0; i < 64; i++) { + if (expectedFingerprint[i] !== actualFingerprint[i]) { + distance++; } - - callback(); - }); - }); + } + if (distance > options.threshold) { + throw new Error(`Expected maximum similarity distance: ${options.threshold}. Actual: ${distance}.`); + } + } catch (err) { + if (callback) { + return callback(err); + } + throw err; + } + if (callback) { + callback(); + } }, - assertMaxColourDistance: function (actualImagePath, expectedImagePath, acceptedDistance) { + assertMaxColourDistance: (actualImagePath, expectedImagePath, acceptedDistance) => { if (typeof actualImagePath !== 'string') { - throw new TypeError('`actualImagePath` must be a string; got ' + actualImagePath); + throw new TypeError(`\`actualImagePath\` must be a string; got ${actualImagePath}`); } if (typeof expectedImagePath !== 'string') { - throw new TypeError('`expectedImagePath` must be a string; got ' + expectedImagePath); + throw new TypeError(`\`expectedImagePath\` must be a string; got ${expectedImagePath}`); } if (typeof acceptedDistance !== 'number') { // Default threshold @@ -188,7 +204,7 @@ module.exports = { } const distance = maxColourDistance(actualImagePath, expectedImagePath); if (distance > acceptedDistance) { - throw new Error('Expected maximum absolute distance of ' + acceptedDistance + ', actual ' + distance); + throw new Error(`Expected maximum absolute distance of ${acceptedDistance}, actual ${distance}`); } } diff --git a/test/fixtures/invalid-illuminant.icc b/test/fixtures/invalid-illuminant.icc new file mode 100644 index 000000000..8ad73be25 Binary files /dev/null and b/test/fixtures/invalid-illuminant.icc differ diff --git a/test/fixtures/p3.png b/test/fixtures/p3.png new file mode 100644 index 000000000..96e10ff15 Binary files /dev/null and b/test/fixtures/p3.png differ diff --git a/test/fixtures/prophoto.png b/test/fixtures/prophoto.png new file mode 100644 index 000000000..e01fcb02a Binary files /dev/null and b/test/fixtures/prophoto.png differ diff --git a/test/fixtures/relax.jp2 b/test/fixtures/relax.jp2 new file mode 100644 index 000000000..1823990f4 Binary files /dev/null and b/test/fixtures/relax.jp2 differ diff --git a/test/fixtures/relax_tileparts.jp2 b/test/fixtures/relax_tileparts.jp2 new file mode 100644 index 000000000..62621c689 Binary files /dev/null and b/test/fixtures/relax_tileparts.jp2 differ diff --git a/test/fixtures/swiss.png b/test/fixtures/swiss.png new file mode 100644 index 000000000..cf310ec60 Binary files /dev/null and b/test/fixtures/swiss.png differ diff --git a/test/fixtures/trim-mc.png b/test/fixtures/trim-mc.png new file mode 100644 index 000000000..f170a290f Binary files /dev/null and b/test/fixtures/trim-mc.png differ diff --git a/test/fixtures/with-alpha.png b/test/fixtures/with-alpha.png new file mode 100644 index 000000000..723dd5850 Binary files /dev/null and b/test/fixtures/with-alpha.png differ diff --git a/test/leak/leak.sh b/test/leak/leak.sh index d417ed599..c1e5a0a33 100755 --- a/test/leak/leak.sh +++ b/test/leak/leak.sh @@ -1,4 +1,5 @@ -#!/bin/sh +#!/usr/bin/env bash +set -e if ! type valgrind >/dev/null; then echo "Please install valgrind before running memory leak tests" @@ -7,14 +8,15 @@ fi curl -s -o ./test/leak/libvips.supp https://raw.githubusercontent.com/libvips/libvips/master/suppressions/valgrind.supp -for test in ./test/unit/*.js; do - G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind \ +TESTS=$(ls test/unit --ignore=svg.js --ignore=text.js) +for test in $TESTS; do + G_SLICE=always-malloc G_DEBUG=gc-friendly VIPS_LEAK=1 VIPS_NOVECTOR=1 valgrind \ --suppressions=test/leak/libvips.supp \ --suppressions=test/leak/sharp.supp \ --gen-suppressions=yes \ --leak-check=full \ - --show-leak-kinds=definite,indirect,possible \ + --show-leak-kinds=definite,indirect \ --num-callers=20 \ --trace-children=yes \ - node --expose-gc node_modules/.bin/mocha --slow=60000 --timeout=120000 --file test/unit/beforeEach.js "$test"; + node --zero-fill-buffers --test "test/unit/$test"; done diff --git a/test/leak/sharp.supp b/test/leak/sharp.supp index e369f906e..c61e5c7d2 100644 --- a/test/leak/sharp.supp +++ b/test/leak/sharp.supp @@ -173,41 +173,6 @@ fun:TIFFWriteEncodedTile } -# gsf -{ - param_gsf_output_write - Memcheck:Param - write(buf) - fun:write - ... - fun:gsf_output_write -} -{ - value_gsf_output_write_crc32_little - Memcheck:Value8 - fun:crc32_little - ... - 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 { leak_fontconfig_FcConfigSubstituteWithPat @@ -225,14 +190,18 @@ fun:FcInitLoadConfigAndFonts } { - leak_fontconfig_doContent + leak_fontconfig_XML_ParseBuffer Memcheck:Leak match-leak-kinds: definite - fun:malloc ... - fun:doContent - fun:doProlog - fun:prologInitProcessor + fun:XML_ParseBuffer + obj:*/libfontconfig.so.* +} +{ + leak_fontconfig_XML_ParseBuffer_indirect + Memcheck:Leak + match-leak-kinds: indirect + ... fun:XML_ParseBuffer obj:*/libfontconfig.so.* } @@ -246,6 +215,36 @@ ... fun:FcInitLoadConfigAndFonts } +{ + leak_fontconfig_FcDefaultSubstitute + Memcheck:Leak + match-leak-kinds: indirect + fun:calloc + ... + fun:FcDefaultSubstitute + ... + fun:pango_itemize_with_base_dir + ... + fun:pango_layout_get_pixel_extents + fun:vips_text_get_extents +} +{ + leak_fontconfig_FcLangSetCreate + Memcheck:Leak + match-leak-kinds: indirect + fun:malloc + fun:FcLangSetCreate + fun:FcLangSetCopy + fun:FcValueSave + ... + fun:FcFontRenderPrepare + fun:FcFontMatch + ... + fun:pango_itemize_with_base_dir + ... + fun:pango_layout_get_pixel_extents + fun:vips_text_get_extents +} # heif { @@ -315,6 +314,41 @@ fun:heif_context_read_from_reader } +# glib +{ + leak_glib__tls_get_addr + Memcheck:Leak + match-leak-kinds: possible + ... + fun:malloc + fun:allocate_dtv_entry + fun:allocate_and_init + fun:tls_get_addr_tail + fun:__tls_get_addr +} +{ + value_g_utf8_make_valid_strlen + Memcheck:Value8 + fun:strlen + fun:g_utf8_make_valid +} +{ + value_g_utf8_make_valid_strncpy + Memcheck:Value8 + fun:strncpy + fun:g_strndup + ... + fun:g_utf8_make_valid +} +{ + cond_g_utf8_make_valid_strncpy + Memcheck:Cond + fun:strncpy + fun:g_strndup + ... + fun:g_utf8_make_valid +} + # libvips { cond_libvips_interpolate_lbb @@ -444,6 +478,22 @@ ... fun:vips__init } +{ + leak_libvips_thread_pool_new + Memcheck:Leak + match-leak-kinds: possible + fun:calloc + ... + fun:g_system_thread_new +} +{ + leak_libvips_thread_pool_push + Memcheck:Leak + match-leak-kinds: possible + fun:calloc + ... + fun:g_thread_pool_push +} { leak_rsvg_static_data Memcheck:Leak @@ -480,6 +530,14 @@ ... fun:rsvg_handle_new_from_gfile_sync } +{ + leak_rsvg_rust_280_bytes_static_regex + Memcheck:Leak + match-leak-kinds: possible + fun:malloc + ... + fun:rsvg_handle_get_dimensions_sub +} # libuv warnings { @@ -622,6 +680,13 @@ ... fun:_ZN4node9inspector5Agent5StartEPNS_12NodePlatformEPKcRKNS_12DebugOptionsE } +{ + leak_nodejs_node9inspector5Agent5StartERKSsRKNS + Memcheck:Leak + match-leak-kinds: possible + ... + fun:_ZN4node9inspector5Agent5StartERKSsRKNS_12DebugOptionsESt10shared_ptrINS_15ExclusiveAccessINS_8HostPortENS_9MutexBaseINS_16LibuvMutexTraitsEEEEEEb +} { leak_nodejs_node12NodePlatform_TracingController Memcheck:Leak @@ -871,3 +936,113 @@ ... fun:_ZN2v88internal18ArrayBufferSweeper10ReleaseAllEv } +{ + cond_v8_ZN2v88internal8compiler12PipelineImpl13OptimizeGraphEPNS1_7LinkageE + Memcheck:Cond + ... + fun:_ZN2v88internal8compiler12PipelineImpl13OptimizeGraphEPNS1_7LinkageE +} +{ + cond_v8_ZN2v88internal4Heap20HasLowAllocationRateEv + Memcheck:Cond + ... + fun:_ZN2v88internal4Heap20HasLowAllocationRateEv +} +{ + cond_v8_ZN2v88internal4Heap15RecomputeLimitsENS0_16GarbageCollectorENS_4base9TimeTicksE + Memcheck:Cond + ... + fun:_ZN2v88internal4Heap15RecomputeLimitsENS0_16GarbageCollectorENS_4base9TimeTicksE +} +{ + cond_node_Builtins_JSEntry + Memcheck:Cond + ... + fun:Builtins_JSEntry + ... + fun:uv__poll_io_uring +} +{ + cond_node_Builtins_TestEqualStrictHandler + Memcheck:Cond + fun:Builtins_TestEqualStrictHandler + ... + fun:uv__poll_io_uring +} +{ + cond_node_Builtins_TestGreaterThanHandler + Memcheck:Cond + fun:Builtins_TestGreaterThanHandler + ... + fun:uv__poll_io_uring +} +{ + cond_node_AfterStat + Memcheck:Cond + ... + fun:_ZN4node2fs9AfterStatEP7uv_fs_s + ... + fun:uv__poll_io_uring +} +{ + cond_node_AfterMkdirp + Memcheck:Cond + fun:_ZN4node2fs11AfterMkdirpEP7uv_fs_s + fun:_ZN4node24MakeLibuvRequestCallbackI7uv_fs_sPFvPS1_EE7WrapperES2_ + fun:_ZZZN4node2fs11MKDirpAsyncEP9uv_loop_sP7uv_fs_sPKciPFvS4_EENKUlS4_E_clES4_ENUlS4_E_4_FUNES4_ + fun:uv__poll_io_uring +} +{ + cond_v8_ArrayBufferSweeper_Finalize + Memcheck:Cond + fun:_ZN2v88internal18ArrayBufferSweeper8FinalizeEv +} +{ + cond_v8_AdjustAmountOfExternalAllocatedMemory + Memcheck:Cond + fun:_ZN2v87Isolate37AdjustAmountOfExternalAllocatedMemoryEl +} +{ + cond_v8_IncrementalMarkingLimitReached + Memcheck:Cond + fun:_ZN2v88internal4Heap30IncrementalMarkingLimitReachedEv +} +{ + cond_v8_ShouldExpandOldGenerationOnSlowAllocation + Memcheck:Cond + fun:_ZN2v88internal4Heap41ShouldExpandOldGenerationOnSlowAllocationEPNS0_9LocalHeapENS0_16AllocationOriginE +} +{ + cond_v8_ArrayBufferSweeper_SweepingJob_SweepListFull + Memcheck:Cond + fun:_ZN2v88internal18ArrayBufferSweeper11SweepingJob13SweepListFullEPNS0_15ArrayBufferListE +} +{ + cond_v8_ArrayBufferSweeper_SweepingJob_SweepYoung + Memcheck:Cond + fun:_ZN2v88internal18ArrayBufferSweeper11SweepingJob10SweepYoungEv +} +{ + cond_v8_StartIncrementalMarkingIfAllocationLimitIsReachedBackground + Memcheck:Cond + fun:_ZN2v88internal4Heap59StartIncrementalMarkingIfAllocationLimitIsReachedBackgroundEv +} +{ + addr_v8_ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0 + Memcheck:Addr8 + fun:strncmp + ... + fun:_ZZN4node7binding6DLOpenERKN2v820FunctionCallbackInfoINS1_5ValueEEEENKUlPNS0_4DLibEE_clES8_ + fun:_ZN4node7binding6DLOpenERKN2v820FunctionCallbackInfoINS1_5ValueEEE + fun:_ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEESA_NS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EENS0_16BuiltinArgumentsE +} +{ + addr_node_binding_dlopen_strncmp + Memcheck:Addr8 + fun:strncmp + fun:is_dst + ... + fun:dlopen_implementation + ... + fun:_ZNSt17_Function_handlerIFbPN4node7binding4DLibEEZNS1_6DLOpenERKN2v820FunctionCallbackInfoINS5_5ValueEEEEUlS3_E_E9_M_invokeERKSt9_Any_dataOS3_ +} diff --git a/test/saliency/README.md b/test/saliency/README.md deleted file mode 100644 index 580d7f244..000000000 --- a/test/saliency/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# Crop strategy accuracy - -1. Download the [MSRA Salient Object Database](http://research.microsoft.com/en-us/um/people/jiansun/SalientObject/salient_object.htm) (101MB). -2. Extract each image and its median human-labelled salient region. -3. Generate a test report of percentage deviance of top and left edges for each crop strategy, plus a naive centre gravity crop as "control". - -```sh -git clone https://github.com/lovell/sharp.git -cd sharp/test/saliency -./download.sh -node report.js -python -m SimpleHTTPServer -``` - -The test report will then be available at -http://localhost:8000/report.html diff --git a/test/saliency/download.sh b/test/saliency/download.sh deleted file mode 100755 index 747844725..000000000 --- a/test/saliency/download.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -# Fetch and parse the MSRA Salient Object Database 'Image set B' -# http://research.microsoft.com/en-us/um/people/jiansun/salientobject/salient_object.htm - -if [ ! -d Image ]; then - if [ ! -f ImageB.zip ]; then - echo "Downloading 5000 images (101MB)" - curl -O http://research.microsoft.com/en-us/um/people/jiansun/salientobject/ImageSetB/ImageB.zip - fi - unzip ImageB.zip -fi - -if [ ! -d UserData ]; then - if [ ! -f UserDataB.zip ]; then - echo "Downloading human-labelled regions" - curl -O http://research.microsoft.com/en-us/um/people/jiansun/salientobject/ImageSetB/UserDataB.zip - fi - unzip UserDataB.zip -fi - -if [ ! -f userData.json ]; then - echo "Processing human-labelled regions" - node userData.js -fi diff --git a/test/saliency/humanae/download.js b/test/saliency/humanae/download.js deleted file mode 100644 index 7a43fd421..000000000 --- a/test/saliency/humanae/download.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const request = require('request'); -const tumblr = require('tumblr.js'); - -const client = tumblr.createClient({ - consumer_key: '***', - consumer_secret: '***' -}); - -const fetchImages = function (offset) { - console.log(`Fetching offset ${offset}`); - client.posts('humanae', { - type: 'photo', - offset: offset - }, function (err, response) { - if (err) throw err; - if (response.posts.length > 0) { - response.posts.forEach((post) => { - const url = post.photos[0].alt_sizes - .filter((image) => image.width === 100) - .map((image) => image.url)[0]; - const filename = `./images/${post.id}.jpg`; - try { - fs.statSync(filename); - } catch (err) { - if (err.code === 'ENOENT') { - request(url).pipe(fs.createWriteStream(filename)); - } - } - }); - fetchImages(offset + 20); - } - }); -}; -fetchImages(0); diff --git a/test/saliency/humanae/package.json b/test/saliency/humanae/package.json deleted file mode 100644 index f436f5a28..000000000 --- a/test/saliency/humanae/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "sharp-crop-strategy-attention-model-humanae", - "version": "0.0.1", - "private": true, - "dependencies": { - "request": "^2.75.0", - "tumblr.js": "^1.1.1" - } -} diff --git a/test/saliency/humanae/tone.js b/test/saliency/humanae/tone.js deleted file mode 100644 index 65f37d1ab..000000000 --- a/test/saliency/humanae/tone.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const childProcess = require('child_process'); - -const a = []; -const b = []; - -fs.readdirSync('./images') - .filter((file) => file.endsWith('.jpg')) - .forEach((file) => { - // Extract one pixel, avoiding first DCT block, and return value of A and B channels - const command = `convert ./images/${file}[1x1+8+8] -colorspace lab -format "%[fx:u.g] %[fx:u.b]" info:`; - const result = childProcess.execSync(command, { encoding: 'utf8' }); - const ab = result.split(' '); - a.push(ab[0]); - b.push(ab[1]); - }); - -a.sort((v1, v2) => v1 - v2); -b.sort((v1, v2) => v1 - v2); - -// Convert from 0..1 to -128..128 -const convert = function (v) { - return Math.round(256 * (v - 0.5)); -}; - -const threshold = Math.round(a.length / 100); -console.log(`Trimming lowest/highest ${threshold} for 98th percentile`); - -// Ignore ~2% outliers -console.log(`a ${convert(a[threshold])} - ${convert(a[a.length - threshold])}`); -console.log(`b ${convert(b[threshold])} - ${convert(b[b.length - threshold])}`); diff --git a/test/saliency/report.html b/test/saliency/report.html deleted file mode 100644 index 792d27374..000000000 --- a/test/saliency/report.html +++ /dev/null @@ -1,25 +0,0 @@ -<html> - <head> - <link href="https://cdnjs.cloudflare.com/ajax/libs/metrics-graphics/2.10.1/metricsgraphics.min.css" rel="stylesheet" type="text/css"> - <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.6/d3.min.js"></script> - <script src="https://cdnjs.cloudflare.com/ajax/libs/metrics-graphics/2.10.1/metricsgraphics.min.js"></script> - </head> - <body> - <div id="accuracy"></div> - <script> - d3.json('report.json', function(err, data) { - MG.data_graphic({ - title: 'Crop accuracy', - data: data, - target: '#accuracy', - width: 960, - height: 600, - x_accessor: 'accuracy', - x_label: '% Accuracy', - y_accessor: ['centre', 'entropy', 'attention'], - legend: ['Centre', 'Entropy', 'Attention'] - }); - }); - </script> - </body> -</html> diff --git a/test/saliency/report.js b/test/saliency/report.js deleted file mode 100644 index c1daf4f7c..000000000 --- a/test/saliency/report.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict'; - -const os = require('os'); -const fs = require('fs'); -const path = require('path'); -const async = require('async'); -const sharp = require('../../'); - -const crops = { - entropy: sharp.strategy.entropy, - attention: sharp.strategy.attention -}; -const concurrency = os.cpus().length; - -const scores = {}; - -const incrementScore = function (accuracy, crop) { - if (typeof scores[accuracy] === 'undefined') { - scores[accuracy] = {}; - } - if (typeof scores[accuracy][crop] === 'undefined') { - scores[accuracy][crop] = 0; - } - scores[accuracy][crop]++; -}; - -const userData = require('./userData.json'); -const files = Object.keys(userData); - -async.eachLimit(files, concurrency, function (file, done) { - const filename = path.join(__dirname, 'Image', file); - const salientWidth = userData[file].right - userData[file].left; - const salientHeight = userData[file].bottom - userData[file].top; - sharp(filename).metadata(function (err, metadata) { - if (err) console.log(err); - const marginWidth = metadata.width - salientWidth; - const marginHeight = metadata.height - salientHeight; - async.each(Object.keys(crops), function (crop, done) { - async.parallel([ - // Left edge accuracy - function (done) { - if (marginWidth) { - sharp(filename).resize(salientWidth, metadata.height).crop(crops[crop]).toBuffer(function (err, data, info) { - const delta = Math.abs(userData[file].left + info.cropOffsetLeft); - const accuracy = Math.round(marginWidth / (marginWidth + delta) * 100); - incrementScore(accuracy, crop); - done(err); - }); - } else { - done(); - } - }, - // Top edge accuracy - function (done) { - if (marginHeight) { - sharp(filename).resize(metadata.width, salientHeight).crop(crops[crop]).toBuffer(function (err, data, info) { - const delta = Math.abs(userData[file].top + info.cropOffsetTop); - const accuracy = Math.round(marginHeight / (marginHeight + delta) * 100); - incrementScore(accuracy, crop); - done(err); - }); - } else { - done(); - } - } - ], done); - }, done); - }); -}, function () { - const report = []; - Object.keys(scores).forEach(function (accuracy) { - report.push( - Object.assign({ - accuracy: Number(accuracy) - }, scores[accuracy]) - ); - }); - fs.writeFileSync('report.json', JSON.stringify(report, null, 2)); -}); diff --git a/test/saliency/userData.js b/test/saliency/userData.js deleted file mode 100644 index 924f6062b..000000000 --- a/test/saliency/userData.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -const userDataDir = 'UserData'; - -const images = {}; - -const median = function (values) { - values.sort(function (a, b) { - return a - b; - }); - const half = Math.floor(values.length / 2); - if (values.length % 2) { - return values[half]; - } else { - return Math.floor((values[half - 1] + values[half]) / 2); - } -}; - -// List of files -fs.readdirSync(userDataDir).forEach(function (file) { - // Contents of file - const lines = fs.readFileSync(path.join(userDataDir, file), { encoding: 'utf-8' }).split(/\r\n/); - // First line = number of entries - const entries = parseInt(lines[0], 10); - // Verify number of entries - if (entries !== 500) { - throw new Error('Expecting 500 images in ' + file + ', found ' + entries); - } - // Keep track of which line we're on - let linePos = 2; - for (let i = 0; i < entries; i++) { - // Get data for current image - const filename = lines[linePos].replace(/\\/, path.sep); - linePos = linePos + 2; - const regions = lines[linePos].split('; '); - linePos = linePos + 2; - // Parse human-labelled regions for min/max coords - const lefts = []; - const tops = []; - const rights = []; - const bottoms = []; - regions.forEach(function (region) { - if (region.indexOf(' ') !== -1) { - const coords = region.split(' '); - lefts.push(parseInt(coords[0], 10)); - tops.push(parseInt(coords[1], 10)); - rights.push(parseInt(coords[2], 10)); - bottoms.push(parseInt(coords[3], 10)); - } - }); - // Add image - images[filename] = { - left: median(lefts), - top: median(tops), - right: median(rights), - bottom: median(bottoms) - }; - } -}); - -// Verify number of images found -const imageCount = Object.keys(images).length; -if (imageCount === 5000) { - // Write output - fs.writeFileSync('userData.json', JSON.stringify(images, null, 2)); -} else { - throw new Error('Expecting 5000 images, found ' + imageCount); -} diff --git a/test/types/sharp.test-d.ts b/test/types/sharp.test-d.ts new file mode 100644 index 000000000..9364c08c8 --- /dev/null +++ b/test/types/sharp.test-d.ts @@ -0,0 +1,773 @@ +// biome-ignore-all lint/correctness/noUnusedFunctionParameters: types only test file +// biome-ignore-all lint/correctness/noUnusedVariables: types only test file + +import sharp = require('../../'); + +import { createReadStream, createWriteStream } from 'node:fs'; + +const input: Buffer = Buffer.alloc(0); +const readableStream: NodeJS.ReadableStream = createReadStream(input); +const writableStream: NodeJS.WritableStream = createWriteStream(input); + +sharp(input) + .extractChannel('green') + .toFile('input_green.jpg', (err, info) => { + // info.channels === 1 + // input_green.jpg contains the green channel of the input image + }); + +sharp('3-channel-rgb-input.png') + .bandbool(sharp.bool.and) + .toFile('1-channel-output.png', (err, info) => { + // The output will be a single channel image where each pixel `P = R & G & B`. + // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]` + // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`. + }); + +sharp('input.png') + .rotate(180) + .resize(300) + .flatten({ background: '#ff6600' }) + .composite([{ input: 'overlay.png', gravity: sharp.gravity.southeast, animated: false, failOn: 'warning' }]) + .sharpen() + .withMetadata() + .withMetadata({ + density: 96, + orientation: 8, + icc: 'some/path', + exif: { IFD0: { Copyright: 'Wernham Hogg' } }, + }) + .webp({ + quality: 90, + }) + .toBuffer() + .then((outputBuffer: Buffer) => { + // outputBuffer contains upside down, 300px wide, alpha channel flattened + // onto orange background, composited with overlay.png with SE gravity, + // sharpened, with metadata, 90% quality WebP image data. Phew! + }); + +sharp('input.png') + .keepMetadata() + .toFile('output.png', (err, info) => { + // output.png is an image containing input.png along with all metadata(EXIF, ICC, XMP, IPTC) from input.png + }) + +sharp('input.jpg') + .resize(300, 200) + .toFile('output.jpg', (err: Error) => { + // output.jpg is a 300 pixels wide and 200 pixels high image + // containing a scaled and cropped version of input.jpg + }); + +sharp('input.jpg').resize({ width: 300 }).blur(false).blur(true).toFile('output.jpg'); + +sharp().blur(); +sharp().blur(1); +sharp().blur({ sigma: 1 }); +sharp().blur({ sigma: 1, precision: 'approximate' }); +sharp().blur({ sigma: 1, minAmplitude: 0.8 }); + +sharp({ + create: { + width: 300, + height: 200, + channels: 4, + background: { r: 255, g: 0, b: 0, alpha: 128 }, + }, +}) + .png() + .toBuffer(); + +let transformer = sharp() + .resize(300) + .on('info', (info: sharp.OutputInfo) => { + console.log(`Image height is ${info.height}`); + }); +readableStream.pipe(transformer).pipe(writableStream); + +console.log(sharp.format); +console.log(sharp.versions); + +sharp.queue.on('change', (queueLength: number) => { + console.log(`Queue contains ${queueLength} task(s)`); +}); + +let pipeline: sharp.Sharp = sharp().rotate(); +pipeline.clone().resize(800, 600).pipe(writableStream); +pipeline.clone().extract({ left: 20, top: 20, width: 100, height: 100 }).pipe(writableStream); +readableStream.pipe(pipeline); +// firstWritableStream receives auto-rotated, resized readableStream +// secondWritableStream receives auto-rotated, extracted region of readableStream + +const image: sharp.Sharp = sharp(input); +image + .metadata() + .then<Buffer | undefined>((metadata: sharp.Metadata) => { + if (metadata.width) { + return image + .resize(Math.round(metadata.width / 2)) + .webp() + .toBuffer(); + } + }) + .then((data) => { + // data contains a WebP image half the width and height of the original JPEG + }); + +pipeline = sharp() + .rotate() + .resize(undefined, 200) + .toBuffer((err: Error, outputBuffer: Buffer, info: sharp.OutputInfo) => { + // outputBuffer contains 200px high JPEG image data, + // auto-rotated using EXIF Orientation tag + // info.width and info.height contain the dimensions of the resized image + }); +readableStream.pipe(pipeline); + +sharp(input) + .extract({ left: 0, top: 0, width: 100, height: 100 }) + .toFile('output', (err: Error) => { + // Extract a region of the input image, saving in the same format. + }); + +sharp(input) + .extract({ left: 0, top: 0, width: 100, height: 100 }) + .resize(200, 200) + .extract({ left: 0, top: 0, width: 100, height: 100 }) + .toFile('output', (err: Error) => { + // Extract a region, resize, then extract from the resized image + }); + +// Resize to 140 pixels wide, then add 10 transparent pixels +// to the top, left and right edges and 20 to the bottom edge +sharp(input) + .resize(140, null, { background: { r: 0, g: 0, b: 0, alpha: 0 } }) + .extend({ top: 10, bottom: 20, left: 10, right: 10 }); + +sharp(input) + .convolve({ + width: 3, + height: 3, + kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1], + }) + .raw() + .toBuffer((err: Error, data: Buffer, info: sharp.OutputInfo) => { + // data contains the raw pixel data representing the convolution + // of the input image with the horizontal Sobel operator + }); + +sharp('input.tiff') + .png() + .tile({ + size: 512, + }) + .toFile('output.dz', (err: Error, info: sharp.OutputInfo) => { + // output.dzi is the Deep Zoom XML definition + // output_files contains 512x512 tiles grouped by zoom level + }); + +sharp('input.tiff') + .png() + .tile({ + size: 512, + center: true, + layout: 'iiif3', + id: 'https://my.image.host/iiif', + }) + .toFile('output'); + +sharp(input) + .resize(200, 300, { + fit: 'contain', + position: 'north', + kernel: sharp.kernel.lanczos2, + background: 'white', + }) + .toFile('output.tiff') + .then(() => { + // output.tiff is a 200 pixels wide and 300 pixels high image + // containing a lanczos2/nohalo scaled version, embedded on a white canvas, + // of the image data in inputBuffer + }); + +sharp(input).resize({ kernel: 'mks2013' }); + +transformer = sharp() + .resize(200, 200, { + fit: 'cover', + position: sharp.strategy.entropy, + }) + .on('error', (err: Error) => { + console.log(err); + }); +// Read image data from readableStream +// Write 200px square auto-cropped image data to writableStream +readableStream.pipe(transformer).pipe(writableStream); + +sharp('input.gif') + .resize(200, 300, { + fit: 'contain', + position: 'north', + background: { r: 0, g: 0, b: 0, alpha: 0 }, + }) + .toFormat(sharp.format.webp) + .toBuffer((err: Error, outputBuffer: Buffer) => { + if (err) { + throw err; + } + // outputBuffer contains WebP image data of a 200 pixels wide and 300 pixels high + // containing a scaled version, embedded on a transparent canvas, of input.gif + }); + +sharp(input) + .resize(200, 200, { fit: 'inside' }) + .toFormat('jpeg') + .toBuffer() + .then((outputBuffer: Buffer) => { + // outputBuffer contains JPEG image data no wider than 200 pixels and no higher + // than 200 pixels regardless of the inputBuffer image dimensions + }); + +sharp(input) + .resize(100, 100) + .toFormat('jpg') + .toBuffer({ resolveWithObject: false }) + .then((outputBuffer: Buffer) => { + // Resolves with a Buffer object when resolveWithObject is false + }); + +sharp(input) + .resize(100, 100) + .toBuffer({ resolveWithObject: true }) + .then((object: { data: Buffer; info: sharp.OutputInfo }) => { + // Resolve with an object containing data Buffer and an OutputInfo object + // when resolveWithObject is true + }); + +sharp(input) + .resize(640, 480, { withoutEnlargement: true }) + .toFormat('jpeg') + .toBuffer() + .then((outputBuffer: Buffer) => { + // outputBuffer contains JPEG image data no larger than the input + }); + +sharp(input) + .resize(640, 480, { withoutReduction: true }) + .toFormat('jpeg') + .toBuffer() + .then((outputBuffer: Buffer) => { + // outputBuffer contains JPEG image data no smaller than the input + }); + +// Output to tif +sharp(input) + .resize(100, 100) + .toFormat('tif') + .toFormat('tiff') + .toFormat(sharp.format.tif) + .toFormat(sharp.format.tiff) + .toBuffer(); + +const stats = sharp.cache(); + +sharp.cache({ items: 200 }); +sharp.cache({ files: 0 }); +sharp.cache(false); + +const threads = sharp.concurrency(); // 4 +sharp.concurrency(2); // 2 +sharp.concurrency(0); // 4 + +const counters = sharp.counters(); // { queue: 2, process: 4 } + +let simd: boolean = sharp.simd(); +// simd is `true` if SIMD is currently enabled + +simd = sharp.simd(true); +// attempts to enable the use of SIMD, returning true if available + +const vipsVersion: string = sharp.versions.vips; + +if (sharp.versions.cairo) { + const cairoVersion: string = sharp.versions.cairo; +} + +sharp('input.gif') + .linear(1) + .linear(1, 0) + .linear(null, 0) + .linear([0.25, 0.5, 0.75], [150, 100, 50]) + + .recomb([ + [0.3588, 0.7044, 0.1368], + [0.299, 0.587, 0.114], + [0.2392, 0.4696, 0.0912], + ]) + + .recomb([ + [1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1], + ]) + + .modulate({ brightness: 2 }) + .modulate({ hue: 180 }) + .modulate({ lightness: 10 }) + .modulate({ brightness: 0.5, saturation: 0.5, hue: 90 }); + +// From https://sharp.pixelplumbing.com/api-output#examples-9 +// Extract raw RGB pixel data from JPEG input +sharp('input.jpg') + .raw({ depth: 'ushort' }) + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { + console.log(data); + console.log(info); + }); + +sharp(input).jpeg().jpeg({}).jpeg({ + progressive: false, + chromaSubsampling: '4:4:4', + trellisQuantisation: false, + overshootDeringing: false, + optimiseScans: false, + optimizeScans: false, + optimiseCoding: false, + optimizeCoding: false, + quantisationTable: 10, + quantizationTable: 10, + mozjpeg: false, + quality: 10, + force: false, +}); + +sharp(input).png().png({}).png({ + progressive: false, + compressionLevel: 10, + adaptiveFiltering: false, + force: false, + quality: 10, + palette: false, + colours: 10, + colors: 10, + dither: 10, +}); + +sharp(input) + .avif() + .avif({}) + .avif({ quality: 50, lossless: false, effort: 5, chromaSubsampling: '4:2:0' }) + .heif() + .heif({}) + .heif({ quality: 50, compression: 'hevc', lossless: false, effort: 5, chromaSubsampling: '4:2:0' }) + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { + console.log(data); + console.log(info); + }); + +sharp(input) + .gif() + .gif({}) + .gif({ loop: 0, delay: [], force: true }) + .gif({ delay: 30 }) + .gif({ reuse: true }) + .gif({ reuse: false }) + .gif({ progressive: true }) + .gif({ progressive: false }) + .gif({ keepDuplicateFrames: true }) + .gif({ keepDuplicateFrames: false }) + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { + console.log(data); + console.log(info); + }); + +sharp(input) + .tiff({ compression: 'packbits' }) + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { + console.log(data); + console.log(info); + }); + +sharp('input.jpg') + .stats() + .then(stats => { + const { + sharpness, + dominant: { r, g, b }, + } = stats; + console.log(sharpness); + console.log(`${r}, ${g}, ${b}`); + }); + +// From https://sharp.pixelplumbing.com/api-output#examples-9 +// Extract alpha channel as raw pixel data from PNG input +sharp('input.png').ensureAlpha().ensureAlpha(0).extractChannel(3).toColourspace('b-w').raw().toBuffer(); + +// From https://sharp.pixelplumbing.com/api-constructor#examples-4 +// Convert an animated GIF to an animated WebP +sharp('in.gif', { animated: true }).toFile('out.webp'); + +// From https://github.com/lovell/sharp/issues/2701 +// Type support for limitInputPixels +sharp({ + create: { + background: 'red', + channels: 4, + height: 25000, + width: 25000, + pageHeight: 1000, + }, + limitInputPixels: false, +}) + .toFormat('png') + .toBuffer() + .then(largeImage => sharp(input).composite([{ input: largeImage, limitInputPixels: false }])); + +// Taken from API documentation at +// https://sharp.pixelplumbing.com/api-operation#clahe +// introduced +sharp('input.jpg').clahe({ width: 10, height: 10 }).toFile('output.jpg'); + +sharp('input.jpg').clahe({ width: 10, height: 10, maxSlope: 5 }).toFile('outfile.jpg'); + +// Support `unlimited` input option +sharp('input.png', { unlimited: true }).resize(320, 240).toFile('outfile.png'); + +// Support creating with noise +sharp({ + create: { + background: 'red', + channels: 4, + height: 100, + width: 100, + noise: { + type: 'gaussian', + mean: 128, + sigma: 30, + }, + }, +}) + .png() + .toFile('output.png'); + +sharp(new Uint8Array(input.buffer)).toFile('output.jpg'); + +// Support for negate options +sharp('input.png').negate({ alpha: false }).toFile('output.png'); + +// From https://github.com/lovell/sharp/pull/2704 +// Type support for pipelineColourspace +sharp(input) + .pipelineColourspace('rgb16') + .resize(320, 240) + .gamma() + .toColourspace('srgb') // this is the default, but included here for clarity + .toBuffer(); + +// From https://github.com/lovell/sharp/pull/1439 +// Second parameter to gamma operation for different output gamma +sharp(input) + .resize(129, 111) + .gamma(2.2, 3.0) + .toBuffer(err => { + if (err) throw err; + }); + +// Support for raw depth specification +sharp('16bpc.png') + .toColourspace('rgb16') + .raw({ depth: 'ushort' }) + .toBuffer((error, data, { width, height, channels, size }) => { + console.log((size / width / height / channels) * 8); + console.log(new Uint16Array(data.buffer)); + }); + +// Output channels are constrained from 1-4, can be used as raw input +sharp(input) + .toBuffer({ resolveWithObject: true }) + .then(result => { + const newImg = sharp(result.data, { + raw: { + channels: result.info.channels, + width: result.info.width, + height: result.info.height, + }, + }); + + return newImg.toBuffer(); + }); + +// Support for specifying a timeout +sharp('someImage.png').timeout({ seconds: 30 }).resize(300, 300).toBuffer(); + +// Support for `effort` in different formats +sharp('input.tiff').png({ effort: 9 }).toFile('out.png'); +sharp('input.tiff').webp({ effort: 9 }).toFile('out.webp'); +sharp('input.tiff').avif({ effort: 9 }).toFile('out.avif'); +sharp('input.tiff').heif({ effort: 9 }).toFile('out.heif'); +sharp('input.tiff').gif({ effort: 9 }).toFile('out.gif'); + +// Support for `colors`/`colours` for gif output +sharp('input.gif').gif({ colors: 16 }).toFile('out.gif'); +sharp('input.gif').gif({ colours: 16 }).toFile('out.gif'); + +// Support for `dither` for gif/png output +sharp('input.gif').gif({ dither: 0.5 }).toFile('out.gif'); +sharp('input.gif').png({ dither: 0.5 }).toFile('out.png'); + +// Support for `interFrameMaxError` for gif output +sharp('input.gif').gif({ interFrameMaxError: 0 }).toFile('out.gif'); + +// Support for `interPaletteMaxError` for gif output +sharp('input.gif').gif({ interPaletteMaxError: 0 }).toFile('out.gif'); + +// Support for `resolutionUnit` for tiff output +sharp('input.tiff').tiff({ resolutionUnit: 'cm' }).toFile('out.tiff'); + +// Support for `jp2` output with different options +sharp('input.tiff').jp2().toFile('out.jp2'); +sharp('input.tiff').jp2({ quality: 50 }).toFile('out.jp2'); +sharp('input.tiff').jp2({ lossless: true }).toFile('out.jp2'); +sharp('input.tiff').jp2({ tileWidth: 128, tileHeight: 128 }).toFile('out.jp2'); +sharp('input.tiff').jp2({ chromaSubsampling: '4:2:0' }).toFile('out.jp2'); + +// Support for `jxl` output with different options +sharp('input.tiff').jxl().toFile('out.jxl'); +sharp('input.tiff').jxl({ distance: 15.0 }).toFile('out.jxl'); +sharp('input.tiff').jxl({ quality: 50 }).toFile('out.jxl'); +sharp('input.tiff').jxl({ decodingTier: 4 }).toFile('out.jxl'); +sharp('input.tiff').jxl({ lossless: true }).toFile('out.jxl'); +sharp('input.tiff').jxl({ effort: 7 }).toFile('out.jxl'); + +// Support `minSize` and `mixed` webp options +sharp('input.tiff').webp({ minSize: true, mixed: true }).toFile('out.gif'); + +// 'failOn' input param +sharp('input.tiff', { failOn: 'none' }); +sharp('input.tiff', { failOn: 'truncated' }); +sharp('input.tiff', { failOn: 'error' }); +sharp('input.tiff', { failOn: 'warning' }); + +// Sharpen operation taking an object instead of three params +sharp('input.tiff').sharpen().toBuffer(); +sharp('input.tiff').sharpen({ sigma: 2 }).toBuffer(); +sharp('input.tiff') + .sharpen({ + sigma: 2, + m1: 0, + m2: 3, + x1: 3, + y2: 15, + y3: 15, + }) + .toBuffer(); + +// Affine operator + interpolator hash +sharp().affine( + [ + [1, 0.3], + [0.1, 0.7], + ], + { + background: 'white', + interpolator: sharp.interpolators.nohalo, + }, +); + +sharp().affine([1, 1, 1, 1], { + background: 'white', + idx: 0, + idy: 0, + odx: 0, + ody: 0, +}); + +const bicubic: string = sharp.interpolators.bicubic; +const bilinear: string = sharp.interpolators.bilinear; +const locallyBoundedBicubic: string = sharp.interpolators.locallyBoundedBicubic; +const nearest: string = sharp.interpolators.nearest; +const nohalo: string = sharp.interpolators.nohalo; +const vertexSplitQuadraticBasisSpline: string = sharp.interpolators.vertexSplitQuadraticBasisSpline; + +// Triming +sharp(input).trim({ background: '#000' }).toBuffer(); +sharp(input).trim({ threshold: 10, lineArt: true }).toBuffer(); +sharp(input).trim({ background: '#bf1942', threshold: 30 }).toBuffer(); + +// Text input +sharp({ + text: { + text: 'Hello world', + align: 'centre', + dpi: 72, + font: 'Arial', + fontfile: 'path/to/arial.ttf', + height: 500, + width: 500, + rgba: true, + justify: true, + spacing: 10, + wrap: 'word-char', + }, +}) + .png() + .toBuffer({ resolveWithObject: true }) + .then(out => { + console.log(out.info.textAutofitDpi); + }); + +// Text composite +sharp('input.png').composite([ + { + input: { + text: { + text: 'Okay then', + font: 'Comic Sans', + }, + }, + }, +]); + +// From https://github.com/lovell/sharp/pull/1835 +sharp('input.png').composite([ + { + input: { + text: { + text: 'Okay then', + font: 'Comic Sans', + }, + }, + blend: 'color-burn', + top: 0, + left: 0, + premultiplied: true, + }, +]); + +// https://github.com/lovell/sharp/pull/402 +(['fs', 'zip'] as const).forEach(container => { + sharp().tile({ container }); +}); + +// From https://github.com/lovell/sharp/issues/2238 +sharp('input.png').tile({ + basename: 'output.dz.tiles', +}); + +// https://github.com/lovell/sharp/issues/3669 +sharp(input).composite([ + { + raw: { + width: 1, + height: 1, + channels: 1, + premultiplied: false, + }, + sequentialRead: false, + unlimited: true, + } +]); + +// Support for webp preset in types +// https://github.com/lovell/sharp/issues/3747 +sharp('input.tiff').webp({ preset: 'photo' }).toFile('out.webp'); +sharp('input.tiff').webp({ preset: 'picture' }).toFile('out.webp'); +sharp('input.tiff').webp({ preset: 'icon' }).toFile('out.webp'); +sharp('input.tiff').webp({ preset: 'drawing' }).toFile('out.webp'); +sharp('input.tiff').webp({ preset: 'text' }).toFile('out.webp'); +sharp('input.tiff').webp({ preset: 'default' }).toFile('out.webp'); + +sharp(input) + .keepExif() + .withExif({ + IFD0: { + k1: 'v1' + } + }) + .withExifMerge({ + IFD1: { + k2: 'v2' + } + }) + .keepXmp() + .withXmp('test') + .keepIccProfile() + .withIccProfile('filename') + .withIccProfile('filename', { attach: false }); + +// Added missing types for OverlayOptions +// https://github.com/lovell/sharp/pull/4048 +sharp(input).composite([ + { + input: 'image.gif', + animated: true, + limitInputPixels: 536805378, + density: 144, + failOn: "warning", + autoOrient: true + } +]) +sharp(input).composite([ + { + input: 'image.png', + animated: false, + limitInputPixels: 178935126, + density: 72, + failOn: "truncated" + } +]) + +// Support format-specific input options +const colour: sharp.Colour = '#fff'; +const color: sharp.Color = '#fff'; +sharp({ pdf: { background: colour } }); +sharp({ pdf: { background: color } }); +sharp({ pdfBackground: colour }); // Deprecated +sharp({ pdfBackground: color }); // Deprecated +sharp({ tiff: { subifd: 3 } }); +sharp({ subifd: 3 }); // Deprecated +sharp({ openSlide: { level: 0 } }); +sharp({ level: 0 }); // Deprecated +sharp({ jp2: { oneshot: true } }); +sharp({ jp2: { oneshot: false } }); +sharp({ svg: { stylesheet: 'test' }}); +sharp({ svg: { highBitdepth: true }}); +sharp({ svg: { highBitdepth: false }}); + +// Raw input options +const raw: sharp.Raw = { width: 1, height: 1, channels: 3 }; +sharp({ raw }); +sharp({ raw: { ...raw, premultiplied: true } }); +sharp({ raw: { ...raw, premultiplied: false } }); +sharp({ raw: { ...raw, pageHeight: 1 } }); + +sharp({ autoOrient: true }); +sharp({ autoOrient: false }); +sharp().autoOrient(); + +sharp([input, input]); +sharp([input, input], { + join: { + animated: true + } +}); +sharp([input, input], { + join: { + across: 2, + shim: 5, + background: colour, + halign: 'centre', + valign: 'bottom' + } +}); + +sharp().erode(); +sharp().erode(1); +sharp().dilate(); +sharp().dilate(1); diff --git a/test/types/tsconfig.json b/test/types/tsconfig.json new file mode 100644 index 000000000..d8ec98092 --- /dev/null +++ b/test/types/tsconfig.json @@ -0,0 +1,4 @@ +{ + "module": "commonjs", + "strict": true +} diff --git a/test/unit.mjs b/test/unit.mjs new file mode 100644 index 000000000..0a1aa3c0f --- /dev/null +++ b/test/unit.mjs @@ -0,0 +1,16 @@ +import { readdir } from 'node:fs/promises'; +import { run } from 'node:test'; +import { spec } from 'node:test/reporters'; + +const files = (await readdir('./test/unit')).map((f) => `./test/unit/${f}`); + +run({ + files, + concurrency: true, + timeout: 60000, + coverage: true, + coverageIncludeGlobs: ['lib/*.js'], + branchCoverage: 100, +}) + .compose(new spec()) + .pipe(process.stdout); diff --git a/test/unit/affine.js b/test/unit/affine.js index 03f4acedd..43ff8ed92 100644 --- a/test/unit/affine.js +++ b/test/unit/affine.js @@ -1,6 +1,10 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); @@ -155,6 +159,15 @@ describe('Affine transform', () => { fixtures.assertSimilar(fixtures.expected('affine-background-all-offsets-expected.jpg'), data, done); }); }); + + it('Animated image rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .affine([1, 1, 1, 1]) + .toBuffer(), + /Affine is not supported for multi-page images/ + ) + ); + describe('Interpolations', () => { const input = fixtures.inputJpg320x240; const inputWidth = 320; diff --git a/test/unit/agent.js b/test/unit/agent.js deleted file mode 100644 index a49e0ed66..000000000 --- a/test/unit/agent.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const agent = require('../../lib/agent'); - -describe('HTTP agent', function () { - it('Without proxy', function () { - assert.strictEqual(null, agent()); - }); - - it('HTTPS proxy with auth from HTTPS_PROXY', function () { - process.env.HTTPS_PROXY = 'https://user:pass@secure:123'; - const proxy = agent(); - delete process.env.HTTPS_PROXY; - assert.strictEqual('object', typeof proxy); - assert.strictEqual('secure', proxy.options.proxy.host); - assert.strictEqual(123, proxy.options.proxy.port); - assert.strictEqual('user:pass', proxy.options.proxy.proxyAuth); - assert.strictEqual(443, proxy.defaultPort); - }); - - it('HTTP proxy without auth from npm_config_proxy', function () { - process.env.npm_config_proxy = 'http://plaintext:456'; - const proxy = agent(); - delete process.env.npm_config_proxy; - assert.strictEqual('object', typeof proxy); - assert.strictEqual('plaintext', proxy.options.proxy.host); - assert.strictEqual(456, proxy.options.proxy.port); - assert.strictEqual(null, proxy.options.proxy.proxyAuth); - assert.strictEqual(443, proxy.defaultPort); - }); -}); diff --git a/test/unit/alpha.js b/test/unit/alpha.js index 6c162acf6..42e178098 100644 --- a/test/unit/alpha.js +++ b/test/unit/alpha.js @@ -1,15 +1,19 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const fixtures = require('../fixtures'); const sharp = require('../../'); -describe('Alpha transparency', function () { - it('Flatten to black', function (done) { +describe('Alpha transparency', () => { + it('Flatten to black', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .flatten() .resize(400, 300) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(400, info.width); assert.strictEqual(300, info.height); @@ -17,13 +21,14 @@ describe('Alpha transparency', function () { }); }); - it('Flatten to RGB orange', function (done) { + it('Flatten to RGB orange', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(400, 300) .flatten({ background: { r: 255, g: 102, b: 0 } }) - .toBuffer(function (err, data, info) { + .jpeg({ chromaSubsampling: '4:4:4' }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(400, info.width); assert.strictEqual(300, info.height); @@ -31,11 +36,12 @@ describe('Alpha transparency', function () { }); }); - it('Flatten to CSS/hex orange', function (done) { + it('Flatten to CSS/hex orange', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(400, 300) .flatten({ background: '#ff6600' }) - .toBuffer(function (err, data, info) { + .jpeg({ chromaSubsampling: '4:4:4' }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(400, info.width); assert.strictEqual(300, info.height); @@ -43,13 +49,13 @@ describe('Alpha transparency', function () { }); }); - it('Flatten 16-bit PNG with transparency to orange', function (done) { + it('Flatten 16-bit PNG with transparency to orange', (_t, done) => { const output = fixtures.path('output.flatten-rgb16-orange.jpg'); sharp(fixtures.inputPngWithTransparency16bit) .flatten({ background: { r: 255, g: 102, b: 0 } }) - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(true, info.size > 0); assert.strictEqual(32, info.width); @@ -59,10 +65,10 @@ describe('Alpha transparency', function () { }); }); - it('Do not flatten', function (done) { + it('Do not flatten', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .flatten(false) - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels); @@ -70,10 +76,10 @@ describe('Alpha transparency', function () { }); }); - it('Ignored for JPEG', function (done) { + it('Ignored for JPEG', (_t, done) => { sharp(fixtures.inputJpg) .flatten({ background: '#ff0000' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(3, info.channels); @@ -93,66 +99,81 @@ describe('Alpha transparency', function () { }); }); - it('Enlargement with non-nearest neighbor interpolation shouldn’t cause dark edges', function () { + it('Enlargement with non-nearest neighbor interpolation shouldn’t cause dark edges', () => { const base = 'alpha-premultiply-enlargement-2048x1536-paper.png'; - const actual = fixtures.path('output.' + base); + const actual = fixtures.path(`output.${base}`); const expected = fixtures.expected(base); return sharp(fixtures.inputPngAlphaPremultiplicationSmall) .resize(2048, 1536) .toFile(actual) - .then(function () { + .then(() => { fixtures.assertMaxColourDistance(actual, expected, 102); }); }); - it('Reduction with non-nearest neighbor interpolation shouldn’t cause dark edges', function () { + it('Reduction with non-nearest neighbor interpolation shouldn’t cause dark edges', () => { const base = 'alpha-premultiply-reduction-1024x768-paper.png'; - const actual = fixtures.path('output.' + base); + const actual = fixtures.path(`output.${base}`); const expected = fixtures.expected(base); return sharp(fixtures.inputPngAlphaPremultiplicationLarge) .resize(1024, 768) .toFile(actual) - .then(function () { + .then(() => { fixtures.assertMaxColourDistance(actual, expected, 102); }); }); - it('Removes alpha from fixtures with transparency, ignores those without', function () { - return Promise.all([ + it('Removes alpha from fixtures with transparency, ignores those without', () => Promise.all([ fixtures.inputPngWithTransparency, fixtures.inputPngWithTransparency16bit, fixtures.inputWebPWithTransparency, fixtures.inputJpg, fixtures.inputPng, fixtures.inputWebP - ].map(function (input) { - return sharp(input) + ].map((input) => sharp(input) .resize(10) .removeAlpha() .toBuffer({ resolveWithObject: true }) - .then(function (result) { + .then((result) => { assert.strictEqual(3, result.info.channels); - }); - })); - }); + })))); - it('Ensures alpha from fixtures without transparency, ignores those with', function () { - return Promise.all([ + it('Ensures alpha from fixtures without transparency, ignores those with', () => Promise.all([ fixtures.inputPngWithTransparency, fixtures.inputPngWithTransparency16bit, fixtures.inputWebPWithTransparency, fixtures.inputJpg, fixtures.inputPng, fixtures.inputWebP - ].map(function (input) { - return sharp(input) + ].map((input) => sharp(input) .resize(10) .ensureAlpha() .png() .toBuffer({ resolveWithObject: true }) - .then(function (result) { + .then((result) => { assert.strictEqual(4, result.info.channels); - }); - })); + })))); + + it('Valid ensureAlpha value used for alpha channel', async () => { + const background = { r: 255, g: 0, b: 0 }; + const [r, g, b, alpha] = await sharp({ + create: { + width: 8, + height: 8, + channels: 3, + background + } + }) + .ensureAlpha(0.5) + .raw() + .toBuffer(); + + assert.deepStrictEqual({ r, g, b, alpha }, { ...background, alpha: 127 }); + }); + + it('Invalid ensureAlpha value throws', async () => { + assert.throws(() => { + sharp().ensureAlpha('fail'); + }); }); }); diff --git a/test/unit/avif.js b/test/unit/avif.js index 37ba73018..6fb08c6fb 100644 --- a/test/unit/avif.js +++ b/test/unit/avif.js @@ -1,9 +1,13 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); -const { inputAvif, inputJpg } = require('../fixtures'); +const { inputAvif, inputJpg, inputGifAnimated } = require('../fixtures'); describe('AVIF', () => { it('called without options does not throw an error', () => { @@ -17,10 +21,13 @@ describe('AVIF', () => { .resize(32) .jpeg() .toBuffer(); - const metadata = await sharp(data) - .metadata(); - const { compression, size, ...metadataWithoutSize } = metadata; - assert.deepStrictEqual(metadataWithoutSize, { + const { size, ...metadata } = await sharp(data).metadata(); + void size; + assert.deepStrictEqual(metadata, { + autoOrient: { + height: 13, + width: 32 + }, channels: 3, chromaSubsampling: '4:2:0', density: 72, @@ -28,8 +35,11 @@ describe('AVIF', () => { format: 'jpeg', hasAlpha: false, hasProfile: false, + // 32 / (2048 / 858) = 13.40625 + // Math.round(13.40625) = 13 height: 13, isProgressive: false, + isPalette: false, space: 'srgb', width: 32 }); @@ -38,20 +48,25 @@ describe('AVIF', () => { it('can convert JPEG to AVIF', async () => { const data = await sharp(inputJpg) .resize(32) - .avif() + .avif({ effort: 0 }) .toBuffer(); - const metadata = await sharp(data) - .metadata(); - const { compression, size, ...metadataWithoutSize } = metadata; - assert.deepStrictEqual(metadataWithoutSize, { + const { size, ...metadata } = await sharp(data).metadata(); + void size; + assert.deepStrictEqual(metadata, { + autoOrient: { + height: 26, + width: 32 + }, channels: 3, + compression: 'av1', depth: 'uchar', format: 'heif', hasAlpha: false, hasProfile: false, height: 26, isProgressive: false, - pageHeight: 26, + isPalette: false, + bitsPerSample: 8, pagePrimary: 0, pages: 1, space: 'srgb', @@ -63,22 +78,107 @@ describe('AVIF', () => { const data = await sharp(inputAvif) .resize(32) .toBuffer(); - const metadata = await sharp(data) - .metadata(); - const { compression, size, ...metadataWithoutSize } = metadata; - assert.deepStrictEqual(metadataWithoutSize, { + const { size, ...metadata } = await sharp(data).metadata(); + void size; + assert.deepStrictEqual(metadata, { + autoOrient: { + height: 13, + width: 32 + }, channels: 3, + compression: 'av1', depth: 'uchar', format: 'heif', hasAlpha: false, hasProfile: false, - height: 12, + height: 13, + isProgressive: false, + isPalette: false, + bitsPerSample: 8, + pagePrimary: 0, + pages: 1, + space: 'srgb', + width: 32 + }); + }); + + it('can convert animated GIF to non-animated AVIF', async () => { + const data = await sharp(inputGifAnimated, { animated: true }) + .resize(10) + .avif({ effort: 0 }) + .toBuffer(); + const { size, ...metadata } = await sharp(data).metadata(); + void size; + assert.deepStrictEqual(metadata, { + autoOrient: { + height: 300, + width: 10 + }, + channels: 4, + compression: 'av1', + depth: 'uchar', + format: 'heif', + hasAlpha: true, + hasProfile: false, + height: 300, + isProgressive: false, + isPalette: false, + bitsPerSample: 8, + pagePrimary: 0, + pages: 1, + space: 'srgb', + width: 10 + }); + }); + + it('should cast to uchar', async () => { + const data = await sharp(inputJpg) + .resize(32) + .sharpen() + .avif({ effort: 0 }) + .toBuffer(); + const { size, ...metadata } = await sharp(data).metadata(); + void size; + assert.deepStrictEqual(metadata, { + autoOrient: { + height: 26, + width: 32 + }, + channels: 3, + compression: 'av1', + depth: 'uchar', + format: 'heif', + hasAlpha: false, + hasProfile: false, + height: 26, isProgressive: false, - pageHeight: 12, + isPalette: false, + bitsPerSample: 8, pagePrimary: 0, pages: 1, space: 'srgb', width: 32 }); }); + + it('Invalid width - too large', async () => + assert.rejects( + () => sharp({ create: { width: 16385, height: 16, channels: 3, background: 'red' } }).avif().toBuffer(), + /Processed image is too large for the HEIF format/ + ) + ); + + it('Invalid height - too large', async () => + assert.rejects( + () => sharp({ create: { width: 16, height: 16385, channels: 3, background: 'red' } }).avif().toBuffer(), + /Processed image is too large for the HEIF format/ + ) + ); + + it('Invalid bitdepth value throws error', () => + assert.throws( + () => sharp().avif({ bitdepth: 11 }), + /Expected 8, 10 or 12 for bitdepth but received 11 of type number/ + ) + ); }); diff --git a/test/unit/bandbool.js b/test/unit/bandbool.js index dfc98cde5..073fb68ba 100644 --- a/test/unit/bandbool.js +++ b/test/unit/bandbool.js @@ -1,48 +1,52 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const fixtures = require('../fixtures'); const sharp = require('../../'); -describe('Bandbool per-channel boolean operations', function () { +describe('Bandbool per-channel boolean operations', () => { [ sharp.bool.and, sharp.bool.or, sharp.bool.eor ] - .forEach(function (op) { - it(op + ' operation', function (done) { + .forEach((op) => { + it(`${op} operation`, (_t, done) => { sharp(fixtures.inputPngBooleanNoAlpha) .bandbool(op) .toColourspace('b-w') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(200, info.width); assert.strictEqual(200, info.height); assert.strictEqual(1, info.channels); - fixtures.assertSimilar(fixtures.expected('bandbool_' + op + '_result.png'), data, done); + fixtures.assertSimilar(fixtures.expected(`bandbool_${op}_result.png`), data, done); }); }); }); - it('sRGB image retains 3 channels', function (done) { + it('sRGB image retains 3 channels', (_t, done) => { sharp(fixtures.inputJpg) .bandbool('and') - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual(3, info.channels); done(); }); }); - it('Invalid operation', function () { - assert.throws(function () { + it('Invalid operation', () => { + assert.throws(() => { sharp().bandbool('fail'); }); }); - it('Missing operation', function () { - assert.throws(function () { + it('Missing operation', () => { + assert.throws(() => { sharp().bandbool(); }); }); diff --git a/test/unit/beforeEach.js b/test/unit/beforeEach.js deleted file mode 100644 index e2fe4fcbd..000000000 --- a/test/unit/beforeEach.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -const detectLibc = require('detect-libc'); -const sharp = require('../../'); - -const usingCache = detectLibc.family !== detectLibc.MUSL; -const usingSimd = !process.env.G_DEBUG; -const concurrency = - detectLibc.family === detectLibc.MUSL || process.arch === 'arm' - ? 1 - : undefined; - -beforeEach(function () { - sharp.cache(usingCache); - sharp.simd(usingSimd); - sharp.concurrency(concurrency); -}); - -afterEach(function () { - if (global.gc) { - global.gc(); - } -}); diff --git a/test/unit/blur.js b/test/unit/blur.js index 72b853036..f5132098b 100644 --- a/test/unit/blur.js +++ b/test/unit/blur.js @@ -1,16 +1,20 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Blur', function () { - it('specific radius 1', function (done) { +describe('Blur', () => { + it('specific radius 1', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .blur(1) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -19,11 +23,11 @@ describe('Blur', function () { }); }); - it('specific radius 10', function (done) { + it('specific radius 10', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .blur(10) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -32,11 +36,24 @@ describe('Blur', function () { }); }); - it('specific radius 0.3', function (done) { + it('specific options.sigma 10', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 240) + .blur({ sigma: 10 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), data, done); + }); + }); + + it('specific radius 0.3', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .blur(0.3) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -45,11 +62,11 @@ describe('Blur', function () { }); }); - it('mild blur', function (done) { + it('mild blur', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .blur() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -58,17 +75,17 @@ describe('Blur', function () { }); }); - it('invalid radius', function () { - assert.throws(function () { + it('invalid radius', () => { + assert.throws(() => { sharp(fixtures.inputJpg).blur(0.1); }); }); - it('blurred image is smaller than non-blurred', function (done) { + it('blurred image is smaller than non-blurred', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .blur(false) - .toBuffer(function (err, notBlurred, info) { + .toBuffer((err, notBlurred, info) => { if (err) throw err; assert.strictEqual(true, notBlurred.length > 0); assert.strictEqual('jpeg', info.format); @@ -77,7 +94,7 @@ describe('Blur', function () { sharp(fixtures.inputJpg) .resize(320, 240) .blur(true) - .toBuffer(function (err, blurred, info) { + .toBuffer((err, blurred, info) => { if (err) throw err; assert.strictEqual(true, blurred.length > 0); assert.strictEqual(true, blurred.length < notBlurred.length); @@ -88,4 +105,54 @@ describe('Blur', function () { }); }); }); + + it('invalid precision', () => { + assert.throws(() => { + sharp(fixtures.inputJpg).blur({ sigma: 1, precision: 'invalid' }); + }, /Expected one of: integer, float, approximate for precision but received invalid of type string/); + }); + + it('invalid minAmplitude', () => { + assert.throws(() => { + sharp(fixtures.inputJpg).blur({ sigma: 1, minAmplitude: 0 }); + }, /Expected number between 0.001 and 1 for minAmplitude but received 0 of type number/); + + assert.throws(() => { + sharp(fixtures.inputJpg).blur({ sigma: 1, minAmplitude: 1.01 }); + }, /Expected number between 0.001 and 1 for minAmplitude but received 1.01 of type number/); + }); + + it('specific radius 10 and precision approximate', async () => { + const approximate = await sharp(fixtures.inputJpg) + .resize(320, 240) + .blur({ sigma: 10, precision: 'approximate' }) + .toBuffer(); + const integer = await sharp(fixtures.inputJpg) + .resize(320, 240) + .blur(10) + .toBuffer(); + + assert.notDeepEqual(approximate, integer); + await fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), approximate); + }); + + it('specific radius 10 and minAmplitude 0.01', async () => { + const minAmplitudeLow = await sharp(fixtures.inputJpg) + .resize(320, 240) + .blur({ sigma: 10, minAmplitude: 0.01 }) + .toBuffer(); + const minAmplitudeDefault = await sharp(fixtures.inputJpg) + .resize(320, 240) + .blur(10) + .toBuffer(); + + assert.notDeepEqual(minAmplitudeLow, minAmplitudeDefault); + await fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), minAmplitudeLow); + }); + + it('options.sigma is required if options object is passed', () => { + assert.throws(() => { + sharp(fixtures.inputJpg).blur({ precision: 'invalid' }); + }, /Expected number between 0.3 and 1000 for options.sigma but received undefined of type undefined/); + }); }); diff --git a/test/unit/boolean.js b/test/unit/boolean.js index 98680c1a7..f85b376da 100644 --- a/test/unit/boolean.js +++ b/test/unit/boolean.js @@ -1,12 +1,16 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const fixtures = require('../fixtures'); const sharp = require('../../'); -describe('Boolean operation between two images', function () { +describe('Boolean operation between two images', () => { const inputJpgBooleanTestBuffer = fs.readFileSync(fixtures.inputJpgBooleanTest); [ @@ -14,63 +18,63 @@ describe('Boolean operation between two images', function () { sharp.bool.or, sharp.bool.eor ] - .forEach(function (op) { - it(op + ' operation, file', function (done) { + .forEach((op) => { + it(`${op} operation, file`, (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .boolean(fixtures.inputJpgBooleanTest, op) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - fixtures.assertSimilar(fixtures.expected('boolean_' + op + '_result.jpg'), data, done); + fixtures.assertSimilar(fixtures.expected(`boolean_${op}_result.jpg`), data, done); }); }); - it(op + ' operation, buffer', function (done) { + it(`${op} operation, buffer`, (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .boolean(inputJpgBooleanTestBuffer, op) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - fixtures.assertSimilar(fixtures.expected('boolean_' + op + '_result.jpg'), data, done); + fixtures.assertSimilar(fixtures.expected(`boolean_${op}_result.jpg`), data, done); }); }); - it(op + ' operation, raw', function (done) { + it(`${op} operation, raw`, (_t, done) => { sharp(fixtures.inputJpgBooleanTest) .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; sharp(fixtures.inputJpg) .resize(320, 240) .boolean(data, op, { raw: info }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - fixtures.assertSimilar(fixtures.expected('boolean_' + op + '_result.jpg'), data, done); + fixtures.assertSimilar(fixtures.expected(`boolean_${op}_result.jpg`), data, done); }); }); }); }); - it('Invalid operation', function () { - assert.throws(function () { + it('Invalid operation', () => { + assert.throws(() => { sharp().boolean(fixtures.inputJpgBooleanTest, 'fail'); }); }); - it('Invalid operation, non-string', function () { - assert.throws(function () { + it('Invalid operation, non-string', () => { + assert.throws(() => { sharp().boolean(fixtures.inputJpgBooleanTest, null); }); }); - it('Missing input', function () { - assert.throws(function () { + it('Missing input', () => { + assert.throws(() => { sharp().boolean(); }); }); diff --git a/test/unit/clahe.js b/test/unit/clahe.js new file mode 100644 index 000000000..f3d68ebbb --- /dev/null +++ b/test/unit/clahe.js @@ -0,0 +1,143 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../lib'); +const fixtures = require('../fixtures'); + +describe('Clahe', () => { + it('width 5 width 5 maxSlope 0', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 5, height: 5, maxSlope: 0 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-5-5-0.jpg'), data, { threshold: 10 }, done); + }); + }); + + it('width 5 width 5 maxSlope 5', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 5, height: 5, maxSlope: 5 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-5-5-5.jpg'), data, done); + }); + }); + + it('width 11 width 25 maxSlope 14', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 11, height: 25, maxSlope: 14 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-11-25-14.jpg'), data, done); + }); + }); + + it('width 50 width 50 maxSlope 0', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 50, height: 50, maxSlope: 0 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-50-50-0.jpg'), data, done); + }); + }); + + it('width 50 width 50 maxSlope 14', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 50, height: 50, maxSlope: 14 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-50-50-14.jpg'), data, done); + }); + }); + + it('width 100 width 50 maxSlope 3', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 100, height: 50, maxSlope: 3 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-100-50-3.jpg'), data, done); + }); + }); + + it('width 100 width 100 maxSlope 0', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 100, height: 100, maxSlope: 0 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-100-100-0.jpg'), data, done); + }); + }); + + it('invalid maxSlope', () => { + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: -5 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 110 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 5.5 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 'a string' }); + }); + }); + + it('invalid width', () => { + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100.5, height: 100 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: -5, height: 100 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: true, height: 100 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 'string test', height: 100 }); + }); + }); + + it('invalid height', () => { + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100.5 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: -5 }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: true }); + }); + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 'string test' }); + }); + }); + + it('invalid options object', () => { + assert.throws(() => { + sharp(fixtures.inputJpgClahe).clahe(100, 100, 5); + }); + }); + + it('uses default maxSlope of 3', (_t, done) => { + sharp(fixtures.inputJpgClahe) + .clahe({ width: 100, height: 50 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + fixtures.assertSimilar(fixtures.expected('clahe-100-50-3.jpg'), data, done); + }); + }); +}); diff --git a/test/unit/clone.js b/test/unit/clone.js index 922ea48e1..dae53b20b 100644 --- a/test/unit/clone.js +++ b/test/unit/clone.js @@ -1,26 +1,30 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { afterEach, beforeEach, describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Clone', function () { - beforeEach(function () { +describe('Clone', () => { + beforeEach(() => { sharp.cache(false); }); - afterEach(function () { + afterEach(() => { sharp.cache(true); }); - it('Read from Stream and write to multiple Streams', function (done) { + it('Read from Stream and write to multiple Streams', (_t, done) => { let finishEventsExpected = 2; // Output stream 1 const output1 = fixtures.path('output.multi-stream.1.jpg'); const writable1 = fs.createWriteStream(output1); - writable1.on('finish', function () { - sharp(output1).toBuffer(function (err, data, info) { + writable1.on('finish', () => { + sharp(output1).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); @@ -37,8 +41,8 @@ describe('Clone', function () { // Output stream 2 const output2 = fixtures.path('output.multi-stream.2.jpg'); const writable2 = fs.createWriteStream(output2); - writable2.on('finish', function () { - sharp(output2).toBuffer(function (err, data, info) { + writable2.on('finish', () => { + sharp(output2).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); @@ -61,17 +65,39 @@ describe('Clone', function () { fs.createReadStream(fixtures.inputJpg).pipe(rotator); }); - it('Stream-based input attaches finish event listener to original', function () { + it('Stream-based input attaches finish event listener to original', () => { const original = sharp(); const clone = original.clone(); assert.strictEqual(1, original.listenerCount('finish')); assert.strictEqual(0, clone.listenerCount('finish')); }); - it('Non Stream-based input does not attach finish event listeners', function () { + it('Non Stream-based input does not attach finish event listeners', () => { const original = sharp(fixtures.inputJpg); const clone = original.clone(); assert.strictEqual(0, original.listenerCount('finish')); assert.strictEqual(0, clone.listenerCount('finish')); }); + + it('Ensure deep clone of properties, including arrays', async () => { + const alpha = await sharp({ + create: { width: 320, height: 240, channels: 3, background: 'red' } + }).toColourspace('b-w').png().toBuffer(); + + const original = sharp(); + const joiner = original.clone().joinChannel(alpha); + const negater = original.clone().negate(); + + fs.createReadStream(fixtures.inputJpg320x240).pipe(original); + const joined = await joiner.png({ effort: 1 }).toBuffer(); + const negated = await negater.png({ effort: 1 }).toBuffer(); + + const joinedMetadata = await sharp(joined).metadata(); + assert.strictEqual(joinedMetadata.channels, 4); + assert.strictEqual(joinedMetadata.hasAlpha, true); + + const negatedMetadata = await sharp(negated).metadata(); + assert.strictEqual(negatedMetadata.channels, 3); + assert.strictEqual(negatedMetadata.hasAlpha, false); + }); }); diff --git a/test/unit/colourspace.js b/test/unit/colourspace.js index 60ce426ce..9c908051c 100644 --- a/test/unit/colourspace.js +++ b/test/unit/colourspace.js @@ -1,19 +1,23 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Colour space conversion', function () { - it('To greyscale', function (done) { +describe('Colour space conversion', () => { + it('To greyscale', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .greyscale() .toFile(fixtures.path('output.greyscale-gamma-0.0.jpg'), done); }); - it('To greyscale with gamma correction', function (done) { + it('To greyscale with gamma correction', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .gamma() @@ -21,19 +25,19 @@ describe('Colour space conversion', function () { .toFile(fixtures.path('output.greyscale-gamma-2.2.jpg'), done); }); - it('Not to greyscale', function (done) { + it('Not to greyscale', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .greyscale(false) .toFile(fixtures.path('output.greyscale-not.jpg'), done); }); - it('Greyscale with single channel output', function (done) { + it('Greyscale with single channel output', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .greyscale() .toColourspace('b-w') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(1, info.channels); assert.strictEqual(320, info.width); @@ -52,10 +56,10 @@ describe('Colour space conversion', function () { assert.strictEqual(format, 'webp'); }); - it('From CMYK to sRGB', function (done) { + it('From CMYK to sRGB', (_t, done) => { sharp(fixtures.inputJpgWithCmykProfile) .resize(320) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -64,13 +68,13 @@ describe('Colour space conversion', function () { }); }); - it('From CMYK to sRGB with white background, not yellow', function (done) { + it('From CMYK to sRGB with white background, not yellow', (_t, done) => { sharp(fixtures.inputJpgWithCmykProfile) .resize(320, 240, { fit: sharp.fit.contain, background: 'white' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -79,10 +83,10 @@ describe('Colour space conversion', function () { }); }); - it('From profile-less CMYK to sRGB', function (done) { + it('From profile-less CMYK to sRGB', (_t, done) => { sharp(fixtures.inputJpgWithCmykNoProfile) .resize(320) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -90,8 +94,99 @@ describe('Colour space conversion', function () { }); }); - it('Invalid input', function () { - assert.throws(function () { + it('Profile-less CMYK roundtrip', async () => { + const [c, m, y, k] = await sharp(fixtures.inputJpgWithCmykNoProfile) + .pipelineColourspace('cmyk') + .toColourspace('cmyk') + .raw() + .toBuffer(); + + assert.deepStrictEqual( + { c, m, y, k }, + { c: 55, m: 27, y: 0, k: 0 } + ); + }); + + it('CMYK profile to CMYK profile conversion using perceptual intent', async () => { + const data = await sharp(fixtures.inputTiffFogra) + .resize(320, 240) + .toColourspace('cmyk') + .pipelineColourspace('cmyk') + .withIccProfile(fixtures.path('XCMYK 2017.icc')) + .raw() + .toBuffer(); + + const [c, m, y, k] = data; + assert.deepStrictEqual( + { c, m, y, k }, + { c: 1, m: 239, y: 227, k: 5 } + ); + }); + + it('CMYK profile to CMYK profile with negate', (_t, done) => { + sharp(fixtures.inputTiffFogra) + .resize(320, 240) + .toColourspace('cmyk') + .pipelineColourspace('cmyk') + .withIccProfile(fixtures.path('XCMYK 2017.icc')) + .negate() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('tiff', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar( + fixtures.expected('colourspace.cmyk-to-cmyk-negated.tif'), + data, + { threshold: 0 }, + done + ); + }); + }); + + it('From sRGB with RGB16 pipeline, resize with gamma, to sRGB', (_t, done) => { + sharp(fixtures.inputPngGradients) + .pipelineColourspace('rgb16') + .resize(320) + .gamma() + .toColourspace('srgb') + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(320, info.width); + fixtures.assertSimilar(fixtures.expected('colourspace-gradients-gamma-resize.png'), data, { + threshold: 0 + }, done); + }); + }); + + it('Convert P3 to sRGB', async () => { + const [r, g, b] = await sharp(fixtures.inputPngP3) + .raw() + .toBuffer(); + assert.strictEqual(r, 255); + assert.strictEqual(g, 0); + assert.strictEqual(b, 0); + }); + + it('Passthrough P3', async () => { + const [r, g, b] = await sharp(fixtures.inputPngP3) + .withMetadata({ icc: 'p3' }) + .raw() + .toBuffer(); + assert.strictEqual(r, 234); + assert.strictEqual(g, 51); + assert.strictEqual(b, 34); + }); + + it('Invalid pipelineColourspace input', () => { + assert.throws(() => { + sharp(fixtures.inputJpg) + .pipelineColorspace(null); + }, /Expected string for colourspace but received null of type object/); + }); + + it('Invalid toColourspace input', () => { + assert.throws(() => { sharp(fixtures.inputJpg) .toColourspace(null); }); diff --git a/test/unit/composite.js b/test/unit/composite.js index 389e6e629..0df0007aa 100644 --- a/test/unit/composite.js +++ b/test/unit/composite.js @@ -1,6 +1,10 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const fixtures = require('../fixtures'); const sharp = require('../../'); @@ -45,22 +49,20 @@ const blends = [ // Test describe('composite', () => { - it('blend', () => Promise.all( - blends.map(blend => { + blends.forEach(blend => { + it(`blend ${blend}`, async () => { const filename = `composite.blend.${blend}.png`; const actual = fixtures.path(`output.${filename}`); const expected = fixtures.expected(filename); - return sharp(redRect) + await sharp(redRect) .composite([{ input: blueRect, blend }]) - .toFile(actual) - .then(() => { - fixtures.assertMaxColourDistance(actual, expected); - }); - }) - )); + .toFile(actual); + fixtures.assertMaxColourDistance(actual, expected); + }); + }); it('premultiplied true', () => { const filename = 'composite.premultiplied.png'; @@ -121,11 +123,31 @@ describe('composite', () => { }); }); - it('multiple', () => { + it('scrgb pipeline', () => { + const filename = 'composite-red-scrgb.png'; + const actual = fixtures.path(`output.${filename}`); + const expected = fixtures.expected(filename); + return sharp({ + create: { + width: 32, height: 32, channels: 4, background: red + } + }) + .pipelineColourspace('scrgb') + .composite([{ + input: fixtures.inputPngWithTransparency16bit, + blend: 'color-burn' + }]) + .toFile(actual) + .then(() => { + fixtures.assertMaxColourDistance(actual, expected); + }); + }); + + it('multiple', async () => { const filename = 'composite-multiple.png'; const actual = fixtures.path(`output.${filename}`); const expected = fixtures.expected(filename); - return sharp(redRect) + await sharp(redRect) .composite([{ input: blueRect, gravity: 'northeast' @@ -133,10 +155,24 @@ describe('composite', () => { input: greenRect, gravity: 'southwest' }]) - .toFile(actual) - .then(() => { - fixtures.assertMaxColourDistance(actual, expected); - }); + .toFile(actual); + fixtures.assertMaxColourDistance(actual, expected); + }); + + it('autoOrient', async () => { + const data = await sharp({ + create: { + width: 600, height: 600, channels: 4, background: { ...red, alpha: 1 } + } + }) + .composite([{ + input: fixtures.inputJpgWithExif, + autoOrient: true + }]) + .jpeg() + .toBuffer(); + + await fixtures.assertSimilar(fixtures.expected('composite-autoOrient.jpg'), data); }); it('zero offset', done => { @@ -247,7 +283,7 @@ describe('composite', () => { sharp(fixtures.inputJpg) .resize(300, 300) .composite([{ - input: Buffer.from('<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'), + input: Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'), density: 96, blend: 'dest-in', cutout: true @@ -270,7 +306,7 @@ describe('composite', () => { .resize(80) .composite([{ input: fixtures.inputPngWithTransparency16bit, - gravity: gravity + gravity }]) .toBuffer((err, data, info) => { if (err) throw err; @@ -287,7 +323,7 @@ describe('composite', () => { describe('string gravity', () => { Object.keys(sharp.gravity).forEach(gravity => { it(gravity, done => { - const expected = fixtures.expected('overlay-gravity-' + gravity + '.jpg'); + const expected = fixtures.expected(`overlay-gravity-${gravity}.jpg`); sharp(fixtures.inputJpg) .resize(80) .composite([{ @@ -309,13 +345,13 @@ describe('composite', () => { describe('tile and gravity', () => { Object.keys(sharp.gravity).forEach(gravity => { it(gravity, done => { - const expected = fixtures.expected('overlay-tile-gravity-' + gravity + '.jpg'); + const expected = fixtures.expected(`overlay-tile-gravity-${gravity}.jpg`); sharp(fixtures.inputJpg) .resize(80) .composite([{ input: fixtures.inputPngWithTransparency16bit, tile: true, - gravity: gravity + gravity }]) .toBuffer((err, data, info) => { if (err) throw err; @@ -424,4 +460,77 @@ describe('composite', () => { assert.deepStrictEqual(red, { r, g, b }); }); + + it('Ensure tiled composition works with resized fit=outside', async () => { + const { info } = await sharp({ + create: { + width: 41, height: 41, channels: 3, background: 'red' + } + }) + .resize({ + width: 10, + height: 40, + fit: 'outside' + }) + .composite([ + { + input: { + create: { + width: 16, height: 16, channels: 3, background: 'green' + } + }, + tile: true + } + ]) + .toBuffer({ resolveWithObject: true }); + + assert.strictEqual(info.width, 40); + assert.strictEqual(info.height, 40); + }); + + it('Ensure implicit unpremultiply after resize but before composite', async () => { + const [r, g, b, a] = await sharp({ + create: { + width: 1, height: 1, channels: 4, background: 'saddlebrown' + } + }) + .resize({ width: 8 }) + .composite([{ + input: Buffer.from([255, 255, 255, 128]), + raw: { width: 1, height: 1, channels: 4 }, + tile: true, + blend: 'dest-in' + }]) + .raw() + .toBuffer(); + + assert.strictEqual(r, 139); + assert.strictEqual(g, 69); + assert.strictEqual(b, 19); + assert.strictEqual(a, 128); + }); + + it('Ensure tiled overlay is fully decoded', async () => { + const tile = await sharp({ + create: { + width: 8, height: 513, channels: 3, background: 'red' + } + }) + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { info } = await sharp({ + create: { + width: 8, height: 514, channels: 3, background: 'green' + } + }) + .composite([{ + input: tile, + tile: true + }]) + .toBuffer({ resolveWithObject: true }); + + assert.strictEqual(info.width, 8); + assert.strictEqual(info.height, 514); + }); }); diff --git a/test/unit/convolve.js b/test/unit/convolve.js index 2d4155490..bc430178a 100644 --- a/test/unit/convolve.js +++ b/test/unit/convolve.js @@ -1,12 +1,16 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Convolve', function () { - it('specific convolution kernel 1', function (done) { +describe('Convolve', () => { + it('specific convolution kernel 1', (_t, done) => { sharp(fixtures.inputPngStripesV) .convolve({ width: 3, @@ -19,7 +23,7 @@ describe('Convolve', function () { 10, 20, 10 ] }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -28,7 +32,7 @@ describe('Convolve', function () { }); }); - it('specific convolution kernel 2', function (done) { + it('specific convolution kernel 2', (_t, done) => { sharp(fixtures.inputPngStripesH) .convolve({ width: 3, @@ -39,7 +43,7 @@ describe('Convolve', function () { 1, 0, 1 ] }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -48,7 +52,7 @@ describe('Convolve', function () { }); }); - it('horizontal Sobel operator', function (done) { + it('horizontal Sobel operator', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .convolve({ @@ -60,7 +64,7 @@ describe('Convolve', function () { -1, 0, 1 ] }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -69,14 +73,14 @@ describe('Convolve', function () { }); }); - describe('invalid kernel specification', function () { - it('missing', function () { - assert.throws(function () { + describe('invalid kernel specification', () => { + it('missing', () => { + assert.throws(() => { sharp(fixtures.inputJpg).convolve({}); }); }); - it('incorrect data format', function () { - assert.throws(function () { + it('incorrect data format', () => { + assert.throws(() => { sharp(fixtures.inputJpg).convolve({ width: 3, height: 3, @@ -84,8 +88,8 @@ describe('Convolve', function () { }); }); }); - it('incorrect dimensions', function () { - assert.throws(function () { + it('incorrect dimensions', () => { + assert.throws(() => { sharp(fixtures.inputJpg).convolve({ width: 3, height: 4, diff --git a/test/unit/dilate.js b/test/unit/dilate.js new file mode 100644 index 000000000..f1f982cb6 --- /dev/null +++ b/test/unit/dilate.js @@ -0,0 +1,37 @@ +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('Dilate', () => { + it('dilate 1 png', (_t, done) => { + sharp(fixtures.inputPngDotAndLines) + .dilate(1) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(100, info.width); + assert.strictEqual(100, info.height); + fixtures.assertSimilar(fixtures.expected('dilate-1.png'), data, done); + }); + }); + + it('dilate 1 png - default width', (_t, done) => { + sharp(fixtures.inputPngDotAndLines) + .dilate() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(100, info.width); + assert.strictEqual(100, info.height); + fixtures.assertSimilar(fixtures.expected('dilate-1.png'), data, done); + }); + }); + + it('invalid dilation width', () => { + assert.throws(() => { + sharp(fixtures.inputJpg).dilate(-1); + }); + }); +}); diff --git a/test/unit/erode.js b/test/unit/erode.js new file mode 100644 index 000000000..0afa874df --- /dev/null +++ b/test/unit/erode.js @@ -0,0 +1,37 @@ +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('Erode', () => { + it('erode 1 png', (_t, done) => { + sharp(fixtures.inputPngDotAndLines) + .erode(1) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(100, info.width); + assert.strictEqual(100, info.height); + fixtures.assertSimilar(fixtures.expected('erode-1.png'), data, done); + }); + }); + + it('erode 1 png - default width', (_t, done) => { + sharp(fixtures.inputPngDotAndLines) + .erode() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(100, info.width); + assert.strictEqual(100, info.height); + fixtures.assertSimilar(fixtures.expected('erode-1.png'), data, done); + }); + }); + + it('invalid erosion width', () => { + assert.throws(() => { + sharp(fixtures.inputJpg).erode(-1); + }); + }); +}); diff --git a/test/unit/extend.js b/test/unit/extend.js index 8ab6d9073..f9d91112a 100644 --- a/test/unit/extend.js +++ b/test/unit/extend.js @@ -1,65 +1,159 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Extend', function () { - it('extend all sides equally via a single value', function (done) { - sharp(fixtures.inputJpg) - .resize(120) - .extend(10) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(140, info.width); - assert.strictEqual(118, info.height); - fixtures.assertSimilar(fixtures.expected('extend-equal-single.jpg'), data, done); - }); +describe('Extend', () => { + describe('extend all sides equally via a single value', () => { + it('JPEG', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(120) + .extend(10) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(140, info.width); + assert.strictEqual(118, info.height); + fixtures.assertSimilar(fixtures.expected('extend-equal-single.jpg'), data, done); + }); + }); + + it('Animated WebP', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(120) + .extend(10) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(140, info.width); + assert.strictEqual(140 * 9, info.height); + fixtures.assertSimilar(fixtures.expected('extend-equal-single.webp'), data, done); + }); + }); }); - it('extend all sides equally with RGB', function (done) { - sharp(fixtures.inputJpg) - .resize(120) + ['background', 'copy', 'mirror', 'repeat'].forEach(extendWith => { + it(`extends all sides with animated WebP (${extendWith})`, (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(120) + .extend({ + extendWith, + top: 40, + bottom: 40, + left: 40, + right: 40 + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(200, info.width); + assert.strictEqual(200 * 9, info.height); + fixtures.assertSimilar(fixtures.expected(`extend-equal-${extendWith}.webp`), data, done); + }); + }); + + it(`extend all sides equally with RGB (${extendWith})`, (_t, done) => { + sharp(fixtures.inputJpg) + .resize(120) + .extend({ + extendWith, + top: 10, + bottom: 10, + left: 10, + right: 10, + background: { r: 255, g: 0, b: 0 } + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(140, info.width); + assert.strictEqual(118, info.height); + fixtures.assertSimilar(fixtures.expected(`extend-equal-${extendWith}.jpg`), data, done); + }); + }); + + it(`extend sides unequally with RGBA (${extendWith})`, (_t, done) => { + sharp(fixtures.inputPngWithTransparency16bit) + .resize(120) + .extend({ + extendWith, + top: 50, + left: 10, + right: 35, + background: { r: 0, g: 0, b: 0, alpha: 0 } + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(165, info.width); + assert.strictEqual(170, info.height); + fixtures.assertSimilar(fixtures.expected(`extend-unequal-${extendWith}.png`), data, done); + }); + }); + + it(`PNG with 2 channels (${extendWith})`, (_t, done) => { + sharp(fixtures.inputPngWithGreyAlpha) + .extend({ + extendWith, + top: 50, + bottom: 50, + left: 80, + right: 80, + background: 'transparent' + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('png', info.format); + assert.strictEqual(560, info.width); + assert.strictEqual(400, info.height); + assert.strictEqual(4, info.channels); + fixtures.assertSimilar(fixtures.expected(`extend-2channel-${extendWith}.png`), data, done); + }); + }); + }); + + it('extend top with mirroring uses ordered read', async () => { + const data = await sharp(fixtures.inputJpg) .extend({ - top: 10, - bottom: 10, - left: 10, - right: 10, - background: { r: 255, g: 0, b: 0 } + extendWith: 'mirror', + top: 1 }) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(140, info.width); - assert.strictEqual(118, info.height); - fixtures.assertSimilar(fixtures.expected('extend-equal.jpg'), data, done); - }); + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(2725, width); + assert.strictEqual(2226, height); }); - it('extend sides unequally with RGBA', function (done) { - sharp(fixtures.inputPngWithTransparency16bit) - .resize(120) + it('multi-page extend uses ordered read', async () => { + const multiPageTiff = await sharp(fixtures.inputGifAnimated, { animated: true }) + .resize({ width: 8, height: 48 }) + .tiff() + .toBuffer(); + + const data = await sharp(multiPageTiff, { pages: -1 }) .extend({ - top: 50, - left: 10, - right: 35, - background: { r: 0, g: 0, b: 0, alpha: 0 } + background: 'red', + top: 1 }) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(165, info.width); - assert.strictEqual(170, info.height); - fixtures.assertSimilar(fixtures.expected('extend-unequal.png'), data, done); - }); + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(8, width); + assert.strictEqual(1470, height); }); - it('missing parameter fails', function () { - assert.throws(function () { + it('missing parameter fails', () => { + assert.throws(() => { sharp().extend(); }); }); - it('negative fails', function () { - assert.throws(function () { + it('negative fails', () => { + assert.throws(() => { sharp().extend(-1); }); }); @@ -87,11 +181,17 @@ describe('Extend', function () { /Expected positive integer for right but received \[object Object\] of type object/ ); }); + it('invalid extendWith fails', () => { + assert.throws( + () => sharp().extend({ extendWith: 'invalid-value' }), + /Expected one of: background, copy, repeat, mirror for extendWith but received invalid-value of type string/ + ); + }); it('can set all edges apart from right', () => { assert.doesNotThrow(() => sharp().extend({ top: 1, left: 2, bottom: 3 })); }); - it('should add alpha channel before extending with a transparent Background', function (done) { + it('should add alpha channel before extending with a transparent Background', (_t, done) => { sharp(fixtures.inputJpgWithLandscapeExif1) .extend({ bottom: 10, @@ -99,7 +199,7 @@ describe('Extend', function () { background: { r: 0, g: 0, b: 0, alpha: 0 } }) .toFormat(sharp.format.png) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(610, info.width); assert.strictEqual(460, info.height); @@ -107,21 +207,25 @@ describe('Extend', function () { }); }); - it('PNG with 2 channels', function (done) { - sharp(fixtures.inputPngWithGreyAlpha) + it('Premultiply background when compositing', async () => { + const background = { r: 191, g: 25, b: 66, alpha: 0.8 }; + const data = await sharp({ + create: { + width: 1, height: 1, channels: 4, background: '#fff0' + } + }) + .composite([{ + input: { + create: { + width: 1, height: 1, channels: 4, background + } + } + }]) .extend({ - bottom: 20, - right: 20, - background: 'transparent' + left: 1, background }) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(true, data.length > 0); - assert.strictEqual('png', info.format); - assert.strictEqual(420, info.width); - assert.strictEqual(320, info.height); - assert.strictEqual(4, info.channels); - fixtures.assertSimilar(fixtures.expected('extend-2channel.png'), data, done); - }); + .raw() + .toBuffer(); + assert.deepStrictEqual(Array.from(data), [191, 25, 66, 204, 191, 25, 66, 204]); }); }); diff --git a/test/unit/extract.js b/test/unit/extract.js index 05d67bf1d..4fee38f08 100644 --- a/test/unit/extract.js +++ b/test/unit/extract.js @@ -1,15 +1,19 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Partial image extraction', function () { - it('JPEG', function (done) { +describe('Partial image extraction', () => { + it('JPEG', (_t, done) => { sharp(fixtures.inputJpg) .extract({ left: 2, top: 2, width: 20, height: 20 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(20, info.width); assert.strictEqual(20, info.height); @@ -17,10 +21,10 @@ describe('Partial image extraction', function () { }); }); - it('PNG', function (done) { + it('PNG', (_t, done) => { sharp(fixtures.inputPng) .extract({ left: 200, top: 300, width: 400, height: 200 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(400, info.width); assert.strictEqual(200, info.height); @@ -28,10 +32,10 @@ describe('Partial image extraction', function () { }); }); - it('WebP', function (done) { + it('WebP', (_t, done) => { sharp(fixtures.inputWebP) .extract({ left: 100, top: 50, width: 125, height: 200 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(125, info.width); assert.strictEqual(200, info.height); @@ -39,11 +43,36 @@ describe('Partial image extraction', function () { }); }); - it('TIFF', function (done) { + describe('Animated WebP', () => { + it('Before resize', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .extract({ left: 0, top: 30, width: 80, height: 20 }) + .resize(320, 80) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(320, info.width); + assert.strictEqual(80 * 9, info.height); + fixtures.assertSimilar(fixtures.expected('gravity-center-height.webp'), data, done); + }); + }); + + it('After resize', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(320, 320) + .extract({ left: 0, top: 120, width: 320, height: 80 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(320, info.width); + assert.strictEqual(80 * 9, info.height); + fixtures.assertSimilar(fixtures.expected('gravity-center-height.webp'), data, done); + }); + }); + }); + + it('TIFF', (_t, done) => { sharp(fixtures.inputTiff) .extract({ left: 34, top: 63, width: 341, height: 529 }) - .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(341, info.width); assert.strictEqual(529, info.height); @@ -51,11 +80,11 @@ describe('Partial image extraction', function () { }); }); - it('Before resize', function (done) { + it('Before resize', (_t, done) => { sharp(fixtures.inputJpg) .extract({ left: 10, top: 10, width: 10, height: 500 }) .resize(100, 100) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(100, info.width); assert.strictEqual(100, info.height); @@ -63,13 +92,13 @@ describe('Partial image extraction', function () { }); }); - it('After resize and crop', function (done) { + it('After resize and crop', (_t, done) => { sharp(fixtures.inputJpg) .resize(500, 500, { position: sharp.gravity.north }) .extract({ left: 10, top: 10, width: 100, height: 100 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(100, info.width); assert.strictEqual(100, info.height); @@ -77,14 +106,14 @@ describe('Partial image extraction', function () { }); }); - it('Before and after resize and crop', function (done) { + it('Before and after resize and crop', (_t, done) => { sharp(fixtures.inputJpg) .extract({ left: 0, top: 0, width: 700, height: 700 }) .resize(500, 500, { position: sharp.gravity.north }) .extract({ left: 10, top: 10, width: 100, height: 100 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(100, info.width); assert.strictEqual(100, info.height); @@ -92,12 +121,12 @@ describe('Partial image extraction', function () { }); }); - it('Extract then rotate', function (done) { + it('Extract then rotate', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .extract({ left: 20, top: 10, width: 380, height: 280 }) .rotate(90) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(280, info.width); assert.strictEqual(380, info.height); @@ -105,24 +134,37 @@ describe('Partial image extraction', function () { }); }); - it('Rotate then extract', function (done) { + it('Rotate then extract', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .rotate(90) .extract({ left: 20, top: 10, width: 280, height: 380 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(280, info.width); assert.strictEqual(380, info.height); - fixtures.assertSimilar(fixtures.expected('rotate-extract.jpg'), data, { threshold: 7 }, done); + fixtures.assertSimilar(fixtures.expected('rotate-extract.jpg'), data, done); }); }); - it('Extract then rotate non-90 anagle', function (done) { + it('Extract then rotate then extract', (_t, done) => { + sharp(fixtures.inputPngWithGreyAlpha) + .extract({ left: 20, top: 10, width: 180, height: 280 }) + .rotate(90) + .extract({ left: 20, top: 10, width: 200, height: 100 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(200, info.width); + assert.strictEqual(100, info.height); + fixtures.assertSimilar(fixtures.expected('extract-rotate-extract.jpg'), data, done); + }); + }); + + it('Extract then rotate non-90 anagle', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .extract({ left: 20, top: 10, width: 380, height: 280 }) .rotate(45) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(467, info.width); assert.strictEqual(467, info.height); @@ -130,84 +172,164 @@ describe('Partial image extraction', function () { }); }); - it('Rotate then extract non-90 angle', function (done) { + it('Rotate then extract non-90 angle', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .rotate(45) .extract({ left: 20, top: 10, width: 380, height: 280 }) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(380, info.width); assert.strictEqual(280, info.height); - fixtures.assertSimilar(fixtures.expected('rotate-extract-45.jpg'), data, { threshold: 7 }, done); + fixtures.assertSimilar(fixtures.expected('rotate-extract-45.jpg'), data, done); + }); + }); + + describe('Apply exif orientation and mirroring then extract', () => { + [ + { + name: 'EXIF-1', + image: fixtures.inputJpgWithLandscapeExif1 + }, + { + name: 'EXIF-2', + image: fixtures.inputJpgWithLandscapeExif2 + }, + { + name: 'EXIF-3', + image: fixtures.inputJpgWithLandscapeExif3 + }, + { + name: 'EXIF-4', + image: fixtures.inputJpgWithLandscapeExif4 + }, + { + name: 'EXIF-5', + image: fixtures.inputJpgWithLandscapeExif5 + }, + { + name: 'EXIF-6', + image: fixtures.inputJpgWithLandscapeExif6 + }, + { + name: 'EXIF-7', + image: fixtures.inputJpgWithLandscapeExif7 + }, + { + name: 'EXIF-8', + image: fixtures.inputJpgWithLandscapeExif8 + } + ].forEach(({ name, image }) => { + it(name, (_t, done) => { + sharp(image) + .rotate() + .extract({ left: 0, top: 208, width: 60, height: 40 }) + .toBuffer((err, data) => { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('rotate-mirror-extract.jpg'), data, done); + }); }); + }); }); - describe('Invalid parameters', function () { - describe('using the legacy extract(top,left,width,height) syntax', function () { - it('String top', function () { - assert.throws(function () { + describe('Invalid parameters', () => { + describe('using the legacy extract(top,left,width,height) syntax', () => { + it('String top', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10); }); }); - it('Non-integral left', function () { - assert.throws(function () { + it('Non-integral left', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10); }); }); - it('Negative width - negative', function () { - assert.throws(function () { + it('Negative width - negative', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract(10, 10, -10, 10); }); }); - it('Null height', function () { - assert.throws(function () { + it('Null height', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract(10, 10, 10, null); }); }); }); - it('Undefined', function () { - assert.throws(function () { + it('Undefined', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract(); }); }); - it('String top', function () { - assert.throws(function () { + it('String top', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract({ left: 10, top: 'spoons', width: 10, height: 10 }); }); }); - it('Non-integral left', function () { - assert.throws(function () { + it('Non-integral left', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract({ left: 10.2, top: 10, width: 10, height: 10 }); }); }); - it('Negative width - negative', function () { - assert.throws(function () { + it('Negative width - negative', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: -10, height: 10 }); }); }); - it('Null height', function () { - assert.throws(function () { + it('Null height', () => { + assert.throws(() => { sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: 10, height: null }); }); }); - it('Bad image area', function (done) { + it('Bad image area', (_t, done) => { sharp(fixtures.inputJpg) .extract({ left: 3000, top: 10, width: 10, height: 10 }) - .toBuffer(function (err) { + .toBuffer((err) => { assert(err instanceof Error); - assert.strictEqual(err.message, 'extract_area: bad extract area\n'); + assert.strictEqual(err.message, 'extract_area: bad extract area'); done(); }); }); + + it('Multiple extract emits warning', () => { + let warningMessage = ''; + const s = sharp(); + s.on('warning', (msg) => { warningMessage = msg; }); + const options = { top: 0, left: 0, width: 1, height: 1 }; + s.extract(options).extract(options); + assert.strictEqual(warningMessage, ''); + s.extract(options); + assert.strictEqual(warningMessage, 'ignoring previous extract options'); + }); + + it('Multiple rotate+extract emits warning', () => { + let warningMessage = ''; + const s = sharp().rotate(); + s.on('warning', (msg) => { warningMessage = msg; }); + const options = { top: 0, left: 0, width: 1, height: 1 }; + s.extract(options).extract(options); + assert.strictEqual(warningMessage, ''); + s.extract(options); + assert.strictEqual(warningMessage, 'ignoring previous extract options'); + }); + + it('Multiple extract+resize emits warning', () => { + let warningMessage = ''; + const s = sharp(); + s.on('warning', (msg) => { warningMessage = msg; }); + const options = { top: 0, left: 0, width: 1, height: 1 }; + s.extract(options).extract(options); + assert.strictEqual(warningMessage, ''); + s.resize(1); + assert.strictEqual(warningMessage, 'operation order will be: extract, resize, extract'); + }); }); }); diff --git a/test/unit/extractChannel.js b/test/unit/extractChannel.js index 3ec17f228..761b816df 100644 --- a/test/unit/extractChannel.js +++ b/test/unit/extractChannel.js @@ -1,16 +1,20 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Image channel extraction', function () { - it('Red channel', function (done) { +describe('Image channel extraction', () => { + it('Red channel', (_t, done) => { sharp(fixtures.inputJpg) .extractChannel('red') .resize(320, 240) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -18,11 +22,11 @@ describe('Image channel extraction', function () { }); }); - it('Green channel', function (done) { + it('Green channel', (_t, done) => { sharp(fixtures.inputJpg) .extractChannel('green') .resize(320, 240) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -30,11 +34,11 @@ describe('Image channel extraction', function () { }); }); - it('Blue channel', function (done) { + it('Blue channel', (_t, done) => { sharp(fixtures.inputJpg) .extractChannel('blue') .resize(320, 240) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -42,11 +46,11 @@ describe('Image channel extraction', function () { }); }); - it('Blue channel by number', function (done) { + it('Blue channel by number', (_t, done) => { sharp(fixtures.inputJpg) .extractChannel(2) .resize(320, 240) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -54,38 +58,32 @@ describe('Image channel extraction', function () { }); }); - it('With colorspace conversion', function (done) { - const output = fixtures.path('output.extract-lch.jpg'); - sharp(fixtures.inputJpg) + it('With colorspace conversion', async () => { + const [chroma] = await sharp({ create: { width: 1, height: 1, channels: 3, background: 'red' } }) .toColourspace('lch') .extractChannel(1) - .resize(320, 240, { fastShrinkOnLoad: false }) - .toFile(output, function (err, info) { - if (err) throw err; - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); - fixtures.assertMaxColourDistance(output, fixtures.expected('extract-lch.jpg'), 9); - done(); - }); + .toBuffer(); + + assert.strictEqual(chroma, 104); }); - it('Alpha from 16-bit PNG', function (done) { - const output = fixtures.path('output.extract-alpha-16bit.jpg'); + it('Alpha from 16-bit PNG', (_t, done) => { + const output = fixtures.path('output.extract-alpha-16bit.png'); sharp(fixtures.inputPngWithTransparency16bit) + .resize(16) .extractChannel(3) - .toFile(output, function (err, info) { + .toFile(output, (err) => { if (err) throw err; - fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.jpg')); + fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.png')); done(); }); }); - it('Alpha from 2-channel input', function (done) { + it('Alpha from 2-channel input', (_t, done) => { const output = fixtures.path('output.extract-alpha-2-channel.png'); sharp(fixtures.inputPngWithGreyAlpha) .extractChannel('alpha') - .toColourspace('b-w') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(1, info.channels); fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-2-channel.png')); @@ -93,26 +91,26 @@ describe('Image channel extraction', function () { }); }); - it('Invalid channel number', function () { - assert.throws(function () { + it('Invalid channel number', () => { + assert.throws(() => { sharp(fixtures.inputJpg) .extractChannel(-1); }); }); - it('No arguments', function () { - assert.throws(function () { + it('No arguments', () => { + assert.throws(() => { sharp(fixtures.inputJpg) .extractChannel(); }); }); - it('Non-existent channel', function (done) { - sharp(fixtures.inputPng) - .extractChannel(1) - .toBuffer(function (err) { - assert(err instanceof Error); - done(); - }); - }); + it('Non-existent channel', async () => + await assert.rejects( + () => sharp({ create: { width: 1, height: 1, channels: 3, background: 'red' } }) + .extractChannel(3) + .toBuffer(), + /Cannot extract channel 3 from image with channels 0-2/ + ) + ); }); diff --git a/test/unit/failOn.js b/test/unit/failOn.js new file mode 100644 index 000000000..c4d44e892 --- /dev/null +++ b/test/unit/failOn.js @@ -0,0 +1,115 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); + +const sharp = require('../../lib'); +const fixtures = require('../fixtures'); + +describe('failOn', () => { + it('handles truncated JPEG', (_t, done) => { + sharp(fixtures.inputJpgTruncated, { failOn: 'none' }) + .resize(32, 24) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + assert.strictEqual(32, info.width); + assert.strictEqual(24, info.height); + fixtures.assertSimilar(fixtures.expected('truncated.jpg'), data, done); + }); + }); + + it('handles truncated PNG, emits warnings', (_t, done) => { + let isWarningEmitted = false; + sharp(fixtures.inputPngTruncated, { failOn: 'none' }) + .on('warning', (warning) => { + assert.ok( + ['read gave 2 warnings', 'not enough data', 'end of stream'] + .some(m => warning.includes(m))); + isWarningEmitted = true; + }) + .resize(32, 24) + .toBuffer((err, _data, info) => { + if (err) throw err; + assert.strictEqual(true, isWarningEmitted); + assert.strictEqual('png', info.format); + assert.strictEqual(32, info.width); + assert.strictEqual(24, info.height); + done(); + }); + }); + + it('throws for invalid options', () => { + assert.throws( + () => sharp({ failOn: 'zoinks' }), + /Expected one of: none, truncated, error, warning for failOn but received zoinks of type string/ + ); + assert.throws( + () => sharp({ failOn: 1 }), + /Expected one of: none, truncated, error, warning for failOn but received 1 of type number/ + ); + }); + + it('deprecated failOnError', () => { + assert.doesNotThrow( + () => sharp({ failOnError: true }) + ); + assert.doesNotThrow( + () => sharp({ failOnError: false }) + ); + assert.throws( + () => sharp({ failOnError: 'zoinks' }), + /Expected boolean for failOnError but received zoinks of type string/ + ); + assert.throws( + () => sharp({ failOnError: 1 }), + /Expected boolean for failOnError but received 1 of type number/ + ); + }); + + it('returns errors to callback for truncated JPEG', (_t, done) => { + sharp(fixtures.inputJpgTruncated, { failOn: 'truncated' }).toBuffer((err, data, info) => { + assert.ok(err.message.includes('VipsJpeg: premature end of'), err); + assert.strictEqual(data, undefined); + assert.strictEqual(info, undefined); + done(); + }); + }); + + it('returns errors to callback for truncated PNG', (_t, done) => { + sharp(fixtures.inputPngTruncated, { failOn: 'truncated' }).toBuffer((err, data, info) => { + assert.ok(err.message.includes('read error'), err); + assert.strictEqual(data, undefined); + assert.strictEqual(info, undefined); + done(); + }); + }); + + it('rejects promises for truncated JPEG', (_t, done) => { + sharp(fixtures.inputJpgTruncated, { failOn: 'error' }) + .toBuffer() + .then(() => { + throw new Error('Expected rejection'); + }) + .catch(err => { + done(err.message.includes('VipsJpeg: premature end of') ? undefined : err); + }); + }); + + it('handles stream-based input', async () => { + const writable = sharp({ failOn: 'none' }).resize(32, 24); + fs.createReadStream(fixtures.inputJpgTruncated).pipe(writable); + return writable.toBuffer(); + }); + + it('converts warnings to error for GeoTIFF', async () => { + await assert.rejects( + sharp(fixtures.inputTiffGeo).toBuffer(), + /Tag 34737/ + ); + }); +}); diff --git a/test/unit/failOnError.js b/test/unit/failOnError.js deleted file mode 100644 index 54c1f0b76..000000000 --- a/test/unit/failOnError.js +++ /dev/null @@ -1,88 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const fs = require('fs'); - -const sharp = require('../../'); -const fixtures = require('../fixtures'); - -describe('failOnError', function () { - it('handles truncated JPEG', function (done) { - sharp(fixtures.inputJpgTruncated, { failOnError: false }) - .resize(320, 240) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); - fixtures.assertSimilar(fixtures.expected('truncated.jpg'), data, done); - }); - }); - - it('handles truncated PNG, emits warnings', function (done) { - let isWarningEmitted = false; - sharp(fixtures.inputPngTruncated, { failOnError: false }) - .on('warning', function (warning) { - assert.ok(warning.includes('not enough data') || warning.includes('end of stream')); - isWarningEmitted = true; - }) - .resize(320, 240) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(true, isWarningEmitted); - assert.strictEqual('png', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); - done(); - }); - }); - - it('rejects invalid values', function () { - assert.doesNotThrow(function () { - sharp(fixtures.inputJpg, { failOnError: true }); - }); - - assert.throws(function () { - sharp(fixtures.inputJpg, { failOnError: 'zoinks' }); - }); - - assert.throws(function () { - sharp(fixtures.inputJpg, { failOnError: 1 }); - }); - }); - - 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'), err); - assert.strictEqual(data, undefined); - assert.strictEqual(info, undefined); - done(); - }); - }); - - it('returns errors to callback for truncated PNG', function (done) { - sharp(fixtures.inputPngTruncated).toBuffer(function (err, data, info) { - assert.ok(err.message.includes('read error'), err); - assert.strictEqual(data, undefined); - assert.strictEqual(info, undefined); - done(); - }); - }); - - it('rejects promises for truncated JPEG', function (done) { - sharp(fixtures.inputJpgTruncated) - .toBuffer() - .then(() => { - throw new Error('Expected rejection'); - }) - .catch(err => { - done(err.message.includes('VipsJpeg: Premature end of') ? undefined : err); - }); - }); - - it('handles stream-based input', function () { - const writable = sharp({ failOnError: false }); - fs.createReadStream(fixtures.inputJpgTruncated).pipe(writable); - return writable.toBuffer(); - }); -}); diff --git a/test/unit/fixtures.js b/test/unit/fixtures.js index 8fecf2dec..9d4583ea2 100644 --- a/test/unit/fixtures.js +++ b/test/unit/fixtures.js @@ -1,25 +1,29 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const fixtures = require('../fixtures'); -describe('Test fixtures', function () { - describe('assertMaxColourDistance', function () { - it('should throw an Error when images have a different number of channels', function () { - assert.throws(function () { +describe('Test fixtures', () => { + describe('assertMaxColourDistance', () => { + it('should throw an Error when images have a different number of channels', () => { + assert.throws(() => { fixtures.assertMaxColourDistance(fixtures.inputPngOverlayLayer1, fixtures.inputJpg); }); }); - it('should throw an Error when images have different dimensions', function () { - assert.throws(function () { + it('should throw an Error when images have different dimensions', () => { + assert.throws(() => { fixtures.assertMaxColourDistance(fixtures.inputJpg, fixtures.inputJpgWithExif); }); }); - it('should accept a zero threshold when comparing an image to itself', function () { + it('should accept a zero threshold when comparing an image to itself', () => { const image = fixtures.inputPngOverlayLayer0; fixtures.assertMaxColourDistance(image, image, 0); }); - it('should accept a numeric threshold for two different images', function () { + it('should accept a numeric threshold for two different images', () => { fixtures.assertMaxColourDistance(fixtures.inputPngOverlayLayer0, fixtures.inputPngOverlayLayer1, 100); }); }); diff --git a/test/unit/gamma.js b/test/unit/gamma.js index da8feea93..2e0b3fcde 100644 --- a/test/unit/gamma.js +++ b/test/unit/gamma.js @@ -1,15 +1,19 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Gamma correction', function () { - it('value of 0.0 (disabled)', function (done) { +describe('Gamma correction', () => { + it('value of 0.0 (disabled)', (_t, done) => { sharp(fixtures.inputJpgWithGammaHoliness) .resize(129, 111) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(129, info.width); @@ -18,11 +22,11 @@ describe('Gamma correction', function () { }); }); - it('value of 2.2 (default)', function (done) { + it('value of 2.2 (default)', (_t, done) => { sharp(fixtures.inputJpgWithGammaHoliness) .resize(129, 111) .gamma() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(129, info.width); @@ -31,11 +35,11 @@ describe('Gamma correction', function () { }); }); - it('value of 3.0', function (done) { + it('value of 3.0', (_t, done) => { sharp(fixtures.inputJpgWithGammaHoliness) .resize(129, 111) .gamma(3) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(129, info.width); @@ -44,11 +48,11 @@ describe('Gamma correction', function () { }); }); - it('input value of 2.2, output value of 3.0', function (done) { + it('input value of 2.2, output value of 3.0', (_t, done) => { sharp(fixtures.inputJpgWithGammaHoliness) .resize(129, 111) .gamma(2.2, 3.0) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(129, info.width); @@ -57,12 +61,12 @@ describe('Gamma correction', function () { }); }); - it('alpha transparency', function (done) { + it('alpha transparency', (_t, done) => { sharp(fixtures.inputPngOverlayLayer1) .resize(320) .gamma() .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -70,14 +74,14 @@ describe('Gamma correction', function () { }); }); - it('invalid first parameter value', function () { - assert.throws(function () { + it('invalid first parameter value', () => { + assert.throws(() => { sharp(fixtures.inputJpgWithGammaHoliness).gamma(4); }); }); - it('invalid second parameter value', function () { - assert.throws(function () { + it('invalid second parameter value', () => { + assert.throws(() => { sharp(fixtures.inputJpgWithGammaHoliness).gamma(2.2, 4); }); }); diff --git a/test/unit/gif.js b/test/unit/gif.js index ff59be28f..4b7d4a76a 100644 --- a/test/unit/gif.js +++ b/test/unit/gif.js @@ -1,7 +1,11 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); @@ -36,59 +40,91 @@ describe('GIF input', () => { }) ); - it('Animated GIF first page to PNG', () => + it('Animated GIF first page to non-animated GIF', () => sharp(fixtures.inputGifAnimated) .toBuffer({ resolveWithObject: true }) .then(({ data, info }) => { assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); - assert.strictEqual(sharp.format.magick.input.buffer ? 'gif' : 'png', info.format); + assert.strictEqual('gif', info.format); assert.strictEqual(80, info.width); assert.strictEqual(80, info.height); assert.strictEqual(4, info.channels); + assert.strictEqual(undefined, info.pages); + assert.strictEqual(undefined, info.pageHeight); }) ); - it('Animated GIF all pages to PNG "toilet roll"', () => + it('Animated GIF round trip', () => sharp(fixtures.inputGifAnimated, { pages: -1 }) .toBuffer({ resolveWithObject: true }) .then(({ data, info }) => { assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); - assert.strictEqual(sharp.format.magick.input.buffer ? 'gif' : 'png', info.format); + assert.strictEqual('gif', info.format); assert.strictEqual(80, info.width); assert.strictEqual(2400, info.height); assert.strictEqual(4, info.channels); + assert.strictEqual(30, info.pages); + assert.strictEqual(80, info.pageHeight); }) ); - if (!sharp.format.magick.output.buffer) { - it('GIF buffer output should fail due to missing ImageMagick', () => { - assert.throws( - () => sharp().gif(), - /GIF output requires libvips with support for ImageMagick/ - ); - }); + it('GIF with reduced colours, no dither, low effort reduces file size', async () => { + const original = await sharp(fixtures.inputJpg) + .resize(120, 80) + .gif() + .toBuffer(); - 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/ - ); - }); - } + const reduced = await sharp(fixtures.inputJpg) + .resize(120, 80) + .gif({ + colours: 128, + dither: 0, + effort: 1 + }) + .toBuffer(); - it('invalid pageHeight throws', () => { - assert.throws(() => { - sharp().gif({ pageHeight: 0 }); - }); + assert.strictEqual(true, reduced.length < original.length); + }); + + it('valid reuse', () => { + assert.doesNotThrow(() => sharp().gif({ reuse: true })); + assert.doesNotThrow(() => sharp().gif({ reuse: false })); + }); + + it('invalid reuse throws', () => { + assert.throws( + () => sharp().gif({ reuse: -1 }), + /Expected boolean for gifReuse but received -1 of type number/ + ); + assert.throws( + () => sharp().gif({ reuse: 'fail' }), + /Expected boolean for gifReuse but received fail of type string/ + ); + }); + + it('progressive changes file size', async () => { + const nonProgressive = await sharp(fixtures.inputGif).gif({ progressive: false }).toBuffer(); + const progressive = await sharp(fixtures.inputGif).gif({ progressive: true }).toBuffer(); + assert(nonProgressive.length !== progressive.length); + }); + + it('invalid progressive throws', () => { + assert.throws( + () => sharp().gif({ progressive: -1 }), + /Expected boolean for gifProgressive but received -1 of type number/ + ); + assert.throws( + () => sharp().gif({ progressive: 'fail' }), + /Expected boolean for gifProgressive but received fail of type string/ + ); }); it('invalid loop throws', () => { assert.throws(() => { sharp().gif({ loop: -1 }); }); - assert.throws(() => { sharp().gif({ loop: 65536 }); }); @@ -96,43 +132,147 @@ describe('GIF input', () => { it('invalid delay throws', () => { assert.throws(() => { - sharp().gif({ delay: [-1] }); + sharp().gif({ delay: -1 }); }); - assert.throws(() => { 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('invalid colour throws', () => { + assert.throws(() => { + sharp().gif({ colours: 1 }); + }); + assert.throws(() => { + sharp().gif({ colours: 'fail' }); + }); + }); + + it('invalid effort throws', () => { + assert.throws(() => { + sharp().gif({ effort: 0 }); + }); + assert.throws(() => { + sharp().gif({ effort: 'fail' }); + }); + }); + + it('invalid dither throws', () => { + assert.throws(() => { + sharp().gif({ dither: 1.1 }); + }); + assert.throws(() => { + sharp().gif({ effort: 'fail' }); + }); + }); + + it('invalid interFrameMaxError throws', () => { + assert.throws( + () => sharp().gif({ interFrameMaxError: 33 }), + /Expected number between 0.0 and 32.0 for interFrameMaxError but received 33 of type number/ + ); + assert.throws( + () => sharp().gif({ interFrameMaxError: 'fail' }), + /Expected number between 0.0 and 32.0 for interFrameMaxError but received fail of type string/ + ); + }); + + it('invalid interPaletteMaxError throws', () => { + assert.throws( + () => sharp().gif({ interPaletteMaxError: 257 }), + /Expected number between 0.0 and 256.0 for interPaletteMaxError but received 257 of type number/ + ); + assert.throws( + () => sharp().gif({ interPaletteMaxError: 'fail' }), + /Expected number between 0.0 and 256.0 for interPaletteMaxError but received fail of type string/ + ); + }); + + it('invalid keepDuplicateFrames throws', () => { + assert.throws( + () => sharp().gif({ keepDuplicateFrames: -1 }), + /Expected boolean for keepDuplicateFrames but received -1 of type number/ + ); + assert.throws( + () => sharp().gif({ keepDuplicateFrames: 'fail' }), + /Expected boolean for keepDuplicateFrames but received fail of type string/ + ); + }); + + it('should work with streams when only animated is set', (_t, done) => { + fs.createReadStream(fixtures.inputGifAnimated) + .pipe(sharp({ animated: true })) + .gif() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('gif', info.format); + fixtures.assertSimilar(fixtures.inputGifAnimated, data, done); + }); + }); + + it('should work with streams when only pages is set', (_t, done) => { + fs.createReadStream(fixtures.inputGifAnimated) + .pipe(sharp({ pages: -1 })) + .gif() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('gif', info.format); + fixtures.assertSimilar(fixtures.inputGifAnimated, data, done); + }); + }); + + it('should optimise file size via interFrameMaxError', async () => { + const input = sharp(fixtures.inputGifAnimated, { animated: true }); + const before = await input.gif({ interFrameMaxError: 0 }).toBuffer(); + const after = await input.gif({ interFrameMaxError: 10 }).toBuffer(); + assert.strict(before.length > after.length); + }); + + it('should optimise file size via interPaletteMaxError', async () => { + const input = sharp(fixtures.inputGifAnimated, { animated: true }); + const before = await input.gif({ interPaletteMaxError: 0 }).toBuffer(); + const after = await input.gif({ interPaletteMaxError: 100 }).toBuffer(); + assert.strict(before.length > after.length); + }); + + it('should keep duplicate frames via keepDuplicateFrames', async () => { + const create = { width: 8, height: 8, channels: 4, background: 'blue' }; + const input = sharp([{ create }, { create }], { join: { animated: true } }); + + const before = await input.gif({ keepDuplicateFrames: false }).toBuffer(); + const after = await input.gif({ keepDuplicateFrames: true }).toBuffer(); + assert.strict(before.length < after.length); + + const beforeMeta = await sharp(before).metadata(); + const afterMeta = await sharp(after).metadata(); + assert.strictEqual(beforeMeta.pages, 1); + assert.strictEqual(afterMeta.pages, 2); }); - 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(); + it('non-animated input defaults to no-loop', async () => { + for (const input of [fixtures.inputGif, fixtures.inputPng]) { + const data = await sharp(input) + .resize(8) + .gif({ effort: 1 }) + .toBuffer(); + + const { format, pages, loop } = await sharp(data).metadata(); + assert.strictEqual('gif', format); + assert.strictEqual(1, pages); + assert.strictEqual(1, loop); } }); + + it('Animated GIF to animated WebP merges identical frames', async () => { + const webp = await sharp(fixtures.inputGifAnimated, { animated: true }) + .webp() + .toBuffer(); + + const { delay, loop, pages } = await sharp(webp).metadata(); + assert.deepStrictEqual([120, 120, 90, 120, 120, 90, 120, 90, 30], delay); + assert.strictEqual(0, loop); + assert.strictEqual(9, pages); + }); }); diff --git a/test/unit/heif.js b/test/unit/heif.js index 3838215f1..adb7b2969 100644 --- a/test/unit/heif.js +++ b/test/unit/heif.js @@ -1,38 +1,42 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); describe('HEIF', () => { - it('called without options does not throw an error', () => { - assert.doesNotThrow(() => { + it('called without options throws an error', () => { + assert.throws(() => { sharp().heif(); }); }); it('valid quality does not throw an error', () => { assert.doesNotThrow(() => { - sharp().heif({ quality: 80 }); + sharp().heif({ compression: 'av1', quality: 80 }); }); }); it('invalid quality should throw an error', () => { assert.throws(() => { - sharp().heif({ quality: 101 }); + sharp().heif({ compression: 'av1', quality: 101 }); }); }); it('non-numeric quality should throw an error', () => { assert.throws(() => { - sharp().heif({ quality: 'fail' }); + sharp().heif({ compression: 'av1', quality: 'fail' }); }); }); it('valid lossless does not throw an error', () => { assert.doesNotThrow(() => { - sharp().heif({ lossless: true }); + sharp().heif({ compression: 'av1', lossless: true }); }); }); it('non-boolean lossless should throw an error', () => { assert.throws(() => { - sharp().heif({ lossless: 'fail' }); + sharp().heif({ compression: 'av1', lossless: 'fail' }); }); }); it('valid compression does not throw an error', () => { @@ -50,29 +54,46 @@ describe('HEIF', () => { sharp().heif({ compression: 1 }); }); }); - it('valid speed does not throw an error', () => { + it('valid effort does not throw an error', () => { assert.doesNotThrow(() => { - sharp().heif({ speed: 6 }); + sharp().heif({ compression: 'av1', effort: 6 }); }); }); - it('out of range speed should throw an error', () => { + it('out of range effort should throw an error', () => { assert.throws(() => { - sharp().heif({ speed: 9 }); + sharp().heif({ compression: 'av1', effort: 10 }); }); }); - it('invalid speed should throw an error', () => { + it('invalid effort should throw an error', () => { assert.throws(() => { - sharp().heif({ compression: 'fail' }); + sharp().heif({ compression: 'av1', effort: 'fail' }); }); }); it('invalid chromaSubsampling should throw an error', () => { assert.throws(() => { - sharp().heif({ chromaSubsampling: 'fail' }); + sharp().heif({ compression: 'av1', chromaSubsampling: 'fail' }); }); }); it('valid chromaSubsampling does not throw an error', () => { assert.doesNotThrow(() => { - sharp().heif({ chromaSubsampling: '4:4:4' }); + sharp().heif({ compression: 'av1', chromaSubsampling: '4:4:4' }); }); }); + it('valid bitdepth value does not throw an error', () => { + const { heif } = sharp.versions; + delete sharp.versions.heif; + assert.doesNotThrow(() => { + sharp().heif({ compression: 'av1', bitdepth: 12 }); + }); + sharp.versions.heif = '1.2.3'; + assert.throws(() => { + sharp().heif({ compression: 'av1', bitdepth: 10 }); + }, /Error: Expected 8 for bitdepth when using prebuilt binaries but received 10 of type number/); + sharp.versions.heif = heif; + }); + it('invalid bitdepth value should throw an error', () => { + assert.throws(() => { + sharp().heif({ compression: 'av1', bitdepth: 11 }); + }, /Error: Expected 8, 10 or 12 for bitdepth but received 11 of type number/); + }); }); diff --git a/test/unit/io.js b/test/unit/io.js index fa8554716..ad13f48b6 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -1,71 +1,75 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); -const rimraf = require('rimraf'); +const fs = require('node:fs'); +const path = require('node:path'); +const { afterEach, beforeEach, describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); const outputJpg = fixtures.path('output.jpg'); -describe('Input/output', function () { - beforeEach(function () { +describe('Input/output', () => { + beforeEach(() => { sharp.cache(false); }); - afterEach(function () { + afterEach(() => { sharp.cache(true); }); - it('Read from File and write to Stream', function (done) { + it('Read from File and write to Stream', (_t, done) => { const writable = fs.createWriteStream(outputJpg); - writable.on('close', function () { - sharp(outputJpg).toBuffer(function (err, data, info) { + writable.on('close', () => { + sharp(outputJpg).toBuffer((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(outputJpg, done); + fs.rm(outputJpg, done); }); }); sharp(fixtures.inputJpg).resize(320, 240).pipe(writable); }); - it('Read from Buffer and write to Stream', function (done) { + it('Read from Buffer and write to Stream', (_t, done) => { const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const writable = fs.createWriteStream(outputJpg); - writable.on('close', function () { - sharp(outputJpg).toBuffer(function (err, data, info) { + writable.on('close', () => { + sharp(outputJpg).toBuffer((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(outputJpg, done); + fs.rm(outputJpg, done); }); }); sharp(inputJpgBuffer).resize(320, 240).pipe(writable); }); - it('Read from Stream and write to File', function (done) { + it('Read from Stream and write to File', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); - const pipeline = sharp().resize(320, 240).toFile(outputJpg, function (err, info) { + const pipeline = sharp().resize(320, 240).toFile(outputJpg, (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(outputJpg, done); + fs.rm(outputJpg, done); }); readable.pipe(pipeline); }); - it('Read from Stream and write to Buffer', function (done) { + it('Read from Stream and write to Buffer', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); - const pipeline = sharp().resize(320, 240).toBuffer(function (err, data, info) { + const pipeline = sharp().resize(320, 240).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); @@ -77,23 +81,23 @@ describe('Input/output', function () { readable.pipe(pipeline); }); - it('Read from Stream and write to Buffer via Promise resolved with Buffer', function () { + it('Read from Stream and write to Buffer via Promise resolved with Buffer', () => { const pipeline = sharp().resize(1, 1); fs.createReadStream(fixtures.inputJpg).pipe(pipeline); return pipeline .toBuffer({ resolveWithObject: false }) - .then(function (data) { + .then((data) => { assert.strictEqual(true, data instanceof Buffer); assert.strictEqual(true, data.length > 0); }); }); - it('Read from Stream and write to Buffer via Promise resolved with Object', function () { + it('Read from Stream and write to Buffer via Promise resolved with Object', () => { const pipeline = sharp().resize(1, 1); fs.createReadStream(fixtures.inputJpg).pipe(pipeline); return pipeline .toBuffer({ resolveWithObject: true }) - .then(function (object) { + .then((object) => { assert.strictEqual('object', typeof object); assert.strictEqual('object', typeof object.info); assert.strictEqual('jpeg', object.info.format); @@ -105,21 +109,18 @@ describe('Input/output', function () { }); }); - it('Read from File and write to Buffer via Promise resolved with Buffer', function () { - return sharp(fixtures.inputJpg) + it('Read from File and write to Buffer via Promise resolved with Buffer', () => sharp(fixtures.inputJpg) .resize(1, 1) .toBuffer({ resolveWithObject: false }) - .then(function (data) { + .then((data) => { assert.strictEqual(true, data instanceof Buffer); assert.strictEqual(true, data.length > 0); - }); - }); + })); - it('Read from File and write to Buffer via Promise resolved with Object', function () { - return sharp(fixtures.inputJpg) + it('Read from File and write to Buffer via Promise resolved with Object', () => sharp(fixtures.inputJpg) .resize(1, 1) .toBuffer({ resolveWithObject: true }) - .then(function (object) { + .then((object) => { assert.strictEqual('object', typeof object); assert.strictEqual('object', typeof object.info); assert.strictEqual('jpeg', object.info.format); @@ -128,27 +129,43 @@ describe('Input/output', function () { assert.strictEqual(3, object.info.channels); assert.strictEqual(true, object.data instanceof Buffer); assert.strictEqual(true, object.data.length > 0); - }); - }); + })); - it('Read from Stream and write to Stream', function (done) { + it('Read from Stream and write to Stream', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); const writable = fs.createWriteStream(outputJpg); - writable.on('close', function () { - sharp(outputJpg).toBuffer(function (err, data, info) { + writable.on('close', () => { + sharp(outputJpg).toBuffer((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(outputJpg, done); + fs.rm(outputJpg, done); }); }); const pipeline = sharp().resize(320, 240); readable.pipe(pipeline).pipe(writable); }); + it('Read from ArrayBuffer and write to Buffer', async () => { + const uint8array = Uint8Array.from([255, 255, 255, 0, 0, 0]); + const arrayBuffer = new ArrayBuffer(uint8array.byteLength); + new Uint8Array(arrayBuffer).set(uint8array); + const { data, info } = await sharp(arrayBuffer, { + 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 Uint8Array and write to Buffer', async () => { const uint8array = Uint8Array.from([255, 255, 255, 0, 0, 0]); const { data, info } = await sharp(uint8array, { @@ -181,73 +198,120 @@ describe('Input/output', function () { assert.strictEqual(info.height, 1); }); - it('Stream should emit info event', function (done) { + it('Read from Uint8ClampedArray with byteOffset 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([0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 255, 255]); + const uint8ArrayWithByteOffset = new Uint8ClampedArray(uint8array.buffer, 3, 6); + const { data, info } = await sharp(uint8ArrayWithByteOffset, { + raw: { + width: 2, + height: 1, + channels: 3 + } + }).toBuffer({ resolveWithObject: true }); + + assert.deepStrictEqual(Uint8ClampedArray.from([255, 255, 255, 0, 0, 0]), new Uint8ClampedArray(data)); + assert.strictEqual(info.width, 2); + assert.strictEqual(info.height, 1); + }); + + it('Stream should emit info event', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); const writable = fs.createWriteStream(outputJpg); const pipeline = sharp().resize(320, 240); let infoEventEmitted = false; - pipeline.on('info', function (info) { + pipeline.on('info', (info) => { assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); assert.strictEqual(3, info.channels); infoEventEmitted = true; }); - writable.on('close', function () { + writable.on('close', () => { assert.strictEqual(true, infoEventEmitted); - rimraf(outputJpg, done); + fs.rm(outputJpg, done); }); readable.pipe(pipeline).pipe(writable); }); - it('Handle Stream to Stream error ', function (done) { + it('Stream should emit close event', (_t, done) => { + const readable = fs.createReadStream(fixtures.inputJpg); + const writable = fs.createWriteStream(outputJpg); + const pipeline = sharp().resize(320, 240); + let closeEventEmitted = false; + pipeline.on('close', () => { + closeEventEmitted = true; + }); + writable.on('close', () => { + assert.strictEqual(true, closeEventEmitted); + fs.rm(outputJpg, done); + }); + readable.pipe(pipeline).pipe(writable); + }); + + it('Handle Stream to Stream error ', (_t, done) => { const pipeline = sharp().resize(320, 240); let anErrorWasEmitted = false; - pipeline.on('error', function (err) { + pipeline.on('error', (err) => { anErrorWasEmitted = !!err; - }).on('end', function () { + }).on('end', () => { assert(anErrorWasEmitted); - rimraf(outputJpg, done); + fs.rm(outputJpg, done); }); const readableButNotAnImage = fs.createReadStream(__filename); const writable = fs.createWriteStream(outputJpg); readableButNotAnImage.pipe(pipeline).pipe(writable); }); - it('Handle File to Stream error', function (done) { + it('Handle File to Stream error', (_t, done) => { const readableButNotAnImage = sharp(__filename).resize(320, 240); let anErrorWasEmitted = false; - readableButNotAnImage.on('error', function (err) { + readableButNotAnImage.on('error', (err) => { anErrorWasEmitted = !!err; - }).on('end', function () { + }).on('end', () => { assert(anErrorWasEmitted); - rimraf(outputJpg, done); + fs.rm(outputJpg, done); }); const writable = fs.createWriteStream(outputJpg); readableButNotAnImage.pipe(writable); }); - it('Readable side of Stream can start flowing after Writable side has finished', function (done) { + it('Readable side of Stream can start flowing after Writable side has finished', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); const writable = fs.createWriteStream(outputJpg); - writable.on('close', function () { - sharp(outputJpg).toBuffer(function (err, data, info) { + writable.on('close', () => { + sharp(outputJpg).toBuffer((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(outputJpg, done); + fs.rm(outputJpg, done); }); }); const pipeline = sharp().resize(320, 240); readable.pipe(pipeline); - pipeline.on('finish', function () { + pipeline.on('finish', () => { pipeline.pipe(writable); }); }); + it('Non-Stream input generates error when provided Stream-like data', (_t, done) => { + sharp('input')._write('fail', null, (err) => { + assert.strictEqual(err.message, 'Unexpected data on Writable Stream'); + done(); + }); + }); + + it('Non-Buffer chunk on Stream input generates error', (_t, done) => { + sharp()._write('fail', null, (err) => { + assert.strictEqual(err.message, 'Non-Buffer data on Writable Stream'); + done(); + }); + }); + it('Invalid sequential read option throws', () => { assert.throws(() => { sharp({ sequentialRead: 'fail' }); @@ -282,11 +346,11 @@ describe('Input/output', function () { }) ); - it('Support output to jpg format', function (done) { + it('Support output to jpg format', (_t, done) => { sharp(fixtures.inputPng) .resize(320, 240) .toFormat('jpg') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); @@ -297,145 +361,201 @@ describe('Input/output', function () { }); }); - it('Fail when output File is input File', function (done) { - sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) { + it('Support output to tif format', (_t, done) => { + sharp(fixtures.inputTiff) + .resize(320, 240) + .toFormat('tif') + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); + assert.strictEqual('tiff', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + done(); + }); + }); + + it('Allow use of toBuffer and toFile with same instance', async () => { + const instance = sharp({ + create: { + width: 8, + height: 8, + channels: 3, + background: 'red' + } + }); + await instance.toFile(fixtures.path('output.jpg')); + const data = await instance.toBuffer(); + assert.strictEqual(Buffer.isBuffer(data), true); + }); + + it('Fail when output File is input File', (_t, done) => { + sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, (err) => { assert(err instanceof Error); assert.strictEqual('Cannot use same file for input and output', err.message); done(); }); }); - it('Fail when output File is input File via Promise', function (done) { - sharp(fixtures.inputJpg).toFile(fixtures.inputJpg).then(function (data) { - assert(false); - done(); - }).catch(function (err) { + it('Fail when output File is input File via Promise', (_t, done) => { + sharp(fixtures.inputJpg).toFile(fixtures.inputJpg).then(() => { + done(new Error('Unexpectedly resolved Promise')); + }).catch((err) => { assert(err instanceof Error); assert.strictEqual('Cannot use same file for input and output', err.message); done(); }); }); - it('Fail when output File is empty', function (done) { - sharp(fixtures.inputJpg).toFile('', function (err) { + it('Fail when output File is input File (relative output, absolute input)', (_t, done) => { + const relativePath = path.relative(process.cwd(), fixtures.inputJpg); + sharp(fixtures.inputJpg).toFile(relativePath, (err) => { assert(err instanceof Error); - assert.strictEqual('Missing output file path', err.message); + assert.strictEqual('Cannot use same file for input and output', err.message); done(); }); }); - it('Fail when output File is empty via Promise', function (done) { - sharp(fixtures.inputJpg).toFile('').then(function (data) { - assert(false); - done(); - }).catch(function (err) { + it('Fail when output File is input File via Promise (relative output, absolute input)', (_t, done) => { + const relativePath = path.relative(process.cwd(), fixtures.inputJpg); + sharp(fixtures.inputJpg).toFile(relativePath).then(() => { + done(new Error('Unexpectedly resolved Promise')); + }).catch((err) => { assert(err instanceof Error); - assert.strictEqual('Missing output file path', err.message); + assert.strictEqual('Cannot use same file for input and output', err.message); done(); }); }); - it('Fail when input is empty Buffer', function (done) { - sharp(Buffer.alloc(0)).toBuffer().then(function () { - assert(false); - done(); - }).catch(function (err) { + it('Fail when output File is input File (relative input, absolute output)', (_t, done) => { + const relativePath = path.relative(process.cwd(), fixtures.inputJpg); + sharp(relativePath).toFile(fixtures.inputJpg, (err) => { assert(err instanceof Error); - assert.strictEqual('Input buffer contains unsupported image format', err.message); + assert.strictEqual('Cannot use same file for input and output', err.message); done(); }); }); - it('Fail when input is invalid Buffer', function (done) { - sharp(Buffer.from([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () { - assert(false); - done(); - }).catch(function (err) { + it('Fail when output File is input File via Promise (relative input, absolute output)', (_t, done) => { + const relativePath = path.relative(process.cwd(), fixtures.inputJpg); + sharp(relativePath).toFile(fixtures.inputJpg).then(() => { + done(new Error('Unexpectedly resolved Promise')); + }).catch((err) => { assert(err instanceof Error); - assert.strictEqual('Input buffer contains unsupported image format', err.message); + assert.strictEqual('Cannot use same file for input and output', err.message); done(); }); }); - it('Fail when input file path is missing', function (done) { - sharp('does-not-exist').toBuffer().then(function () { - assert(false); + it('Fail when output File is empty', (_t, done) => { + sharp(fixtures.inputJpg).toFile('', (err) => { + assert(err instanceof Error); + assert.strictEqual('Missing output file path', err.message); done(); - }).catch(function (err) { + }); + }); + + it('Fail when output File is empty via Promise', (_t, done) => { + sharp(fixtures.inputJpg).toFile('').then(() => { + done(new Error('Unexpectedly resolved Promise')); + }).catch((err) => { assert(err instanceof Error); - assert.strictEqual('Input file is missing', err.message); + assert.strictEqual('Missing output file path', err.message); done(); }); }); - describe('Fail for unsupported input', function () { - it('Undefined', function () { - assert.throws(function () { + it('Fail when input is invalid Buffer', async () => + assert.rejects( + () => sharp(Buffer.from([0x1, 0x2, 0x3, 0x4])).toBuffer(), + (err) => { + assert.strictEqual(err.message, 'Input buffer contains unsupported image format'); + assert(err.stack.includes('at Sharp.toBuffer')); + assert(err.stack.includes(__filename)); + return true; + } + ) + ); + + it('Fail when input file path is missing', async () => + assert.rejects( + () => sharp('does-not-exist').toFile('fail'), + (err) => { + assert.strictEqual(err.message, 'Input file is missing: does-not-exist'); + assert(err.stack.includes('at Sharp.toFile')); + assert(err.stack.includes(__filename)); + return true; + } + ) + ); + + describe('Fail for unsupported input', () => { + it('Undefined', () => { + assert.throws(() => { sharp(undefined); }); }); - it('Null', function () { - assert.throws(function () { + it('Null', () => { + assert.throws(() => { sharp(null); }); }); - it('Numeric', function () { - assert.throws(function () { + it('Numeric', () => { + assert.throws(() => { sharp(1); }); }); - it('Boolean', function () { - assert.throws(function () { + it('Boolean', () => { + assert.throws(() => { sharp(true); }); }); - it('Error Object', function () { - assert.throws(function () { + it('Error Object', () => { + assert.throws(() => { sharp(new Error()); }); }); }); - it('Promises/A+', function () { - return sharp(fixtures.inputJpg) + it('Promises/A+', () => sharp(fixtures.inputJpg) .resize(320, 240) - .toBuffer(); - }); + .toBuffer()); - it('Invalid output format', function (done) { + it('Invalid output format', (_t, done) => { let isValid = false; try { sharp().toFormat('zoinks'); isValid = true; - } catch (e) {} + } catch (_err) {} assert(!isValid); done(); }); - it('File input with corrupt header fails gracefully', function (done) { + it('File input with corrupt header fails gracefully', (_t, done) => { sharp(fixtures.inputJpgWithCorruptHeader) - .toBuffer(function (err) { + .toBuffer((err) => { assert.strictEqual(true, !!err); done(); }); }); - it('Buffer input with corrupt header fails gracefully', function (done) { + it('Buffer input with corrupt header fails gracefully', (_t, done) => { sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) - .toBuffer(function (err) { + .toBuffer((err) => { assert.strictEqual(true, !!err); done(); }); }); - it('Stream input with corrupt header fails gracefully', function (done) { + it('Stream input with corrupt header fails gracefully', (_t, done) => { const transformer = sharp(); transformer .toBuffer() - .then(function () { + .then(() => { done(new Error('Unexpectedly resolved Promise')); }) - .catch(function (err) { + .catch((err) => { assert.strictEqual(true, !!err); done(); }); @@ -444,94 +564,81 @@ describe('Input/output', function () { .pipe(transformer); }); - describe('Output filename with unknown extension', function () { + describe('Output filename with unknown extension', () => { const outputZoinks = fixtures.path('output.zoinks'); - it('Match JPEG input', function (done) { + it('Match JPEG input', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 80) - .toFile(outputZoinks, function (err, info) { + .toFile(outputZoinks, (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(outputZoinks, done); + fs.rm(outputZoinks, done); }); }); - it('Match PNG input', function (done) { + it('Match PNG input', (_t, done) => { sharp(fixtures.inputPng) .resize(320, 80) - .toFile(outputZoinks, function (err, info) { + .toFile(outputZoinks, (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(outputZoinks, done); + fs.rm(outputZoinks, done); }); }); - it('Match WebP input', function (done) { + it('Match WebP input', (_t, done) => { sharp(fixtures.inputWebP) .resize(320, 80) - .toFile(outputZoinks, function (err, info) { + .toFile(outputZoinks, (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(outputZoinks, done); + fs.rm(outputZoinks, done); }); }); - it('Match TIFF input', function (done) { + it('Match TIFF input', (_t, done) => { sharp(fixtures.inputTiff) .resize(320, 80) - .toFile(outputZoinks, function (err, info) { + .toFile(outputZoinks, (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(outputZoinks, done); - }); - }); - - it('Autoconvert GIF input to PNG output', function (done) { - sharp(fixtures.inputGif) - .resize(320, 80) - .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(outputZoinks, done); + fs.rm(outputZoinks, done); }); }); - it('Force JPEG format for PNG input', function (done) { + it('Force JPEG format for PNG input', (_t, done) => { sharp(fixtures.inputPng) .resize(320, 80) .jpeg() - .toFile(outputZoinks, function (err, info) { + .toFile(outputZoinks, (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(outputZoinks, done); + fs.rm(outputZoinks, done); }); }); }); - it('Input and output formats match when not forcing', function (done) { + it('Input and output formats match when not forcing', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .png({ compressionLevel: 1, force: false }) - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -540,42 +647,42 @@ describe('Input/output', function () { }); }); - it('Can force output format with output chaining', function () { - return sharp(fixtures.inputJpg) + it('Can force output format with output chaining', () => sharp(fixtures.inputJpg) .resize(320, 240) .png({ force: true }) .jpeg({ force: false }) .toBuffer({ resolveWithObject: true }) - .then(function (out) { + .then((out) => { assert.strictEqual('png', out.info.format); - }); - }); + })); - it('toFormat=JPEG takes precedence over WebP extension', function (done) { + it('toFormat=JPEG takes precedence over WebP extension', (_t, done) => { const outputWebP = fixtures.path('output.webp'); sharp(fixtures.inputPng) + .resize(8) .jpeg() - .toFile(outputWebP, function (err, info) { + .toFile(outputWebP, (err, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); - rimraf(outputWebP, done); + fs.rm(outputWebP, done); }); }); - it('toFormat=WebP takes precedence over JPEG extension', function (done) { + it('toFormat=WebP takes precedence over JPEG extension', (_t, done) => { sharp(fixtures.inputPng) + .resize(8) .webp() - .toFile(outputJpg, function (err, info) { + .toFile(outputJpg, (err, info) => { if (err) throw err; assert.strictEqual('webp', info.format); done(); }); }); - it('Load Vips V file', function (done) { + it('Load Vips V file', (_t, done) => { sharp(fixtures.inputV) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -585,20 +692,54 @@ describe('Input/output', function () { }); }); - it('Save Vips V file', function (done) { + it('Save Vips V file', (_t, done) => { const outputV = fixtures.path('output.v'); sharp(fixtures.inputJpg) .extract({ left: 910, top: 1105, width: 70, height: 60 }) - .toFile(outputV, function (err, info) { + .toFile(outputV, (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(outputV, done); + fs.rm(outputV, done); }); }); + it('can ignore ICC profile', async () => { + const [r1, g1, b1] = await sharp(fixtures.inputJpgWithPortraitExif5, { ignoreIcc: true }) + .extract({ width: 1, height: 1, top: 16, left: 16 }) + .raw() + .toBuffer(); + + const [r2, g2, b2] = await sharp(fixtures.inputJpgWithPortraitExif5, { ignoreIcc: false }) + .extract({ width: 1, height: 1, top: 16, left: 16 }) + .raw() + .toBuffer(); + + assert.deepStrictEqual({ r1, g1, b1, r2, g2, b2 }, { + r1: 60, + r2: 77, + g1: 54, + g2: 69, + b1: 20, + b2: 25 + }); + }); + + describe('Switch off safety limits for certain formats', () => { + it('Valid', () => { + assert.doesNotThrow(() => { + sharp({ unlimited: true }); + }); + }); + it('Invalid', () => { + assert.throws(() => { + sharp({ unlimited: -1 }); + }, /Expected boolean for unlimited but received -1 of type number/); + }); + }); + describe('Limit pixel count of input image', () => { it('Invalid fails - negative', () => { assert.throws(() => { @@ -612,6 +753,12 @@ describe('Input/output', function () { }); }); + it('Invalid fails - integer overflow', () => { + assert.throws(() => { + sharp({ limitInputPixels: Number.MAX_SAFE_INTEGER + 1 }); + }); + }); + it('Invalid fails - string', () => { assert.throws(() => { sharp({ limitInputPixels: 'fail' }); @@ -622,7 +769,9 @@ describe('Input/output', function () { sharp(fixtures.inputJpg) .metadata() .then(({ width, height }) => - sharp(fixtures.inputJpg, { limitInputPixels: width * height }).toBuffer() + sharp(fixtures.inputJpg, { limitInputPixels: width * height }) + .resize(2) + .toBuffer() ) ); @@ -643,6 +792,17 @@ describe('Input/output', function () { }) ); + it('Enabling default limit works and fails for an image with resolution higher than uint32 limit', () => + sharp(fixtures.inputPngUint32Limit, { limitInputPixels: true }) + .toBuffer() + .then(() => { + assert.fail('Expected to fail'); + }) + .catch(err => { + assert.strictEqual(err.message, 'Input image exceeds pixel limit'); + }) + ); + it('Smaller than input fails', () => sharp(fixtures.inputJpg) .metadata() @@ -659,78 +819,169 @@ describe('Input/output', function () { ); }); - describe('Input options', function () { - it('Option-less', function () { + describe('Input options', () => { + it('Option-less', () => { sharp(); }); - it('Ignore unknown attribute', function () { + it('Ignore unknown attribute', () => { sharp({ unknown: true }); }); - it('undefined with options fails', function () { - assert.throws(function () { + it('undefined with options fails', () => { + assert.throws(() => { sharp(undefined, {}); }, /Unsupported input 'undefined' of type undefined when also providing options of type object/); }); - it('null with options fails', function () { - assert.throws(function () { + it('null with options fails', () => { + assert.throws(() => { sharp(null, {}); }, /Unsupported input 'null' of type object when also providing options of type object/); }); - it('Non-Object options fails', function () { - assert.throws(function () { + it('Non-Object options fails', () => { + assert.throws(() => { sharp('test', 'zoinks'); }, /Invalid input options zoinks/); }); - it('Invalid density: string', function () { - assert.throws(function () { + it('Invalid density: string', () => { + assert.throws(() => { sharp({ density: 'zoinks' }); }, /Expected number between 1 and 100000 for density but received zoinks of type string/); }); - it('Setting animated property updates pages property', function () { + it('Invalid ignoreIcc: string', () => { + assert.throws(() => { + sharp({ ignoreIcc: 'zoinks' }); + }, /Expected boolean for ignoreIcc but received zoinks of type string/); + }); + it('Setting animated property updates pages property', () => { assert.strictEqual(sharp({ animated: false }).options.input.pages, 1); assert.strictEqual(sharp({ animated: true }).options.input.pages, -1); }); - it('Invalid animated property throws', function () { - assert.throws(function () { + it('Invalid animated property throws', () => { + assert.throws(() => { sharp({ animated: -1 }); }, /Expected boolean for animated but received -1 of type number/); }); - it('Invalid page property throws', function () { - assert.throws(function () { + it('Invalid page property throws', () => { + assert.throws(() => { sharp({ page: -1 }); }, /Expected integer between 0 and 100000 for page but received -1 of type number/); }); - it('Invalid pages property throws', function () { - assert.throws(function () { + it('Invalid pages property throws', () => { + assert.throws(() => { sharp({ pages: '1' }); }, /Expected integer between -1 and 100000 for pages but received 1 of type string/); }); - it('Valid level property', function () { + it('Valid openSlide.level property', () => { + sharp({ openSlide: { level: 1 } }); sharp({ level: 1 }); }); - it('Invalid level property (string) throws', function () { - assert.throws(function () { - sharp({ level: '1' }); - }, /Expected integer between 0 and 256 for level but received 1 of type string/); + it('Invalid openSlide.level property (string) throws', () => { + assert.throws( + () => sharp({ openSlide: { level: '1' } }), + /Expected integer between 0 and 256 for openSlide.level but received 1 of type string/ + ); + assert.throws( + () => sharp({ level: '1' }), + /Expected integer between 0 and 256 for level but received 1 of type string/ + ); + }); + it('Invalid openSlide.level property (negative) throws', () => { + assert.throws( + () => sharp({ openSlide: { level: -1 } }), + /Expected integer between 0 and 256 for openSlide\.level but received -1 of type number/ + ); + assert.throws( + () => sharp({ level: -1 }), + /Expected integer between 0 and 256 for level but received -1 of type number/ + ); + }); + it('Valid tiff.subifd property', () => { + sharp({ tiff: { subifd: 1 } }); + sharp({ subifd: 1 }); + }); + it('Invalid tiff.subifd property (string) throws', () => { + assert.throws( + () => sharp({ tiff: { subifd: '1' } }), + /Expected integer between -1 and 100000 for tiff\.subifd but received 1 of type string/ + ); + assert.throws( + () => sharp({ subifd: '1' }), + /Expected integer between -1 and 100000 for subifd but received 1 of type string/ + ); + }); + it('Invalid tiff.subifd property (float) throws', () => { + assert.throws( + () => sharp({ tiff: { subifd: 1.2 } }), + /Expected integer between -1 and 100000 for tiff\.subifd but received 1.2 of type number/ + ); + assert.throws( + () => sharp({ subifd: 1.2 }), + /Expected integer between -1 and 100000 for subifd but received 1.2 of type number/ + ); }); - it('Invalid level property (negative) throws', function () { - assert.throws(function () { - sharp({ level: -1 }); - }, /Expected integer between 0 and 256 for level but received -1 of type number/); + it('Valid pdf.background property (string)', () => { + sharp({ pdf: { background: '#00ff00' } }); + sharp({ pdfBackground: '#00ff00' }); + }); + it('Valid pdf.background property (object)', () => { + sharp({ pdf: { background: { r: 0, g: 255, b: 0 } } }); + sharp({ pdfBackground: { r: 0, g: 255, b: 0 } }); + }); + it('Invalid pdf.background property (string) throws', () => { + assert.throws( + () => sharp({ pdf: { background: '00ff00' } }), + /Unable to parse color from string/ + ); + assert.throws( + () => sharp({ pdfBackground: '00ff00' }), + /Unable to parse color from string/ + ); + }); + it('Invalid pdf.background property (number) throws', () => { + assert.throws( + () => sharp({ pdf: { background: 255 } }), + /Expected object or string for background/ + ); + assert.throws( + () => sharp({ pdf: { background: 255 } }), + /Expected object or string for background/ + ); + }); + it('Invalid pdf.background property (object)', () => { + assert.throws( + () => sharp({ pdf: { background: { red: 0, green: 255, blue: 0 } } }), + /Unable to parse color from object/ + ); + assert.throws( + () => sharp({ pdfBackground: { red: 0, green: 255, blue: 0 } }), + /Unable to parse color from object/ + ); }); }); - describe('create new image', function () { - it('RGB', function (done) { + it('Fails when writing to missing directory', async () => { + const create = { + width: 8, + height: 8, + channels: 3, + background: { r: 0, g: 0, b: 0 } + }; + await assert.rejects( + () => sharp({ create }).toFile('does-not-exist/out.jpg'), + /unable to open for write/ + ); + }); + + describe('create new image', () => { + it('RGB', (_t, done) => { const create = { width: 10, height: 20, channels: 3, background: { r: 0, g: 255, b: 0 } }; - sharp({ create: create }) + sharp({ create }) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(create.width, info.width); assert.strictEqual(create.height, info.height); @@ -739,16 +990,16 @@ describe('Input/output', function () { fixtures.assertSimilar(fixtures.expected('create-rgb.jpg'), data, done); }); }); - it('RGBA', function (done) { + it('RGBA', (_t, done) => { const create = { width: 20, height: 10, channels: 4, background: { r: 255, g: 0, b: 0, alpha: 128 } }; - sharp({ create: create }) + sharp({ create }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(create.width, info.width); assert.strictEqual(create.height, info.height); @@ -757,40 +1008,40 @@ describe('Input/output', function () { fixtures.assertSimilar(fixtures.expected('create-rgba.png'), data, done); }); }); - it('Invalid channels', function () { + it('Invalid channels', () => { const create = { width: 10, height: 20, channels: 2, background: { r: 0, g: 0, b: 0 } }; - assert.throws(function () { - sharp({ create: create }); + assert.throws(() => { + sharp({ create }); }); }); - it('Missing background', function () { + it('Missing background', () => { const create = { width: 10, height: 20, channels: 3 }; - assert.throws(function () { - sharp({ create: create }); + assert.throws(() => { + sharp({ create }); }); }); }); - it('Queue length change events', function (done) { + it('Queue length change events', (_t, done) => { let eventCounter = 0; - const queueListener = function (queueLength) { + const queueListener = (queueLength) => { assert.strictEqual(true, queueLength === 0 || queueLength === 1); eventCounter++; }; sharp.queue.on('change', queueListener); sharp(fixtures.inputJpg) .resize(320, 240) - .toBuffer(function (err) { - process.nextTick(function () { + .toBuffer((err) => { + process.nextTick(() => { sharp.queue.removeListener('change', queueListener); if (err) throw err; assert.strictEqual(2, eventCounter); @@ -799,19 +1050,19 @@ describe('Input/output', function () { }); }); - it('Info event data', function (done) { + it('Info event data', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJPGBig); const inPipeline = sharp() .resize(840, 472) .raw() - .on('info', function (info) { + .on('info', (info) => { assert.strictEqual(840, info.width); assert.strictEqual(472, info.height); assert.strictEqual(3, info.channels); }); const badPipeline = sharp({ raw: { width: 840, height: 500, channels: 3 } }) .toFormat('jpeg') - .toBuffer(function (err, data, info) { + .toBuffer((err) => { assert.strictEqual(err.message.indexOf('memory area too small') > 0, true); const readable = fs.createReadStream(fixtures.inputJPGBig); const inPipeline = sharp() @@ -819,12 +1070,26 @@ describe('Input/output', function () { .raw(); const goodPipeline = sharp({ raw: { width: 840, height: 472, channels: 3 } }) .toFormat('jpeg') - .toBuffer(function (err, data, info) { - if (err) throw err; - done(); - }); + .toBuffer(done); readable.pipe(inPipeline).pipe(goodPipeline); }); readable.pipe(inPipeline).pipe(badPipeline); }); + + it('supports wide-character filenames', async () => { + const filename = fixtures.path('output.图片.jpg'); + const create = { + width: 8, + height: 8, + channels: 3, + background: 'green' + }; + await sharp({ create }).toFile(filename); + + const { width, height, channels, format } = await sharp(filename).metadata(); + assert.strictEqual(width, 8); + assert.strictEqual(height, 8); + assert.strictEqual(channels, 3); + assert.strictEqual(format, 'jpeg'); + }); }); diff --git a/test/unit/join.js b/test/unit/join.js new file mode 100644 index 000000000..c95c35626 --- /dev/null +++ b/test/unit/join.js @@ -0,0 +1,130 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('Join input images together', () => { + it('Join two images horizontally', async () => { + const data = await sharp([ + fixtures.inputPngPalette, + { create: { width: 68, height: 68, channels: 3, background: 'green' } } + ], { join: { across: 2 } }).toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.format, 'png'); + assert.strictEqual(metadata.width, 136); + assert.strictEqual(metadata.height, 68); + assert.strictEqual(metadata.space, 'srgb'); + assert.strictEqual(metadata.channels, 3); + assert.strictEqual(metadata.hasAlpha, false); + }); + + it('Join two images vertically with shim and alpha channel', async () => { + const data = await sharp([ + fixtures.inputPngPalette, + { create: { width: 68, height: 68, channels: 4, background: 'green' } } + ], { join: { across: 1, shim: 8 } }).toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.format, 'png'); + assert.strictEqual(metadata.width, 68); + assert.strictEqual(metadata.height, 144); + assert.strictEqual(metadata.space, 'srgb'); + assert.strictEqual(metadata.channels, 4); + assert.strictEqual(metadata.hasAlpha, true); + }); + + it('Join four images in 2x2 grid, with centre alignment', async () => { + const output = fixtures.path('output.join2x2.png'); + const info = await sharp([ + fixtures.inputPngPalette, + { create: { width: 128, height: 128, channels: 3, background: 'green' } }, + { create: { width: 128, height: 128, channels: 3, background: 'red' } }, + fixtures.inputPngPalette + ], { join: { across: 2, halign: 'centre', valign: 'centre', background: 'blue' } }) + .toFile(output); + + fixtures.assertMaxColourDistance(output, fixtures.expected('join2x2.png')); + + assert.strictEqual(info.format, 'png'); + assert.strictEqual(info.width, 256); + assert.strictEqual(info.height, 256); + assert.strictEqual(info.channels, 3); + }); + + it('Join two images as animation', async () => { + const data = await sharp([ + fixtures.inputPngPalette, + { create: { width: 68, height: 68, channels: 3, background: 'green' } } + ], { join: { animated: true } }).gif().toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.format, 'gif'); + assert.strictEqual(metadata.width, 68); + assert.strictEqual(metadata.height, 68); + assert.strictEqual(metadata.pages, 2); + }); + + it('Empty array of inputs throws', () => { + assert.throws( + () => sharp([]), + /Expected at least two images to join/ + ); + }); + it('Attempt to recursively join throws', () => { + assert.throws( + () => sharp([fixtures.inputJpg, [fixtures.inputJpg, fixtures.inputJpg]]), + /Recursive join is unsupported/ + ); + }); + it('Attempt to set join props on non-array input throws', () => { + assert.throws( + () => sharp(fixtures.inputJpg, { join: { across: 2 } }), + /Expected input to be an array of images to join/ + ); + }); + it('Invalid animated throws', () => { + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { animated: 'fail' } }), + /Expected boolean for join.animated but received fail of type string/ + ); + }); + it('Invalid across throws', () => { + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { across: 'fail' } }), + /Expected integer between 1 and 100000 for join.across but received fail of type string/ + ); + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { across: 0 } }), + /Expected integer between 1 and 100000 for join.across but received 0 of type number/ + ); + }); + it('Invalid shim throws', () => { + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { shim: 'fail' } }), + /Expected integer between 0 and 100000 for join.shim but received fail of type string/ + ); + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { shim: -1 } }), + /Expected integer between 0 and 100000 for join.shim but received -1 of type number/ + ); + }); + it('Invalid halign', () => { + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { halign: 'fail' } }), + /Expected valid alignment for join.halign but received fail of type string/ + ); + }); + it('Invalid valign', () => { + assert.throws( + () => sharp([fixtures.inputJpg, fixtures.inputJpg], { join: { valign: 'fail' } }), + /Expected valid alignment for join.valign but received fail of type string/ + ); + }); +}); diff --git a/test/unit/joinChannel.js b/test/unit/joinChannel.js index 1fd9cae5e..feebb1337 100644 --- a/test/unit/joinChannel.js +++ b/test/unit/joinChannel.js @@ -1,18 +1,22 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); -const fs = require('fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Image channel insertion', function () { - it('Grayscale to RGB, buffer', function (done) { +describe('Image channel insertion', () => { + it('Grayscale to RGB, buffer', (_t, done) => { sharp(fixtures.inputPng) // gray -> red .resize(320, 240) .joinChannel(fixtures.inputPngTestJoinChannel) // new green channel .joinChannel(fixtures.inputPngStripesH) // new blue channel - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -21,12 +25,12 @@ describe('Image channel insertion', function () { }); }); - it('Grayscale to RGB, file', function (done) { + it('Grayscale to RGB, file', (_t, done) => { sharp(fixtures.inputPng) // gray -> red .resize(320, 240) .joinChannel(fs.readFileSync(fixtures.inputPngTestJoinChannel)) // new green channel .joinChannel(fs.readFileSync(fixtures.inputPngStripesH)) // new blue channel - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -35,7 +39,7 @@ describe('Image channel insertion', function () { }); }); - it('Grayscale to RGBA, buffer', function (done) { + it('Grayscale to RGBA, buffer', (_t, done) => { sharp(fixtures.inputPng) // gray -> red .resize(320, 240) .joinChannel([ @@ -44,7 +48,7 @@ describe('Image channel insertion', function () { fixtures.inputPngStripesV ]) // new green + blue + alpha channel .toColourspace(sharp.colourspace.srgb) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -53,7 +57,7 @@ describe('Image channel insertion', function () { }); }); - it('Grayscale to RGBA, file', function (done) { + it('Grayscale to RGBA, file', (_t, done) => { sharp(fixtures.inputPng) // gray -> red .resize(320, 240) .joinChannel([ @@ -62,7 +66,7 @@ describe('Image channel insertion', function () { fs.readFileSync(fixtures.inputPngStripesV) // new alpha channel ]) .toColourspace('srgb') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -71,7 +75,7 @@ describe('Image channel insertion', function () { }); }); - it('Grayscale to CMYK, buffers', function (done) { + it('Grayscale to CMYK, buffers', (_t, done) => { sharp(fixtures.inputPng) // gray -> magenta .resize(320, 240) .joinChannel([ @@ -81,7 +85,7 @@ describe('Image channel insertion', function () { ]) .toColorspace('cmyk') .toFormat('jpeg') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -90,12 +94,12 @@ describe('Image channel insertion', function () { }); }); - it('Join raw buffers to RGB', function (done) { + it('Join raw buffers to RGB', (_t, done) => { Promise.all([ sharp(fixtures.inputPngTestJoinChannel).toColourspace('b-w').raw().toBuffer(), sharp(fixtures.inputPngStripesH).toColourspace('b-w').raw().toBuffer() ]) - .then(function (buffers) { + .then((buffers) => { sharp(fixtures.inputPng) .resize(320, 240) .joinChannel(buffers, { @@ -105,7 +109,7 @@ describe('Image channel insertion', function () { channels: 1 } }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -113,12 +117,12 @@ describe('Image channel insertion', function () { fixtures.assertSimilar(fixtures.expected('joinChannel-rgb.jpg'), data, done); }); }) - .catch(function (err) { + .catch((err) => { throw err; }); }); - it('Grayscale to RGBA, files, two arrays', function (done) { + it('Grayscale to RGBA, files, two arrays', (_t, done) => { sharp(fixtures.inputPng) // gray -> red .resize(320, 240) .joinChannel([fs.readFileSync(fixtures.inputPngTestJoinChannel)]) // new green channel @@ -127,7 +131,7 @@ describe('Image channel insertion', function () { fs.readFileSync(fixtures.inputPngStripesV) // new alpha channel ]) .toColourspace('srgb') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -136,20 +140,20 @@ describe('Image channel insertion', function () { }); }); - it('Invalid raw buffer description', function () { - assert.throws(function () { + it('Invalid raw buffer description', () => { + assert.throws(() => { sharp().joinChannel(fs.readFileSync(fixtures.inputPng), { raw: {} }); }); }); - it('Invalid input', function () { - assert.throws(function () { + it('Invalid input', () => { + assert.throws(() => { sharp(fixtures.inputJpg).joinChannel(1); }); }); - it('No arguments', function () { - assert.throws(function () { + it('No arguments', () => { + assert.throws(() => { sharp(fixtures.inputJpg).joinChannel(); }); }); diff --git a/test/unit/jp2.js b/test/unit/jp2.js new file mode 100644 index 000000000..536e0a040 --- /dev/null +++ b/test/unit/jp2.js @@ -0,0 +1,131 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('JP2 output', () => { + if (!sharp.format.jp2k.input.buffer) { + it('JP2 output should fail due to missing OpenJPEG', () => + assert.rejects(async () => + sharp(fixtures.inputJpg) + .jp2() + .toBuffer(), + /JP2 output requires libvips with support for OpenJPEG/ + ) + ); + + it('JP2 file output should fail due to missing OpenJPEG', () => + assert.rejects(async () => sharp(fixtures.inputJpg).toFile('test.jp2'), + /JP2 output requires libvips with support for OpenJPEG/ + ) + ); + + it('File with JP2-like suffix should not fail due to missing OpenJPEG', () => { + const output = fixtures.path('output.failj2c'); + return assert.doesNotReject( + async () => sharp(fixtures.inputPngWithOneColor).toFile(output) + ); + }); + } else { + it('JP2 Buffer to PNG Buffer', () => { + sharp(fs.readFileSync(fixtures.inputJp2)) + .resize(8, 15) + .png() + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { + assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); + assert.strictEqual('png', info.format); + assert.strictEqual(8, info.width); + assert.strictEqual(15, info.height); + assert.strictEqual(3, info.channels); + }); + }); + + it('JP2 quality', (_t, done) => { + sharp(fixtures.inputJp2) + .resize(320, 240) + .jp2({ quality: 70 }) + .toBuffer((err, buffer70) => { + if (err) throw err; + sharp(fixtures.inputJp2) + .resize(320, 240) + .toBuffer((err, buffer80) => { + if (err) throw err; + assert(buffer70.length < buffer80.length); + done(); + }); + }); + }); + + it('Without chroma subsampling generates larger file', (_t, done) => { + // First generate with chroma subsampling (default) + sharp(fixtures.inputJp2) + .resize(320, 240) + .jp2({ chromaSubsampling: '4:2:0' }) + .toBuffer((err, withChromaSubsamplingData, withChromaSubsamplingInfo) => { + if (err) throw err; + assert.strictEqual(true, withChromaSubsamplingData.length > 0); + assert.strictEqual(withChromaSubsamplingData.length, withChromaSubsamplingInfo.size); + assert.strictEqual('jp2', withChromaSubsamplingInfo.format); + assert.strictEqual(320, withChromaSubsamplingInfo.width); + assert.strictEqual(240, withChromaSubsamplingInfo.height); + // Then generate without + sharp(fixtures.inputJp2) + .resize(320, 240) + .jp2({ chromaSubsampling: '4:4:4' }) + .toBuffer((err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) => { + if (err) throw err; + assert.strictEqual(true, withoutChromaSubsamplingData.length > 0); + assert.strictEqual(withoutChromaSubsamplingData.length, withoutChromaSubsamplingInfo.size); + assert.strictEqual('jp2', withoutChromaSubsamplingInfo.format); + assert.strictEqual(320, withoutChromaSubsamplingInfo.width); + assert.strictEqual(240, withoutChromaSubsamplingInfo.height); + assert.strictEqual(true, withChromaSubsamplingData.length <= withoutChromaSubsamplingData.length); + done(); + }); + }); + }); + + it('can use the jp2Oneshot option to handle multi-part tiled JPEG 2000 file', async () => { + const outputJpg = fixtures.path('output.jpg'); + await assert.rejects( + () => sharp(fixtures.inputJp2TileParts).toFile(outputJpg) + ); + await assert.doesNotReject(async () => { + await sharp(fixtures.inputJp2TileParts, { jp2Oneshot: true }).toFile(outputJpg); + const { format, width, height } = await sharp(outputJpg).metadata(); + assert.strictEqual(format, 'jpeg'); + assert.strictEqual(width, 320); + assert.strictEqual(height, 240); + }); + }); + + it('Invalid JP2 chromaSubsampling value throws error', () => { + assert.throws( + () => sharp().jp2({ chromaSubsampling: '4:2:2' }), + /Expected one of: 4:2:0, 4:4:4 for chromaSubsampling but received 4:2:2 of type string/ + ); + }); + } + + it('valid JP2 oneshot value does not throw error', () => { + assert.doesNotThrow( + () => sharp({ jp2: { oneshot: true } }) + ); + }); + + it('invalid JP2 oneshot value throws error', () => { + assert.throws( + () => sharp({ jp2: { oneshot: 'fail' } }), + /Expected boolean for jp2.oneshot but received fail of type string/ + ); + }); +}); diff --git a/test/unit/jpeg.js b/test/unit/jpeg.js index f368843ca..9a802cb8f 100644 --- a/test/unit/jpeg.js +++ b/test/unit/jpeg.js @@ -1,25 +1,29 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('JPEG', function () { - it('JPEG quality', function (done) { +describe('JPEG', () => { + it('JPEG quality', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ quality: 70 }) - .toBuffer(function (err, buffer70) { + .toBuffer((err, buffer70) => { if (err) throw err; sharp(fixtures.inputJpg) .resize(320, 240) - .toBuffer(function (err, buffer80) { + .toBuffer((err, buffer80) => { if (err) throw err; sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ quality: 90 }) - .toBuffer(function (err, buffer90) { + .toBuffer((err, buffer90) => { if (err) throw err; assert(buffer70.length < buffer80.length); assert(buffer80.length < buffer90.length); @@ -29,31 +33,31 @@ describe('JPEG', function () { }); }); - describe('Invalid JPEG quality', function () { - [-1, 88.2, 'test'].forEach(function (quality) { - it(quality.toString(), function () { - assert.throws(function () { - sharp().jpeg({ quality: quality }); + describe('Invalid JPEG quality', () => { + [-1, 88.2, 'test'].forEach((quality) => { + it(quality.toString(), () => { + assert.throws(() => { + sharp().jpeg({ quality }); }); }); }); }); - describe('Invalid JPEG quantisation table', function () { - [-1, 88.2, 'test'].forEach(function (table) { - it(table.toString(), function () { - assert.throws(function () { + describe('Invalid JPEG quantisation table', () => { + [-1, 88.2, 'test'].forEach((table) => { + it(table.toString(), () => { + assert.throws(() => { sharp().jpeg({ quantisationTable: table }); }); }); }); }); - it('Progressive JPEG image', function (done) { + it('Progressive JPEG image', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ progressive: false }) - .toBuffer(function (err, nonProgressiveData, nonProgressiveInfo) { + .toBuffer((err, nonProgressiveData, nonProgressiveInfo) => { if (err) throw err; assert.strictEqual(true, nonProgressiveData.length > 0); assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size); @@ -63,7 +67,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ progressive: true }) - .toBuffer(function (err, progressiveData, progressiveInfo) { + .toBuffer((err, progressiveData, progressiveInfo) => { if (err) throw err; assert.strictEqual(true, progressiveData.length > 0); assert.strictEqual(progressiveData.length, progressiveInfo.size); @@ -76,12 +80,12 @@ describe('JPEG', function () { }); }); - it('Without chroma subsampling generates larger file', function (done) { + it('Without chroma subsampling generates larger file', (_t, done) => { // First generate with chroma subsampling (default) sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ chromaSubsampling: '4:2:0' }) - .toBuffer(function (err, withChromaSubsamplingData, withChromaSubsamplingInfo) { + .toBuffer((err, withChromaSubsamplingData, withChromaSubsamplingInfo) => { if (err) throw err; assert.strictEqual(true, withChromaSubsamplingData.length > 0); assert.strictEqual(withChromaSubsamplingData.length, withChromaSubsamplingInfo.size); @@ -92,7 +96,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ chromaSubsampling: '4:4:4' }) - .toBuffer(function (err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) { + .toBuffer((err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) => { if (err) throw err; assert.strictEqual(true, withoutChromaSubsamplingData.length > 0); assert.strictEqual(withoutChromaSubsamplingData.length, withoutChromaSubsamplingInfo.size); @@ -105,18 +109,18 @@ describe('JPEG', function () { }); }); - it('Invalid JPEG chromaSubsampling value throws error', function () { - assert.throws(function () { + it('Invalid JPEG chromaSubsampling value throws error', () => { + assert.throws(() => { sharp().jpeg({ chromaSubsampling: '4:2:2' }); }); }); - it('Trellis quantisation', function (done) { + it('Trellis quantisation', (_t, done) => { // First generate without sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ trellisQuantisation: false }) - .toBuffer(function (err, withoutData, withoutInfo) { + .toBuffer((err, withoutData, withoutInfo) => { if (err) throw err; assert.strictEqual(true, withoutData.length > 0); assert.strictEqual(withoutData.length, withoutInfo.size); @@ -127,7 +131,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ trellisQuantization: true }) - .toBuffer(function (err, withData, withInfo) { + .toBuffer((err, withData, withInfo) => { if (err) throw err; assert.strictEqual(true, withData.length > 0); assert.strictEqual(withData.length, withInfo.size); @@ -141,12 +145,12 @@ describe('JPEG', function () { }); }); - it('Overshoot deringing', function (done) { + it('Overshoot deringing', (_t, done) => { // First generate without sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ overshootDeringing: false }) - .toBuffer(function (err, withoutData, withoutInfo) { + .toBuffer((err, withoutData, withoutInfo) => { if (err) throw err; assert.strictEqual(true, withoutData.length > 0); assert.strictEqual(withoutData.length, withoutInfo.size); @@ -157,7 +161,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ overshootDeringing: true }) - .toBuffer(function (err, withData, withInfo) { + .toBuffer((err, withData, withInfo) => { if (err) throw err; assert.strictEqual(true, withData.length > 0); assert.strictEqual(withData.length, withInfo.size); @@ -169,12 +173,12 @@ describe('JPEG', function () { }); }); - it('Optimise scans generates different output length', function (done) { + it('Optimise scans generates different output length', (_t, done) => { // First generate without sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimiseScans: false }) - .toBuffer(function (err, withoutData, withoutInfo) { + .toBuffer((err, withoutData, withoutInfo) => { if (err) throw err; assert.strictEqual(true, withoutData.length > 0); assert.strictEqual(withoutData.length, withoutInfo.size); @@ -185,7 +189,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimizeScans: true }) - .toBuffer(function (err, withData, withInfo) { + .toBuffer((err, withData, withInfo) => { if (err) throw err; assert.strictEqual(true, withData.length > 0); assert.strictEqual(withData.length, withInfo.size); @@ -199,12 +203,12 @@ describe('JPEG', function () { }); }); - it('Optimise coding generates smaller output length', function (done) { + it('Optimise coding generates smaller output length', (_t, done) => { // First generate with optimize coding enabled (default) sharp(fixtures.inputJpg) .resize(320, 240) .jpeg() - .toBuffer(function (err, withOptimiseCoding, withInfo) { + .toBuffer((err, withOptimiseCoding, withInfo) => { if (err) throw err; assert.strictEqual(true, withOptimiseCoding.length > 0); assert.strictEqual(withOptimiseCoding.length, withInfo.size); @@ -215,7 +219,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimizeCoding: false }) - .toBuffer(function (err, withoutOptimiseCoding, withoutInfo) { + .toBuffer((err, withoutOptimiseCoding, withoutInfo) => { if (err) throw err; assert.strictEqual(true, withoutOptimiseCoding.length > 0); assert.strictEqual(withoutOptimiseCoding.length, withoutInfo.size); @@ -229,12 +233,12 @@ describe('JPEG', function () { }); }); - it('Specifying quantisation table provides different JPEG', function (done) { + it('Specifying quantisation table provides different JPEG', (_t, done) => { // First generate with default quantisation table sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimiseCoding: false }) - .toBuffer(function (err, withDefaultQuantisationTable, withInfo) { + .toBuffer((err, withDefaultQuantisationTable, withInfo) => { if (err) throw err; assert.strictEqual(true, withDefaultQuantisationTable.length > 0); assert.strictEqual(withDefaultQuantisationTable.length, withInfo.size); @@ -245,7 +249,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimiseCoding: false, quantisationTable: 3 }) - .toBuffer(function (err, withQuantTable3, withoutInfo) { + .toBuffer((err, withQuantTable3, withoutInfo) => { if (err) throw err; assert.strictEqual(true, withQuantTable3.length > 0); assert.strictEqual(withQuantTable3.length, withoutInfo.size); @@ -260,12 +264,12 @@ describe('JPEG', function () { }); }); - it('Specifying quantization table provides different JPEG', function (done) { + it('Specifying quantization table provides different JPEG', (_t, done) => { // First generate with default quantization table sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimiseCoding: false }) - .toBuffer(function (err, withDefaultQuantizationTable, withInfo) { + .toBuffer((err, withDefaultQuantizationTable, withInfo) => { if (err) throw err; assert.strictEqual(true, withDefaultQuantizationTable.length > 0); assert.strictEqual(withDefaultQuantizationTable.length, withInfo.size); @@ -276,7 +280,7 @@ describe('JPEG', function () { sharp(fixtures.inputJpg) .resize(320, 240) .jpeg({ optimiseCoding: false, quantizationTable: 3 }) - .toBuffer(function (err, withQuantTable3, withoutInfo) { + .toBuffer((err, withQuantTable3, withoutInfo) => { if (err) throw err; assert.strictEqual(true, withQuantTable3.length > 0); assert.strictEqual(withQuantTable3.length, withoutInfo.size); diff --git a/test/unit/jxl.js b/test/unit/jxl.js new file mode 100644 index 000000000..f70de1cbb --- /dev/null +++ b/test/unit/jxl.js @@ -0,0 +1,101 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); + +describe('JXL', () => { + it('called without options does not throw an error', () => { + assert.doesNotThrow(() => { + sharp().jxl(); + }); + }); + it('valid distance does not throw an error', () => { + assert.doesNotThrow(() => { + sharp().jxl({ distance: 2.3 }); + }); + }); + it('invalid distance should throw an error', () => { + assert.throws(() => { + sharp().jxl({ distance: 15.1 }); + }); + }); + it('non-numeric distance should throw an error', () => { + assert.throws(() => { + sharp().jxl({ distance: 'fail' }); + }); + }); + it('valid quality > 30 does not throw an error', () => { + const s = sharp(); + assert.doesNotThrow(() => { + s.jxl({ quality: 80 }); + }); + assert.strictEqual(s.options.jxlDistance, 1.9); + }); + it('valid quality < 30 does not throw an error', () => { + const s = sharp(); + assert.doesNotThrow(() => { + s.jxl({ quality: 20 }); + }); + assert.strictEqual(s.options.jxlDistance, 9.066666666666666); + }); + it('valid quality does not throw an error', () => { + assert.doesNotThrow(() => { + sharp().jxl({ quality: 80 }); + }); + }); + it('invalid quality should throw an error', () => { + assert.throws(() => { + sharp().jxl({ quality: 101 }); + }); + }); + it('non-numeric quality should throw an error', () => { + assert.throws(() => { + sharp().jxl({ quality: 'fail' }); + }); + }); + it('valid decodingTier does not throw an error', () => { + assert.doesNotThrow(() => { + sharp().jxl({ decodingTier: 2 }); + }); + }); + it('invalid decodingTier should throw an error', () => { + assert.throws(() => { + sharp().jxl({ decodingTier: 5 }); + }); + }); + it('non-numeric decodingTier should throw an error', () => { + assert.throws(() => { + sharp().jxl({ decodingTier: 'fail' }); + }); + }); + it('valid lossless does not throw an error', () => { + assert.doesNotThrow(() => { + sharp().jxl({ lossless: true }); + }); + }); + it('non-boolean lossless should throw an error', () => { + assert.throws(() => { + sharp().jxl({ lossless: 'fail' }); + }); + }); + it('valid effort does not throw an error', () => { + assert.doesNotThrow(() => { + sharp().jxl({ effort: 6 }); + }); + }); + it('out of range effort should throw an error', () => { + assert.throws(() => { + sharp().jxl({ effort: 10 }); + }); + }); + it('invalid effort should throw an error', () => { + assert.throws(() => { + sharp().jxl({ effort: 'fail' }); + }); + }); +}); diff --git a/test/unit/libvips.js b/test/unit/libvips.js index 60d566584..8ed822458 100644 --- a/test/unit/libvips.js +++ b/test/unit/libvips.js @@ -1,66 +1,65 @@ -'use strict'; - -const assert = require('assert'); -const fs = require('fs'); +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { after, before, describe, it } = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); const semver = require('semver'); const libvips = require('../../lib/libvips'); -const mockFS = require('mock-fs'); const originalPlatform = process.platform; -const setPlatform = function (platform) { +const setPlatform = (platform) => { Object.defineProperty(process, 'platform', { value: platform }); }; -const restorePlatform = function () { +const restorePlatform = () => { setPlatform(originalPlatform); }; -describe('libvips binaries', function () { - describe('Windows platform', function () { - before(function () { setPlatform('win32'); }); +describe('libvips binaries', () => { + describe('Windows platform', () => { + before(() => { setPlatform('win32'); }); after(restorePlatform); - it('pkgConfigPath returns empty string', function () { + it('pkgConfigPath returns empty string', () => { assert.strictEqual('', libvips.pkgConfigPath()); }); - it('globalLibvipsVersion returns empty string', function () { + it('globalLibvipsVersion returns empty string', () => { assert.strictEqual('', libvips.globalLibvipsVersion()); }); - it('globalLibvipsVersion is always false', function () { + it('globalLibvipsVersion is always false', () => { assert.strictEqual(false, libvips.useGlobalLibvips()); }); }); - describe('non-Windows platforms', function () { - before(function () { setPlatform('linux'); }); + describe('non-Windows platforms', () => { + before(() => { setPlatform('linux'); }); after(restorePlatform); - it('pkgConfigPath returns a string', function () { + it('pkgConfigPath returns a string', () => { const pkgConfigPath = libvips.pkgConfigPath(); assert.strictEqual('string', typeof pkgConfigPath); }); - it('globalLibvipsVersion returns a string', function () { + it('globalLibvipsVersion returns a string', () => { const globalLibvipsVersion = libvips.globalLibvipsVersion(); assert.strictEqual('string', typeof globalLibvipsVersion); }); - it('globalLibvipsVersion returns a boolean', function () { + it('globalLibvipsVersion returns a boolean', () => { const useGlobalLibvips = libvips.useGlobalLibvips(); assert.strictEqual('boolean', typeof useGlobalLibvips); }); }); - describe('platform agnostic', function () { - it('minimumLibvipsVersion returns a valid semver', function () { + describe('platform agnostic', () => { + it('minimumLibvipsVersion returns a valid semver', () => { const minimumLibvipsVersion = libvips.minimumLibvipsVersion; assert.strictEqual('string', typeof minimumLibvipsVersion); assert.notStrictEqual(null, semver.valid(minimumLibvipsVersion)); }); - it('hasVendoredLibvips returns a boolean', function () { - const hasVendoredLibvips = libvips.hasVendoredLibvips(); - assert.strictEqual('boolean', typeof hasVendoredLibvips); - }); - it('useGlobalLibvips can be ignored via an env var', function () { + it('useGlobalLibvips can be ignored via an env var', () => { process.env.SHARP_IGNORE_GLOBAL_LIBVIPS = 1; const useGlobalLibvips = libvips.useGlobalLibvips(); @@ -68,67 +67,130 @@ describe('libvips binaries', function () { delete process.env.SHARP_IGNORE_GLOBAL_LIBVIPS; }); - it('cachePath returns a valid path ending with _libvips', function () { - const cachePath = libvips.cachePath(); - assert.strictEqual('string', typeof cachePath); - assert.strictEqual('_libvips', cachePath.substr(-8)); - assert.strictEqual(true, fs.existsSync(cachePath)); + it('useGlobalLibvips can be forced via an env var', () => { + process.env.SHARP_FORCE_GLOBAL_LIBVIPS = 1; + + const useGlobalLibvips = libvips.useGlobalLibvips(); + assert.strictEqual(true, useGlobalLibvips); + + let logged = false; + const logger = (message) => { + assert.strictEqual(message, 'Detected SHARP_FORCE_GLOBAL_LIBVIPS, skipping search for globally-installed libvips'); + logged = true; + }; + const useGlobalLibvipsWithLogger = libvips.useGlobalLibvips(logger); + assert.strictEqual(true, useGlobalLibvipsWithLogger); + assert.strictEqual(true, logged); + + delete process.env.SHARP_FORCE_GLOBAL_LIBVIPS; }); }); - describe('safe directory creation', function () { - before(function () { - mockFS({ - exampleDirA: { - exampleDirB: { - exampleFile: 'Example test file' - } - } - }); + describe('Build time platform detection', () => { + it('Can override platform with npm_config_platform and npm_config_libc', function () { + process.env.npm_config_platform = 'testplatform'; + process.env.npm_config_libc = 'testlibc'; + const platformArch = libvips.buildPlatformArch(); + if (platformArch === 'wasm32') { + return this.skip(); + } + const [platform] = platformArch.split('-'); + assert.strictEqual(platform, 'testplatformtestlibc'); + delete process.env.npm_config_platform; + delete process.env.npm_config_libc; + }); + it('Can override arch with npm_config_arch', function () { + process.env.npm_config_arch = 'test'; + const platformArch = libvips.buildPlatformArch(); + if (platformArch === 'wasm32') { + return this.skip(); + } + const [, arch] = platformArch.split('-'); + assert.strictEqual(arch, 'test'); + delete process.env.npm_config_arch; }); - after(function () { mockFS.restore(); }); - - it('mkdirSync creates a directory', function () { - const dirPath = 'createdDir'; + }); - libvips.mkdirSync(dirPath); - assert.strictEqual(true, fs.existsSync(dirPath)); + describe('Build time directories', () => { + it('sharp-libvips include', () => { + const dir = libvips.buildSharpLibvipsIncludeDir(); + if (dir) { + assert.strictEqual(fs.statSync(dir).isDirectory(), true); + } + }); + it('sharp-libvips cplusplus', () => { + const dir = libvips.buildSharpLibvipsCPlusPlusDir(); + if (dir) { + assert.strictEqual(fs.statSync(dir).isDirectory(), true); + } + }); + it('sharp-libvips lib', () => { + const dir = libvips.buildSharpLibvipsLibDir(); + if (dir) { + assert.strictEqual(fs.statSync(dir).isDirectory(), true); + } }); - it('mkdirSync does not throw error or overwrite an existing dir', function () { - const dirPath = 'exampleDirA'; - const nestedDirPath = 'exampleDirA/exampleDirB'; - assert.strictEqual(true, fs.existsSync(dirPath)); - - libvips.mkdirSync(dirPath); + }); - assert.strictEqual(true, fs.existsSync(dirPath)); - assert.strictEqual(true, fs.existsSync(nestedDirPath)); + describe('Runtime detection', () => { + it('platform', () => { + const [platform] = libvips.runtimePlatformArch().split('-'); + assert.strict(['darwin', 'freebsd', 'linux', 'linuxmusl', 'win32'].includes(platform)); + }); + it('arch', () => { + const [, arch] = libvips.runtimePlatformArch().split('-'); + assert.strict(['arm', 'arm64', 'ia32', 'x64', 'ppc64'].includes(arch)); + }); + it('isUnsupportedNodeRuntime', () => { + assert.strictEqual(libvips.isUnsupportedNodeRuntime(), undefined); }); }); - describe('logger', function () { + describe('logger', () => { const consoleLog = console.log; const consoleError = console.error; - after(function () { + after(() => { console.log = consoleLog; console.error = consoleError; }); - it('logs an info message', function (done) { - console.log = function (msg) { + it('logs an info message', (_t, done) => { + console.log = (msg) => { assert.strictEqual(msg, 'sharp: progress'); done(); }; libvips.log('progress'); }); - it('logs an error message', function (done) { - console.error = function (msg) { - assert.strictEqual(msg, 'sharp: problem'); + it('logs an error message', (_t, done) => { + console.error = (msg) => { + assert.strictEqual(msg, 'sharp: Installation error: problem'); done(); }; libvips.log(new Error('problem')); }); }); + + describe('yarn locator hash', () => { + it('known platform', () => { + const cc = process.env.CC; + delete process.env.CC; + process.env.npm_config_platform = 'linux'; + process.env.npm_config_arch = 's390x'; + process.env.npm_config_libc = ''; + const locatorHash = libvips.yarnLocator(); + assert.strictEqual(locatorHash, '4ab19140fd'); + delete process.env.npm_config_platform; + delete process.env.npm_config_arch; + delete process.env.npm_config_libc; + process.env.CC = cc; + }); + it('unknown platform', () => { + process.env.npm_config_platform = 'unknown-platform'; + const locatorHash = libvips.yarnLocator(); + assert.strictEqual(locatorHash, ''); + delete process.env.npm_config_platform; + }); + }); }); diff --git a/test/unit/linear.js b/test/unit/linear.js index 17d9c5e77..c6d29a7de 100644 --- a/test/unit/linear.js +++ b/test/unit/linear.js @@ -1,79 +1,137 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const sharp = require('../../'); const fixtures = require('../fixtures'); -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); -describe('Linear adjustment', function () { +describe('Linear adjustment', () => { const blackPoint = 70; const whitePoint = 203; const a = 255 / (whitePoint - blackPoint); const b = -blackPoint * a; - it('applies linear levels adjustment w/o alpha ch', function (done) { + it('applies linear levels adjustment w/o alpha ch', (_t, done) => { sharp(fixtures.inputJpgWithLowContrast) .linear(a, b) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.expected('low-contrast-linear.jpg'), data, done); }); }); - it('applies slope level adjustment w/o alpha ch', function (done) { + it('applies slope level adjustment w/o alpha ch', (_t, done) => { sharp(fixtures.inputJpgWithLowContrast) .linear(a) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.expected('low-contrast-slope.jpg'), data, done); }); }); - it('applies offset level adjustment w/o alpha ch', function (done) { + it('applies offset level adjustment w/o alpha ch', (_t, done) => { sharp(fixtures.inputJpgWithLowContrast) .linear(null, b) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.expected('low-contrast-offset.jpg'), data, done); }); }); - it('applies linear levels adjustment w alpha ch', function (done) { + it('applies linear levels adjustment w alpha ch', (_t, done) => { sharp(fixtures.inputPngOverlayLayer1) + .resize(240) .linear(a, b) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-linear.png'), data, done); }); }); - it('applies slope level adjustment w alpha ch', function (done) { + it('applies linear levels adjustment to 16-bit w alpha ch', (_t, done) => { + sharp(fixtures.inputPngWithTransparency16bit) + .linear(a, b) + .png({ compressionLevel: 0 }) + .toBuffer((err, data) => { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('linear-16bit.png'), data, done); + }); + }); + + it('applies slope level adjustment w alpha ch', (_t, done) => { sharp(fixtures.inputPngOverlayLayer1) + .resize(240) .linear(a) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-slope.png'), data, done); }); }); - it('applies offset level adjustment w alpha ch', function (done) { + it('applies offset level adjustment w alpha ch', (_t, done) => { sharp(fixtures.inputPngOverlayLayer1) + .resize(240) .linear(null, b) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-offset.png'), data, done); }); }); - it('Invalid linear arguments', function () { - assert.throws(function () { - sharp(fixtures.inputPngOverlayLayer1) - .linear('foo'); - }); + it('per channel level adjustment', (_t, done) => { + sharp(fixtures.inputWebP) + .linear([0.25, 0.5, 0.75], [150, 100, 50]).toBuffer((err, data) => { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('linear-per-channel.jpg'), data, done); + }); + }); + + it('output is integer, not float, RGB', async () => { + const data = await sharp({ create: { width: 1, height: 1, channels: 3, background: 'red' } }) + .linear(1, 0) + .tiff({ compression: 'none' }) + .toBuffer(); + + const { channels, depth } = await sharp(data).metadata(); + assert.strictEqual(channels, 3); + assert.strictEqual(depth, 'uchar'); + }); + + it('output is integer, not float, RGBA', async () => { + const data = await sharp({ create: { width: 1, height: 1, channels: 4, background: '#ff000077' } }) + .linear(1, 0) + .tiff({ compression: 'none' }) + .toBuffer(); + + const { channels, depth } = await sharp(data).metadata(); + assert.strictEqual(channels, 4); + assert.strictEqual(depth, 'uchar'); + }); - assert.throws(function () { - sharp(fixtures.inputPngOverlayLayer1) - .linear(undefined, { bar: 'baz' }); - }); + it('Invalid linear arguments', () => { + assert.throws( + () => sharp().linear('foo'), + /Expected number or array of numbers for a but received foo of type string/ + ); + assert.throws( + () => sharp().linear(undefined, { bar: 'baz' }), + /Expected number or array of numbers for b but received \[object Object\] of type object/ + ); + assert.throws( + () => sharp().linear([], [1]), + /Expected number or array of numbers for a but received {2}of type object/ + ); + assert.throws( + () => sharp().linear([1, 2], [1]), + /Expected a and b to be arrays of the same length/ + ); + assert.throws( + () => sharp().linear([1]), + /Expected a and b to be arrays of the same length/ + ); }); }); diff --git a/test/unit/median.js b/test/unit/median.js index a25ef1a1f..14d63d566 100644 --- a/test/unit/median.js +++ b/test/unit/median.js @@ -1,72 +1,55 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); -const fixtures = require('../fixtures'); -describe('Median filter', function () { - it('1x1 window', function (done) { - sharp(fixtures.inputJpgThRandom) - .median(1) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(200, info.width); - assert.strictEqual(200, info.height); - fixtures.assertSimilar(fixtures.expected('median_1.jpg'), data, done); - }); +const row = [0, 3, 15, 63, 127, 255]; +const input = Buffer.from(Array.from(row, () => row).flat()); +const raw = { + width: 6, + height: 6, + channels: 1 +}; + +describe('Median filter', () => { + it('default window (3x3)', async () => { + const data = await sharp(input, { raw }) + .median() + .toColourspace('b-w') + .raw() + .toBuffer(); + + assert.deepStrictEqual(data.subarray(0, 6), Buffer.from(row)); }); - it('3x3 window', function (done) { - sharp(fixtures.inputJpgThRandom) + it('3x3 window', async () => { + const data = await sharp(input, { raw }) .median(3) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(200, info.width); - assert.strictEqual(200, info.height); - fixtures.assertSimilar(fixtures.expected('median_3.jpg'), data, done); - }); - }); - it('5x5 window', function (done) { - sharp(fixtures.inputJpgThRandom) - .median(5) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(200, info.width); - assert.strictEqual(200, info.height); - fixtures.assertSimilar(fixtures.expected('median_5.jpg'), data, done); - }); + .toColourspace('b-w') + .raw() + .toBuffer(); + + assert.deepStrictEqual(data.subarray(0, 6), Buffer.from(row)); }); - it('color image', function (done) { - sharp(fixtures.inputJpgRandom) + it('5x5 window', async () => { + const data = await sharp(input, { raw }) .median(5) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(200, info.width); - assert.strictEqual(200, info.height); - fixtures.assertSimilar(fixtures.expected('median_color.jpg'), data, done); - }); - }); + .toColourspace('b-w') + .raw() + .toBuffer(); - it('no windows size', function (done) { - sharp(fixtures.inputJpgThRandom) - .median() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(200, info.width); - assert.strictEqual(200, info.height); - fixtures.assertSimilar(fixtures.expected('median_3.jpg'), data, done); - }); + assert.deepStrictEqual(data.subarray(0, 6), Buffer.from(row)); }); - it('invalid radius', function () { - assert.throws(function () { - sharp(fixtures.inputJpg).median(0.1); + + it('invalid radius', () => { + assert.throws(() => { + sharp().median(0.1); }); }); }); diff --git a/test/unit/metadata.js b/test/unit/metadata.js index e26957889..300380edd 100644 --- a/test/unit/metadata.js +++ b/test/unit/metadata.js @@ -1,16 +1,22 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const exifReader = require('exif-reader'); const icc = require('icc'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Image metadata', function () { - it('JPEG', function (done) { - sharp(fixtures.inputJpg).metadata(function (err, metadata) { +const create = { width: 1, height: 1, channels: 3, background: 'red' }; + +describe('Image metadata', () => { + it('JPEG', (_t, done) => { + sharp(fixtures.inputJpg).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('jpeg', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -31,8 +37,8 @@ describe('Image metadata', function () { }); }); - it('JPEG with EXIF/ICC', function (done) { - sharp(fixtures.inputJpgWithExif).metadata(function (err, metadata) { + it('JPEG with EXIF/ICC', (_t, done) => { + sharp(fixtures.inputJpgWithExif).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('jpeg', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -52,8 +58,8 @@ describe('Image metadata', function () { assert.strictEqual(true, metadata.exif instanceof Buffer); const exif = exifReader(metadata.exif); assert.strictEqual('object', typeof exif); - assert.strictEqual('object', typeof exif.image); - assert.strictEqual('number', typeof exif.image.XResolution); + assert.strictEqual('object', typeof exif.Image); + assert.strictEqual('number', typeof exif.Image.XResolution); // ICC assert.strictEqual('object', typeof metadata.icc); assert.strictEqual(true, metadata.icc instanceof Buffer); @@ -64,8 +70,8 @@ describe('Image metadata', function () { }); }); - it('JPEG with IPTC/XMP', function (done) { - sharp(fixtures.inputJpgWithIptcAndXmp).metadata(function (err, metadata) { + it('JPEG with IPTC/XMP', (_t, done) => { + sharp(fixtures.inputJpgWithIptcAndXmp).metadata((err, metadata) => { if (err) throw err; // IPTC assert.strictEqual('object', typeof metadata.iptc); @@ -77,12 +83,13 @@ describe('Image metadata', function () { assert.strictEqual(true, metadata.xmp instanceof Buffer); assert.strictEqual(12466, metadata.xmp.byteLength); assert.strictEqual(metadata.xmp.indexOf(Buffer.from('<?xpacket begin="')), 0); + assert(metadata.xmpAsString.startsWith('<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>')); done(); }); }); - it('TIFF', function (done) { - sharp(fixtures.inputTiff).metadata(function (err, metadata) { + it('TIFF', (_t, done) => { + sharp(fixtures.inputTiff).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('tiff', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -97,14 +104,19 @@ describe('Image metadata', function () { assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(1, metadata.orientation); + assert.strictEqual(2464, metadata.autoOrient.width); + assert.strictEqual(3248, metadata.autoOrient.height); assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); + assert.strictEqual('undefined', typeof metadata.xmp); + assert.strictEqual('undefined', typeof metadata.xmpAsString); + assert.strictEqual('inch', metadata.resolutionUnit); done(); }); }); - it('Multipage TIFF', function (done) { - sharp(fixtures.inputTiffMultipage).metadata(function (err, metadata) { + it('Multipage TIFF', (_t, done) => { + sharp(fixtures.inputTiffMultipage).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('tiff', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -126,8 +138,8 @@ describe('Image metadata', function () { }); }); - it('PNG', function (done) { - sharp(fixtures.inputPng).metadata(function (err, metadata) { + it('PNG', (_t, done) => { + sharp(fixtures.inputPng).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('png', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -142,14 +154,41 @@ describe('Image metadata', function () { assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual('undefined', typeof metadata.orientation); + assert.strictEqual(2809, metadata.autoOrient.width); + assert.strictEqual(2074, metadata.autoOrient.height); assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); done(); }); }); - it('Transparent PNG', function (done) { - sharp(fixtures.inputPngWithTransparency).metadata(function (err, metadata) { + it('PNG with comment', (_t, done) => { + sharp(fixtures.inputPngTestJoinChannel).metadata((err, metadata) => { + if (err) throw err; + assert.strictEqual('png', metadata.format); + assert.strictEqual('undefined', typeof metadata.size); + assert.strictEqual(320, metadata.width); + assert.strictEqual(240, metadata.height); + assert.strictEqual('b-w', metadata.space); + assert.strictEqual(1, metadata.channels); + assert.strictEqual('uchar', metadata.depth); + assert.strictEqual(72, metadata.density); + assert.strictEqual('undefined', typeof metadata.chromaSubsampling); + assert.strictEqual(false, metadata.isProgressive); + assert.strictEqual(false, metadata.hasProfile); + assert.strictEqual(false, metadata.hasAlpha); + assert.strictEqual('undefined', typeof metadata.orientation); + assert.strictEqual('undefined', typeof metadata.exif); + assert.strictEqual('undefined', typeof metadata.icc); + assert.strictEqual(1, metadata.comments.length); + assert.strictEqual('Comment', metadata.comments[0].keyword); + assert.strictEqual('Created with GIMP', metadata.comments[0].text); + done(); + }); + }); + + it('Transparent PNG', (_t, done) => { + sharp(fixtures.inputPngWithTransparency).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('png', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -170,8 +209,58 @@ describe('Image metadata', function () { }); }); - it('WebP', function (done) { - sharp(fixtures.inputWebP).metadata(function (err, metadata) { + it('PNG with greyscale bKGD chunk - 8 bit', async () => { + const data = await sharp(fixtures.inputPng8BitGreyBackground).metadata(); + assert.deepStrictEqual(data, { + background: { + gray: 0 + }, + bitsPerSample: 8, + channels: 2, + density: 72, + depth: 'uchar', + format: 'png', + hasAlpha: true, + hasProfile: false, + height: 32, + isPalette: false, + isProgressive: false, + space: 'b-w', + width: 32, + autoOrient: { + width: 32, + height: 32 + } + }); + }); + + it('PNG with greyscale bKGD chunk - 16 bit', async () => { + const data = await sharp(fixtures.inputPng16BitGreyBackground).metadata(); + assert.deepStrictEqual(data, { + background: { + gray: 67 + }, + bitsPerSample: 16, + channels: 2, + density: 72, + depth: 'ushort', + format: 'png', + hasAlpha: true, + hasProfile: false, + height: 32, + isPalette: false, + isProgressive: false, + space: 'grey16', + width: 32, + autoOrient: { + width: 32, + height: 32 + } + }); + }); + + it('WebP', (_t, done) => { + sharp(fixtures.inputWebP).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('webp', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -194,6 +283,29 @@ describe('Image metadata', function () { it('Animated WebP', () => sharp(fixtures.inputWebPAnimated) + .metadata() + .then(({ + format, width, height, space, channels, depth, + isProgressive, pages, loop, delay, hasProfile, + hasAlpha + }) => { + assert.strictEqual(format, 'webp'); + assert.strictEqual(width, 80); + assert.strictEqual(height, 80); + assert.strictEqual(space, 'srgb'); + assert.strictEqual(channels, 4); + assert.strictEqual(depth, 'uchar'); + assert.strictEqual(isProgressive, false); + assert.strictEqual(pages, 9); + assert.strictEqual(loop, 0); + assert.deepStrictEqual(delay, [120, 120, 90, 120, 120, 90, 120, 90, 30]); + assert.strictEqual(hasProfile, false); + assert.strictEqual(hasAlpha, true); + }) + ); + + it('Animated WebP with all pages', () => + sharp(fixtures.inputWebPAnimated, { pages: -1 }) .metadata() .then(({ format, width, height, space, channels, depth, @@ -202,7 +314,7 @@ describe('Image metadata', function () { }) => { assert.strictEqual(format, 'webp'); assert.strictEqual(width, 80); - assert.strictEqual(height, 80); + assert.strictEqual(height, 720); assert.strictEqual(space, 'srgb'); assert.strictEqual(channels, 4); assert.strictEqual(depth, 'uchar'); @@ -221,8 +333,8 @@ describe('Image metadata', function () { .metadata() .then(({ format, width, height, space, channels, depth, - isProgressive, pages, pageHeight, loop, delay, - hasProfile, hasAlpha + isProgressive, pages, loop, delay, hasProfile, + hasAlpha }) => { assert.strictEqual(format, 'webp'); assert.strictEqual(width, 370); @@ -232,7 +344,6 @@ describe('Image metadata', function () { assert.strictEqual(depth, 'uchar'); assert.strictEqual(isProgressive, false); assert.strictEqual(pages, 10); - assert.strictEqual(pageHeight, 285); assert.strictEqual(loop, 3); assert.deepStrictEqual(delay, [...Array(9).fill(3000), 15000]); assert.strictEqual(hasProfile, false); @@ -240,8 +351,8 @@ describe('Image metadata', function () { }) ); - it('GIF via giflib', function (done) { - sharp(fixtures.inputGif).metadata(function (err, metadata) { + it('GIF', (_t, done) => { + sharp(fixtures.inputGif).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('gif', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -253,27 +364,26 @@ describe('Image metadata', function () { assert.strictEqual('undefined', typeof metadata.chromaSubsampling); assert.strictEqual(false, metadata.isProgressive); assert.strictEqual(false, metadata.hasProfile); - assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); + assert.deepStrictEqual(metadata.background, { r: 138, g: 148, b: 102 }); done(); }); }); - it('GIF grey+alpha via giflib', function (done) { - sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) { + it('GIF grey+alpha', (_t, done) => { + sharp(fixtures.inputGifGreyPlusAlpha).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('gif', metadata.format); assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual(2, metadata.width); assert.strictEqual(1, metadata.height); - assert.strictEqual(2, metadata.channels); + assert.strictEqual(4, metadata.channels); assert.strictEqual('uchar', metadata.depth); assert.strictEqual('undefined', typeof metadata.density); assert.strictEqual('undefined', typeof metadata.chromaSubsampling); assert.strictEqual(false, metadata.isProgressive); assert.strictEqual(false, metadata.hasProfile); - assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); @@ -286,7 +396,7 @@ describe('Image metadata', function () { .metadata() .then(({ format, width, height, space, channels, depth, - isProgressive, pages, pageHeight, loop, delay, + isProgressive, pages, loop, delay, background, hasProfile, hasAlpha }) => { assert.strictEqual(format, 'gif'); @@ -297,9 +407,9 @@ describe('Image metadata', function () { assert.strictEqual(depth, 'uchar'); assert.strictEqual(isProgressive, false); assert.strictEqual(pages, 30); - assert.strictEqual(pageHeight, 80); assert.strictEqual(loop, 0); assert.deepStrictEqual(delay, Array(30).fill(30)); + assert.deepStrictEqual(background, { r: 0, g: 0, b: 0 }); assert.strictEqual(hasProfile, false); assert.strictEqual(hasAlpha, true); }) @@ -310,8 +420,8 @@ describe('Image metadata', function () { .metadata() .then(({ format, width, height, space, channels, depth, - isProgressive, pages, pageHeight, loop, delay, - hasProfile, hasAlpha + isProgressive, pages, loop, delay, hasProfile, + hasAlpha }) => { assert.strictEqual(format, 'gif'); assert.strictEqual(width, 370); @@ -321,7 +431,6 @@ describe('Image metadata', function () { assert.strictEqual(depth, 'uchar'); assert.strictEqual(isProgressive, false); assert.strictEqual(pages, 10); - assert.strictEqual(pageHeight, 285); assert.strictEqual(loop, 3); assert.deepStrictEqual(delay, [...Array(9).fill(3000), 15000]); assert.strictEqual(hasProfile, false); @@ -350,8 +459,8 @@ describe('Image metadata', function () { }) ); - it('File in, Promise out', function (done) { - sharp(fixtures.inputJpg).metadata().then(function (metadata) { + it('File in, Promise out', (_t, done) => { + sharp(fixtures.inputJpg).metadata().then((metadata) => { assert.strictEqual('jpeg', metadata.format); assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual(2725, metadata.width); @@ -371,19 +480,33 @@ describe('Image metadata', function () { }); }); - it('Non-existent file in, Promise out', function (done) { - sharp('fail').metadata().then(function (metadata) { - throw new Error('Non-existent file'); - }, function (err) { - assert.ok(!!err); - done(); - }); + it('Non-existent file in, Promise out', async () => + assert.rejects( + () => sharp('fail').metadata(), + (err) => { + assert.strictEqual(err.message, 'Input file is missing: fail'); + assert(err.stack.includes('at Sharp.metadata')); + assert(err.stack.includes(__filename)); + return true; + } + ) + ); + + it('Invalid stream in, callback out', (_t, done) => { + fs.createReadStream(__filename).pipe( + sharp().metadata((err) => { + assert.strictEqual(err.message, 'Input buffer contains unsupported image format'); + assert(err.stack.includes('at Sharp.metadata')); + assert(err.stack.includes(__filename)); + done(); + }) + ); }); - it('Stream in, Promise out', function (done) { + it('Stream in, Promise out', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); const pipeline = sharp(); - pipeline.metadata().then(function (metadata) { + pipeline.metadata().then((metadata) => { assert.strictEqual('jpeg', metadata.format); assert.strictEqual(829183, metadata.size); assert.strictEqual(2725, metadata.width); @@ -400,7 +523,7 @@ describe('Image metadata', function () { assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); done(); - }).catch(done); + }).catch(_t, done); readable.pipe(pipeline); }); @@ -418,9 +541,22 @@ describe('Image metadata', function () { ); }); - it('Stream', function (done) { + it('Stream in, finish event fires before metadata is requested', (_t, done) => { + const create = { width: 1, height: 1, channels: 3, background: 'red' }; + const image1 = sharp({ create }).png().pipe(sharp()); + const image2 = sharp({ create }).png().pipe(sharp()); + setTimeout(async () => { + const data1 = await image1.metadata(); + assert.strictEqual('png', data1.format); + const data2 = await image2.metadata(); + assert.strictEqual('png', data2.format); + done(); + }, 500); + }); + + it('Stream', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); - const pipeline = sharp().metadata(function (err, metadata) { + const pipeline = sharp().metadata((err, metadata) => { if (err) throw err; assert.strictEqual('jpeg', metadata.format); assert.strictEqual(829183, metadata.size); @@ -442,9 +578,9 @@ describe('Image metadata', function () { readable.pipe(pipeline); }); - it('Resize to half width using metadata', function (done) { + it('Resize to half width using metadata', (_t, done) => { const image = sharp(fixtures.inputJpg); - image.metadata(function (err, metadata) { + image.metadata((err, metadata) => { if (err) throw err; assert.strictEqual('jpeg', metadata.format); assert.strictEqual('undefined', typeof metadata.size); @@ -461,7 +597,7 @@ describe('Image metadata', function () { assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); - image.resize(Math.floor(metadata.width / 2)).toBuffer(function (err, data, info) { + image.resize(Math.floor(metadata.width / 2)).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(1362, info.width); @@ -471,23 +607,27 @@ describe('Image metadata', function () { }); }); - it('Keep EXIF metadata and add sRGB profile after a resize', function (done) { + it('Keep EXIF metadata and add sRGB profile after a resize', (_t, done) => { sharp(fixtures.inputJpgWithExif) .resize(320, 240) .withMetadata() - .toBuffer(function (err, buffer) { + .toBuffer((err, buffer) => { if (err) throw err; - sharp(buffer).metadata(function (err, metadata) { + sharp(buffer).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(true, metadata.hasProfile); assert.strictEqual(8, metadata.orientation); + assert.strictEqual(320, metadata.width); + assert.strictEqual(240, metadata.height); + assert.strictEqual(240, metadata.autoOrient.width); + assert.strictEqual(320, metadata.autoOrient.height); assert.strictEqual('object', typeof metadata.exif); assert.strictEqual(true, metadata.exif instanceof Buffer); // EXIF const exif = exifReader(metadata.exif); assert.strictEqual('object', typeof exif); - assert.strictEqual('object', typeof exif.image); - assert.strictEqual('number', typeof exif.image.XResolution); + assert.strictEqual('object', typeof exif.Image); + assert.strictEqual('number', typeof exif.Image.XResolution); // ICC assert.strictEqual('object', typeof metadata.icc); assert.strictEqual(true, metadata.icc instanceof Buffer); @@ -501,14 +641,111 @@ describe('Image metadata', function () { }); }); - it('Apply CMYK output ICC profile', function (done) { + it('keep existing ICC profile', async () => { + const data = await sharp(fixtures.inputJpgWithExif) + .keepIccProfile() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + const { description } = icc.parse(metadata.icc); + assert.strictEqual(description, 'Generic RGB Profile'); + }); + + it('keep existing CMYK input profile for CMYK output', async () => { + const data = await sharp(fixtures.inputJpgWithCmykProfile) + .keepIccProfile() + .toColourspace('cmyk') + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.channels, 4); + const { description } = icc.parse(metadata.icc); + assert.strictEqual(description, 'U.S. Web Coated (SWOP) v2'); + }); + + it('transform with but discard existing RGB input profile for CMYK output', async () => { + const data = await sharp(fixtures.inputJpgWithExif) + .keepIccProfile() + .toColourspace('cmyk') + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.channels, 4); + assert.strictEqual(metadata.icc, undefined); + }); + + it('keep existing ICC profile, avoid colour transform', async () => { + const [r, g, b] = await sharp(fixtures.inputPngWithProPhotoProfile) + .keepIccProfile() + .raw() + .toBuffer(); + + assert.strictEqual(r, 131); + assert.strictEqual(g, 141); + assert.strictEqual(b, 192); + }); + + it('keep existing CMYK ICC profile', async () => { + const data = await sharp(fixtures.inputJpgWithCmykProfile) + .pipelineColourspace('cmyk') + .toColourspace('cmyk') + .keepIccProfile() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.channels, 4); + const { description } = icc.parse(metadata.icc); + assert.strictEqual(description, 'U.S. Web Coated (SWOP) v2'); + }); + + it('transform to ICC profile and attach', async () => { + const data = await sharp({ create }) + .png() + .withIccProfile('p3', { attach: true }) + .toBuffer(); + + const metadata = await sharp(data).metadata(); + const { description } = icc.parse(metadata.icc); + assert.strictEqual(description, 'sP3C'); + }); + + it('transform to ICC profile but do not attach', async () => { + const data = await sharp({ create }) + .png() + .withIccProfile('p3', { attach: false }) + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(3, metadata.channels); + assert.strictEqual(undefined, metadata.icc); + }); + + it('transform to invalid ICC profile emits warning', async () => { + const img = sharp({ create }) + .png() + .withIccProfile(fixtures.path('invalid-illuminant.icc')); + + const warningsEmitted = []; + img.on('warning', (warning) => { + warningsEmitted.push(warning); + }); + + const data = await img.toBuffer(); + assert.strict(warningsEmitted.includes('Invalid profile')); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(3, metadata.channels); + assert.strictEqual(undefined, metadata.icc); + }); + + it('Apply CMYK output ICC profile', (_t, done) => { const output = fixtures.path('output.icc-cmyk.jpg'); sharp(fixtures.inputJpg) .resize(64) - .withMetadata({ icc: 'cmyk' }) - .toFile(output, function (err, info) { + .withIccProfile('cmyk') + .toFile(output, (err) => { if (err) throw err; - sharp(output).metadata(function (err, metadata) { + sharp(output).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(true, metadata.hasProfile); assert.strictEqual('cmyk', metadata.space); @@ -522,18 +759,18 @@ describe('Image metadata', function () { assert.strictEqual('Relative', profile.intent); assert.strictEqual('Printer', profile.deviceClass); }); - fixtures.assertSimilar(output, fixtures.path('expected/icc-cmyk.jpg'), { threshold: 0 }, done); + fixtures.assertSimilar(output, fixtures.expected('icc-cmyk.jpg'), { threshold: 1 }, done); }); }); - it('Apply custom output ICC profile', function (done) { + it('Apply custom output ICC profile', (_t, done) => { const output = fixtures.path('output.hilutite.jpg'); sharp(fixtures.inputJpg) .resize(64) - .withMetadata({ icc: fixtures.path('hilutite.icm') }) - .toFile(output, function (err, info) { + .withIccProfile(fixtures.path('hilutite.icm')) + .toFile(output, (err) => { if (err) throw err; - fixtures.assertMaxColourDistance(output, fixtures.path('expected/hilutite.jpg'), 9); + fixtures.assertMaxColourDistance(output, fixtures.expected('hilutite.jpg'), 9); done(); }); }); @@ -552,8 +789,8 @@ describe('Image metadata', function () { // EXIF const exif = exifReader(metadata.exif); assert.strictEqual('object', typeof exif); - assert.strictEqual('object', typeof exif.image); - assert.strictEqual('number', typeof exif.image.XResolution); + assert.strictEqual('object', typeof exif.Image); + assert.strictEqual('number', typeof exif.Image.XResolution); // ICC assert.strictEqual('object', typeof metadata.icc); assert.strictEqual(true, metadata.icc instanceof Buffer); @@ -566,13 +803,12 @@ describe('Image metadata', function () { ) ); - it('Remove EXIF metadata after a resize', function (done) { + it('Remove EXIF metadata after a resize', (_t, done) => { sharp(fixtures.inputJpgWithExif) .resize(320, 240) - .withMetadata(false) - .toBuffer(function (err, buffer) { + .toBuffer((err, buffer) => { if (err) throw err; - sharp(buffer).metadata(function (err, metadata) { + sharp(buffer).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(false, metadata.hasProfile); assert.strictEqual('undefined', typeof metadata.orientation); @@ -583,12 +819,12 @@ describe('Image metadata', function () { }); }); - it('Remove metadata from PNG output', function (done) { + it('Remove metadata from PNG output', (_t, done) => { sharp(fixtures.inputJpgWithExif) .png() - .toBuffer(function (err, buffer) { + .toBuffer((err, buffer) => { if (err) throw err; - sharp(buffer).metadata(function (err, metadata) { + sharp(buffer).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(false, metadata.hasProfile); assert.strictEqual('undefined', typeof metadata.orientation); @@ -599,55 +835,82 @@ describe('Image metadata', function () { }); }); - it('chromaSubsampling 4:4:4:4 CMYK JPEG', function () { - return sharp(fixtures.inputJpgWithCmykProfile) + it('Add EXIF metadata to JPEG', async () => { + const data = await sharp({ create }) + .jpeg() + .withMetadata({ + exif: { + IFD0: { Software: 'sharp' }, + IFD2: { ExposureTime: '0.2' } + } + }) + .toBuffer(); + + const { exif } = await sharp(data).metadata(); + const parsedExif = exifReader(exif); + assert.strictEqual(parsedExif.Image.Software, 'sharp'); + assert.strictEqual(parsedExif.Photo.ExposureTime, 0.2); + }); + + it('Set density of JPEG', async () => { + const data = await sharp({ create }) + .withMetadata({ + density: 300 + }) + .jpeg() + .toBuffer(); + + const { density } = await sharp(data).metadata(); + assert.strictEqual(density, 300); + }); + + it('Set density of PNG', async () => { + const data = await sharp({ create }) + .withMetadata({ + density: 96 + }) + .png() + .toBuffer(); + + const { density } = await sharp(data).metadata(); + assert.strictEqual(density, 96); + }); + + it('chromaSubsampling 4:4:4:4 CMYK JPEG', () => sharp(fixtures.inputJpgWithCmykProfile) .metadata() - .then(function (metadata) { + .then((metadata) => { assert.strictEqual('4:4:4:4', metadata.chromaSubsampling); - }); - }); + })); - it('chromaSubsampling 4:4:4 RGB JPEG', function () { - return sharp(fixtures.inputJpg) + it('chromaSubsampling 4:4:4 RGB JPEG', () => sharp(fixtures.inputJpg) .resize(10, 10) .jpeg({ chromaSubsampling: '4:4:4' }) .toBuffer() - .then(function (data) { - return sharp(data) + .then((data) => sharp(data) .metadata() - .then(function (metadata) { + .then((metadata) => { assert.strictEqual('4:4:4', metadata.chromaSubsampling); - }); - }); - }); + }))); - it('isProgressive JPEG', function () { - return sharp(fixtures.inputJpg) + it('isProgressive JPEG', () => sharp(fixtures.inputJpg) .resize(10, 10) .jpeg({ progressive: true }) .toBuffer() - .then(function (data) { - return sharp(data) + .then((data) => sharp(data) .metadata() - .then(function (metadata) { + .then((metadata) => { assert.strictEqual(true, metadata.isProgressive); - }); - }); - }); + }))); - it('isProgressive PNG', function () { - return sharp(fixtures.inputJpg) + it('isProgressive PNG', () => sharp(fixtures.inputJpg) .resize(10, 10) .png({ progressive: true }) .toBuffer() - .then(function (data) { - return sharp(data) + .then((data) => sharp(data) .metadata() - .then(function (metadata) { + .then((metadata) => { assert.strictEqual(true, metadata.isProgressive); - }); - }); - }); + }))); it('16-bit TIFF with TIFFTAG_PHOTOSHOP metadata', () => sharp(fixtures.inputTifftagPhotoshop) @@ -664,58 +927,421 @@ describe('Image metadata', function () { }) ); - it('File input with corrupt header fails gracefully', function (done) { + it('AVIF', async () => { + const metadata = await sharp(fixtures.inputAvif).metadata(); + assert.deepStrictEqual(metadata, { + format: 'heif', + width: 2048, + height: 858, + space: 'srgb', + channels: 3, + depth: 'uchar', + isProgressive: false, + isPalette: false, + bitsPerSample: 8, + pages: 1, + pagePrimary: 0, + compression: 'av1', + hasProfile: false, + hasAlpha: false, + autoOrient: { + width: 2048, + height: 858 + } + }); + }); + + it('withMetadata adds default sRGB profile', async () => { + const data = await sharp(fixtures.inputJpg) + .resize(32, 24) + .withMetadata() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + const { colorSpace, deviceClass, intent } = icc.parse(metadata.icc); + assert.strictEqual(colorSpace, 'RGB'); + assert.strictEqual(deviceClass, 'Monitor'); + assert.strictEqual(intent, 'Perceptual'); + }); + + it('withMetadata adds default sRGB profile to RGB16', async () => { + const data = await sharp({ create }) + .toColorspace('rgb16') + .png() + .withMetadata() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.depth, 'ushort'); + + const { description } = icc.parse(metadata.icc); + assert.strictEqual(description, 'sRGB'); + }); + + it('withMetadata adds P3 profile to 16-bit PNG', async () => { + const data = await sharp({ create }) + .toColorspace('rgb16') + .png() + .withMetadata({ icc: 'p3' }) + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual(metadata.depth, 'ushort'); + + const { description } = icc.parse(metadata.icc); + assert.strictEqual(description, 'sP3C'); + }); + + it('File input with corrupt header fails gracefully', (_t, done) => { sharp(fixtures.inputJpgWithCorruptHeader) - .metadata(function (err) { + .metadata((err) => { assert.strictEqual(true, !!err); - assert.ok(err.message.includes('Input file has corrupt header: VipsJpeg: Premature end of'), err); + assert.ok(err.message.includes('Input file has corrupt header: VipsJpeg: premature end of'), err); done(); }); }); - it('Buffer input with corrupt header fails gracefully', function (done) { + it('Buffer input with corrupt header fails gracefully', (_t, done) => { sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) - .metadata(function (err) { + .metadata((err) => { assert.strictEqual(true, !!err); - assert.ok(err.message.includes('Input buffer has corrupt header: VipsJpeg: Premature end of'), err); + assert.ok(err.message.includes('Input buffer has corrupt header: VipsJpeg: premature end of'), err); done(); }); }); - it('Unsupported lossless JPEG passes underlying error message', function (done) { - sharp(fixtures.inputJpgLossless) - .metadata(function (err) { - assert.strictEqual(true, !!err); - assert.strictEqual(true, /Input file has corrupt header: VipsJpeg: Unsupported JPEG process: SOF type 0xc3/.test(err.message)); - done(); - }); + it('Lossless JPEG', async () => { + const metadata = await sharp(fixtures.inputJpgLossless).metadata(); + assert.deepStrictEqual(metadata, { + format: 'jpeg', + width: 227, + height: 149, + space: 'srgb', + channels: 3, + depth: 'uchar', + density: 72, + chromaSubsampling: '4:4:4', + isProgressive: false, + isPalette: false, + hasProfile: false, + hasAlpha: false, + autoOrient: { width: 227, height: 149 } + }); + }); + + it('keepExif maintains all EXIF metadata', async () => { + const data1 = await sharp({ create }) + .withExif({ + IFD0: { + Copyright: 'Test 1', + Software: 'sharp' + } + }) + .jpeg() + .toBuffer(); + + const data2 = await sharp(data1) + .keepExif() + .toBuffer(); + + const md2 = await sharp(data2).metadata(); + const exif2 = exifReader(md2.exif); + assert.strictEqual(exif2.Image.Copyright, 'Test 1'); + assert.strictEqual(exif2.Image.Software, 'sharp'); + }); + + it('withExif replaces all EXIF metadata', async () => { + const data1 = await sharp({ create }) + .withExif({ + IFD0: { + Copyright: 'Test 1', + Software: 'sharp' + } + }) + .jpeg() + .toBuffer(); + + const md1 = await sharp(data1).metadata(); + const exif1 = exifReader(md1.exif); + assert.strictEqual(exif1.Image.Copyright, 'Test 1'); + assert.strictEqual(exif1.Image.Software, 'sharp'); + + const data2 = await sharp(data1) + .withExif({ + IFD0: { + Copyright: 'Test 2' + } + }) + .toBuffer(); + + const md2 = await sharp(data2).metadata(); + const exif2 = exifReader(md2.exif); + assert.strictEqual(exif2.Image.Copyright, 'Test 2'); + assert.strictEqual(exif2.Image.Software, undefined); }); - describe('Invalid withMetadata parameters', function () { - it('String orientation', function () { - assert.throws(function () { + it('withExifMerge merges all EXIF metadata', async () => { + const data1 = await sharp({ create }) + .withExif({ + IFD0: { + Copyright: 'Test 1' + } + }) + .jpeg() + .toBuffer(); + + const md1 = await sharp(data1).metadata(); + const exif1 = exifReader(md1.exif); + assert.strictEqual(exif1.Image.Copyright, 'Test 1'); + assert.strictEqual(exif1.Image.Software, undefined); + + const data2 = await sharp(data1) + .withExifMerge({ + IFD0: { + Copyright: 'Test 2', + Software: 'sharp' + + } + }) + .toBuffer(); + + const md2 = await sharp(data2).metadata(); + const exif2 = exifReader(md2.exif); + assert.strictEqual(exif2.Image.Copyright, 'Test 2'); + assert.strictEqual(exif2.Image.Software, 'sharp'); + }); + + describe('XMP metadata tests', () => { + it('withMetadata preserves existing XMP metadata from input', async () => { + const data = await sharp(fixtures.inputJpgWithIptcAndXmp) + .resize(320, 240) + .withMetadata() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual('object', typeof metadata.xmp); + assert.strictEqual(true, metadata.xmp instanceof Buffer); + assert.strictEqual(true, metadata.xmp.length > 0); + // Check that XMP starts with the expected XML declaration + assert.strictEqual(metadata.xmp.indexOf(Buffer.from('<?xpacket begin="')), 0); + }); + + it('keepXmp preserves existing XMP metadata from input', async () => { + const data = await sharp(fixtures.inputJpgWithIptcAndXmp) + .resize(320, 240) + .keepXmp() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual('object', typeof metadata.xmp); + assert.strictEqual(true, metadata.xmp instanceof Buffer); + assert.strictEqual(true, metadata.xmp.length > 0); + // Check that XMP starts with the expected XML declaration + assert.strictEqual(metadata.xmp.indexOf(Buffer.from('<?xpacket begin="')), 0); + }); + + it('withXmp with custom XMP replaces existing XMP', async () => { + const customXmp = '<?xml version="1.0"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:creator><rdf:Seq><rdf:li>Test Creator</rdf:li></rdf:Seq></dc:creator><dc:title><rdf:Alt><rdf:li xml:lang="x-default">Test Title</rdf:li></rdf:Alt></dc:title></rdf:Description></rdf:RDF></x:xmpmeta>'; + + const data = await sharp(fixtures.inputJpgWithIptcAndXmp) + .resize(320, 240) + .withXmp(customXmp) + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual('object', typeof metadata.xmp); + assert.strictEqual(true, metadata.xmp instanceof Buffer); + + // Check that the XMP contains our custom content + const xmpString = metadata.xmp.toString(); + assert.strictEqual(true, xmpString.includes('Test Creator')); + assert.strictEqual(true, xmpString.includes('Test Title')); + }); + + it('withXmp with custom XMP buffer on image without existing XMP', async () => { + const customXmp = '<?xml version="1.0"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:description><rdf:Alt><rdf:li xml:lang="x-default">Added via Sharp</rdf:li></rdf:Alt></dc:description></rdf:Description></rdf:RDF></x:xmpmeta>'; + + const data = await sharp(fixtures.inputJpg) + .resize(320, 240) + .withXmp(customXmp) + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual('object', typeof metadata.xmp); + assert.strictEqual(true, metadata.xmp instanceof Buffer); + + // Check that the XMP contains our custom content + const xmpString = metadata.xmp.toString(); + assert.strictEqual(true, xmpString.includes('Added via Sharp')); + }); + + it('withXmp with valid XMP metadata for different image formats', async () => { + const customXmp = '<?xml version="1.0"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:subject><rdf:Bag><rdf:li>test</rdf:li><rdf:li>metadata</rdf:li></rdf:Bag></dc:subject></rdf:Description></rdf:RDF></x:xmpmeta>'; + + // Test with JPEG output + const jpegData = await sharp(fixtures.inputJpg) + .resize(100, 100) + .jpeg() + .withXmp(customXmp) + .toBuffer(); + + const jpegMetadata = await sharp(jpegData).metadata(); + assert.strictEqual('object', typeof jpegMetadata.xmp); + assert.strictEqual(true, jpegMetadata.xmp instanceof Buffer); + assert.strictEqual(true, jpegMetadata.xmp.toString().includes('test')); + + // Test with PNG output (PNG should also support XMP metadata) + const pngData = await sharp(fixtures.inputJpg) + .resize(100, 100) + .png() + .withXmp(customXmp) + .toBuffer(); + + const pngMetadata = await sharp(pngData).metadata(); + // PNG format should preserve XMP metadata when using withXmp + assert.strictEqual('object', typeof pngMetadata.xmp); + assert.strictEqual(true, pngMetadata.xmp instanceof Buffer); + assert.strictEqual(true, pngMetadata.xmp.toString().includes('test')); + + // Test with WebP output (WebP should also support XMP metadata) + const webpData = await sharp(fixtures.inputJpg) + .resize(100, 100) + .webp() + .withXmp(customXmp) + .toBuffer(); + + const webpMetadata = await sharp(webpData).metadata(); + // WebP format should preserve XMP metadata when using withXmp + assert.strictEqual('object', typeof webpMetadata.xmp); + assert.strictEqual(true, webpMetadata.xmp instanceof Buffer); + assert.strictEqual(true, webpMetadata.xmp.toString().includes('test')); + }); + + it('XMP metadata persists through multiple operations', async () => { + const customXmp = '<?xml version="1.0"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:identifier>persistent-test</dc:identifier></rdf:Description></rdf:RDF></x:xmpmeta>'; + + const data = await sharp(fixtures.inputJpg) + .resize(320, 240) + .withXmp(customXmp) + .rotate(90) + .blur(1) + .sharpen() + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual('object', typeof metadata.xmp); + assert.strictEqual(true, metadata.xmp instanceof Buffer); + assert.strictEqual(true, metadata.xmp.toString().includes('persistent-test')); + }); + + it('withXmp XMP works with WebP format specifically', async () => { + const webpXmp = '<?xml version="1.0"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:creator><rdf:Seq><rdf:li>WebP Creator</rdf:li></rdf:Seq></dc:creator><dc:format>image/webp</dc:format></rdf:Description></rdf:RDF></x:xmpmeta>'; + + const data = await sharp(fixtures.inputJpg) + .resize(120, 80) + .webp({ quality: 80 }) + .withXmp(webpXmp) + .toBuffer(); + + const metadata = await sharp(data).metadata(); + assert.strictEqual('webp', metadata.format); + assert.strictEqual('object', typeof metadata.xmp); + assert.strictEqual(true, metadata.xmp instanceof Buffer); + + const xmpString = metadata.xmp.toString(); + assert.strictEqual(true, xmpString.includes('WebP Creator')); + assert.strictEqual(true, xmpString.includes('image/webp')); + }); + + it('withXmp XMP validation - non-string input', () => { + assert.throws( + () => sharp().withXmp(123), + /Expected non-empty string for xmp but received 123 of type number/ + ); + }); + + it('withXmp XMP validation - null input', () => { + assert.throws( + () => sharp().withXmp(null), + /Expected non-empty string for xmp but received null of type object/ + ); + }); + + it('withXmp XMP validation - empty string', () => { + assert.throws( + () => sharp().withXmp(''), + /Expected non-empty string for xmp/ + ); + }); + }); + + describe('Invalid parameters', () => { + it('String orientation', () => { + assert.throws(() => { sharp().withMetadata({ orientation: 'zoinks' }); }); }); - it('Negative orientation', function () { - assert.throws(function () { + it('Negative orientation', () => { + assert.throws(() => { sharp().withMetadata({ orientation: -1 }); }); }); - it('Zero orientation', function () { - assert.throws(function () { + it('Zero orientation', () => { + assert.throws(() => { sharp().withMetadata({ orientation: 0 }); }); }); - it('Too large orientation', function () { - assert.throws(function () { + it('Too large orientation', () => { + assert.throws(() => { sharp().withMetadata({ orientation: 9 }); }); }); - it('Non string icc', function () { - assert.throws(function () { + it('Non-numeric density', () => { + assert.throws(() => { + sharp().withMetadata({ density: '1' }); + }); + }); + it('Negative density', () => { + assert.throws(() => { + sharp().withMetadata({ density: -1 }); + }); + }); + it('Non string icc', () => { + assert.throws(() => { sharp().withMetadata({ icc: true }); }); }); + it('Non object exif', () => { + assert.throws(() => { + sharp().withMetadata({ exif: false }); + }); + }); + it('Non string value in object exif', () => { + assert.throws(() => { + sharp().withMetadata({ exif: { ifd0: false } }); + }); + }); + it('Non string value in nested object exif', () => { + assert.throws(() => { + sharp().withMetadata({ exif: { ifd0: { fail: false } } }); + }); + }); + it('withIccProfile invalid profile', () => { + assert.throws( + () => sharp().withIccProfile(false), + /Expected string for icc but received false of type boolean/ + ); + }); + it('withIccProfile missing attach', () => { + assert.doesNotThrow( + () => sharp().withIccProfile('test', {}) + ); + }); + it('withIccProfile invalid attach', () => { + assert.throws( + () => sharp().withIccProfile('test', { attach: 1 }), + /Expected boolean for attach but received 1 of type number/ + ); + }); }); }); diff --git a/test/unit/modulate.js b/test/unit/modulate.js index 8cd6b5ab3..40c5b83f1 100644 --- a/test/unit/modulate.js +++ b/test/unit/modulate.js @@ -1,11 +1,15 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ const sharp = require('../../'); -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const fixtures = require('../fixtures'); -describe('Modulate', function () { - describe('Invalid options', function () { +describe('Modulate', () => { + describe('Invalid options', () => { [ null, undefined, @@ -18,10 +22,12 @@ describe('Modulate', function () { { saturation: null }, { hue: '50deg' }, { hue: 1.5 }, - { hue: null } - ].forEach(function (options) { - it('should throw', function () { - assert.throws(function () { + { hue: null }, + { lightness: '+50' }, + { lightness: null } + ].forEach((options) => { + it('should throw', () => { + assert.throws(() => { sharp(fixtures.inputJpg).modulate(options); }); }); @@ -108,6 +114,22 @@ describe('Modulate', function () { assert.deepStrictEqual({ r: 127, g: 83, b: 81 }, { r, g, b }); }); + it('should be able to lighten', async () => { + const [r, g, b] = await sharp({ + create: { + width: 1, + height: 1, + channels: 3, + background: { r: 153, g: 68, b: 68 } + } + }) + .modulate({ lightness: 10 }) + .raw() + .toBuffer(); + + assert.deepStrictEqual({ r: 182, g: 93, b: 92 }, { r, g, b }); + }); + it('should be able to modulate all channels', async () => { const [r, g, b] = await sharp({ create: { @@ -157,7 +179,7 @@ describe('Modulate', function () { .png({ compressionLevel: 0 }) .toFile(actual) .then(() => { - fixtures.assertMaxColourDistance(actual, expected); + fixtures.assertMaxColourDistance(actual, expected, 3); }); }); }); diff --git a/test/unit/negate.js b/test/unit/negate.js index aff4fd909..87196a592 100644 --- a/test/unit/negate.js +++ b/test/unit/negate.js @@ -1,16 +1,20 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Negate', function () { - it('negate (jpeg)', function (done) { +describe('Negate', () => { + it('negate (jpeg)', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .negate() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -19,11 +23,11 @@ describe('Negate', function () { }); }); - it('negate (png)', function (done) { + it('negate (png)', (_t, done) => { sharp(fixtures.inputPng) .resize(320, 240) .negate() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -32,11 +36,11 @@ describe('Negate', function () { }); }); - it('negate (png, trans)', function (done) { + it('negate (png, trans)', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 240) .negate() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -45,11 +49,11 @@ describe('Negate', function () { }); }); - it('negate (png, alpha)', function (done) { + it('negate (png, alpha)', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .resize(320, 240) .negate() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -58,11 +62,11 @@ describe('Negate', function () { }); }); - it('negate (webp)', function (done) { + it('negate (webp)', (_t, done) => { sharp(fixtures.inputWebP) .resize(320, 240) .negate() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('webp', info.format); assert.strictEqual(320, info.width); @@ -71,11 +75,11 @@ describe('Negate', function () { }); }); - it('negate (webp, trans)', function (done) { + it('negate (webp, trans)', (_t, done) => { sharp(fixtures.inputWebPWithTransparency) .resize(320, 240) .negate() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('webp', info.format); assert.strictEqual(320, info.width); @@ -84,11 +88,11 @@ describe('Negate', function () { }); }); - it('negate (true)', function (done) { + it('negate (true)', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .negate(true) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -97,14 +101,114 @@ describe('Negate', function () { }); }); - it('negate (false)', function (done) { + it('negate (false)', (_t, done) => { const output = fixtures.path('output.unmodified-by-negate.png'); sharp(fixtures.inputJpgWithLowContrast) .negate(false) - .toFile(output, function (err, info) { + .toFile(output, (err) => { if (err) throw err; fixtures.assertMaxColourDistance(output, fixtures.inputJpgWithLowContrast, 0); done(); }); }); + + it('negate ({alpha: true})', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 240) + .negate({ alpha: true }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('jpeg', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('negate.jpg'), data, done); + }); + }); + + it('negate non-alpha channels (png)', (_t, done) => { + sharp(fixtures.inputPng) + .resize(320, 240) + .negate({ alpha: false }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha.png'), data, done); + }); + }); + + it('negate non-alpha channels (png, trans)', (_t, done) => { + sharp(fixtures.inputPngWithTransparency) + .resize(320, 240) + .negate({ alpha: false }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha-trans.png'), data, done); + }); + }); + + it('negate non-alpha channels (png, alpha)', (_t, done) => { + sharp(fixtures.inputPngWithGreyAlpha) + .resize(320, 240) + .negate({ alpha: false }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha-grey.png'), data, done); + }); + }); + + it('negate non-alpha channels (webp)', (_t, done) => { + sharp(fixtures.inputWebP) + .resize(320, 240) + .negate({ alpha: false }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('webp', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha.webp'), data, done); + }); + }); + + it('negate non-alpha channels (webp, trans)', (_t, done) => { + sharp(fixtures.inputWebPWithTransparency) + .resize(320, 240) + .negate({ alpha: false }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('webp', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha-trans.webp'), data, done); + }); + }); + + it('negate create', async () => { + const [r, g, b] = await sharp({ + create: { + width: 1, + height: 1, + channels: 3, + background: { r: 10, g: 20, b: 30 } + } + }) + .negate() + .raw() + .toBuffer(); + + assert.deepStrictEqual({ r, g, b }, { r: 245, g: 235, b: 225 }); + }); + + it('invalid alpha value', () => { + assert.throws(() => { + sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' }); + }); + }); }); diff --git a/test/unit/noise.js b/test/unit/noise.js index 555a39eb0..c95006b6d 100644 --- a/test/unit/noise.js +++ b/test/unit/noise.js @@ -1,12 +1,16 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Gaussian noise', function () { - it('generate single-channel gaussian noise', function (done) { +describe('Gaussian noise', () => { + it('generate single-channel gaussian noise', (_t, done) => { const output = fixtures.path('output.noise-1-channel.png'); const noise = sharp({ create: { @@ -19,14 +23,14 @@ describe('Gaussian noise', function () { sigma: 30 } } - }); - noise.toFile(output, function (err, info) { + }).toColourspace('b-w'); + noise.toFile(output, (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) { + sharp(output).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('b-w', metadata.space); assert.strictEqual('uchar', metadata.depth); @@ -35,7 +39,7 @@ describe('Gaussian noise', function () { }); }); - it('generate 3-channels gaussian noise', function (done) { + it('generate 3-channels gaussian noise', (_t, done) => { const output = fixtures.path('output.noise-3-channels.png'); const noise = sharp({ create: { @@ -49,13 +53,13 @@ describe('Gaussian noise', function () { } } }); - noise.toFile(output, function (err, info) { + noise.toFile(output, (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) { + sharp(output).metadata((err, metadata) => { if (err) throw err; assert.strictEqual('srgb', metadata.space); assert.strictEqual('uchar', metadata.depth); @@ -64,7 +68,7 @@ describe('Gaussian noise', function () { }); }); - it('overlay 3-channels gaussian noise over image', function (done) { + it('overlay 3-channels gaussian noise over image', (_t, done) => { const output = fixtures.path('output.noise-image.jpg'); const noise = sharp({ create: { @@ -78,7 +82,7 @@ describe('Gaussian noise', function () { } } }); - noise.toBuffer(function (err, data, info) { + noise.toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(3, info.channels); sharp(fixtures.inputJpg) @@ -94,14 +98,14 @@ describe('Gaussian noise', function () { } } ]) - .toFile(output, function (err, info) { + .toFile(output, (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) { + fixtures.assertSimilar(output, fixtures.inputJpg, (err) => { if (err) throw err; done(); }); @@ -109,7 +113,7 @@ describe('Gaussian noise', function () { }); }); - it('overlay strong single-channel (sRGB) gaussian noise with 25% transparency over transparent png image', function (done) { + it('overlay strong single-channel (sRGB) gaussian noise with 25% transparency over transparent png image', (_t, done) => { const output = fixtures.path('output.noise-image-transparent.png'); const width = 320; const height = 240; @@ -131,14 +135,15 @@ describe('Gaussian noise', function () { } }); noise - .toBuffer(function (err, data, info) { + .toColourspace('b-w') + .toBuffer((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) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(4, info.channels); sharp(fixtures.inputPngRGBWithAlpha) @@ -154,13 +159,13 @@ describe('Gaussian noise', function () { } } ]) - .toFile(output, function (err, info) { + .toFile(output, (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) { + fixtures.assertSimilar(output, fixtures.inputPngRGBWithAlpha, { threshold: 10 }, (err) => { if (err) throw err; done(); }); @@ -169,16 +174,36 @@ describe('Gaussian noise', function () { }); }); - it('no create object properties specified', function () { - assert.throws(function () { + it('animated noise', async () => { + const gif = await sharp({ + create: { + width: 16, + height: 64, + pageHeight: 16, + channels: 3, + noise: { type: 'gaussian' } + } + }) + .gif() + .toBuffer(); + + const { width, height, pages, delay } = await sharp(gif).metadata(); + assert.strictEqual(width, 16); + assert.strictEqual(height, 16); + assert.strictEqual(pages, 4); + assert.strictEqual(delay.length, 4); + }); + + it('no create object properties specified', () => { + assert.throws(() => { sharp({ create: {} }); }); }); - it('invalid noise object', function () { - assert.throws(function () { + it('invalid noise object', () => { + assert.throws(() => { sharp({ create: { width: 100, @@ -190,8 +215,8 @@ describe('Gaussian noise', function () { }); }); - it('unknown type of noise', function () { - assert.throws(function () { + it('unknown type of noise', () => { + assert.throws(() => { sharp({ create: { width: 100, @@ -205,8 +230,8 @@ describe('Gaussian noise', function () { }); }); - it('gaussian noise, invalid amount of channels', function () { - assert.throws(function () { + it('gaussian noise, invalid amount of channels', () => { + assert.throws(() => { sharp({ create: { width: 100, @@ -222,8 +247,8 @@ describe('Gaussian noise', function () { }); }); - it('gaussian noise, invalid mean', function () { - assert.throws(function () { + it('gaussian noise, invalid mean', () => { + assert.throws(() => { sharp({ create: { width: 100, @@ -239,8 +264,8 @@ describe('Gaussian noise', function () { }); }); - it('gaussian noise, invalid sigma', function () { - assert.throws(function () { + it('gaussian noise, invalid sigma', () => { + assert.throws(() => { sharp({ create: { width: 100, @@ -255,4 +280,29 @@ describe('Gaussian noise', function () { }); }); }); + + it('Invalid pageHeight', () => { + const create = { + width: 8, + height: 8, + channels: 4, + noise: { type: 'gaussian' } + }; + assert.throws( + () => sharp({ create: { ...create, pageHeight: 'zoinks' } }), + /Expected positive integer for create\.pageHeight but received zoinks of type string/ + ); + assert.throws( + () => sharp({ create: { ...create, pageHeight: -1 } }), + /Expected positive integer for create\.pageHeight but received -1 of type number/ + ); + assert.throws( + () => sharp({ create: { ...create, pageHeight: 9 } }), + /Expected positive integer for create\.pageHeight but received 9 of type number/ + ); + assert.throws( + () => sharp({ create: { ...create, pageHeight: 3 } }), + /Expected create\.height 8 to be a multiple of create\.pageHeight 3/ + ); + }); }); diff --git a/test/unit/normalize.js b/test/unit/normalize.js index 1bfb2862d..2c0cf32c6 100644 --- a/test/unit/normalize.js +++ b/test/unit/normalize.js @@ -1,64 +1,67 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -const assertNormalized = function (data) { +const assertNormalized = (data) => { let min = 255; let max = 0; for (let i = 0; i < data.length; i++) { min = Math.min(min, data[i]); max = Math.max(max, data[i]); } - assert.strictEqual(0, min); - assert.strictEqual(255, max); + assert.strictEqual(0, min, 'min too high'); + assert.ok(max > 248, 'max too low'); }; -describe('Normalization', function () { - it('spreads rgb image values between 0 and 255', function (done) { +describe('Normalization', () => { + it('spreads rgb image values between 0 and 255', (_t, done) => { sharp(fixtures.inputJpgWithLowContrast) .normalise() .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; assertNormalized(data); done(); }); }); - it('spreads grayscaled image values between 0 and 255', function (done) { + it('spreads grayscaled image values between 0 and 255', (_t, done) => { sharp(fixtures.inputJpgWithLowContrast) - .gamma() .greyscale() - .normalize(true) + .normalize() .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; assertNormalized(data); done(); }); }); - it('stretches greyscale images with alpha channel', function (done) { + it('stretches greyscale images with alpha channel', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .normalise() .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; assertNormalized(data); done(); }); }); - it('keeps an existing alpha channel', function (done) { + it('keeps an existing alpha channel', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(8, 8) .normalize() - .toBuffer(function (err, data) { + .toBuffer((err, data) => { if (err) throw err; - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) return done(err); assert.strictEqual(4, metadata.channels); assert.strictEqual(true, metadata.hasAlpha); @@ -68,13 +71,13 @@ describe('Normalization', function () { }); }); - it('keeps the alpha channel of greyscale images intact', function (done) { + it('keeps the alpha channel of greyscale images intact', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .resize(8, 8) .normalise() - .toBuffer(function (err, data) { + .toBuffer((err, data) => { if (err) throw err; - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) return done(err); assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual(4, metadata.channels); @@ -84,25 +87,79 @@ describe('Normalization', function () { }); }); - it('does not alter images with only one color', function (done) { + it('does not alter images with only one color', (_t, done) => { const output = fixtures.path('output.unmodified-png-with-one-color.png'); sharp(fixtures.inputPngWithOneColor) .normalize() - .toFile(output, function (err, info) { + .toFile(output, (err) => { if (err) done(err); fixtures.assertMaxColourDistance(output, fixtures.inputPngWithOneColor, 0); done(); }); }); - it('works with 16-bit RGBA images', function (done) { + it('works with 16-bit RGBA images', (_t, done) => { sharp(fixtures.inputPngWithTransparency16bit) .normalise() .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { + if (err) throw err; + assertNormalized(data); + done(); + }); + }); + + it('should handle luminance range', (_t, done) => { + sharp(fixtures.inputJpgWithLowContrast) + .normalise({ lower: 10, upper: 70 }) + .raw() + .toBuffer((err, data) => { if (err) throw err; assertNormalized(data); done(); }); }); + + it('should allow lower without upper', () => { + assert.doesNotThrow(() => sharp().normalize({ lower: 2 })); + }); + it('should allow upper without lower', () => { + assert.doesNotThrow(() => sharp().normalize({ upper: 98 })); + }); + it('should throw when lower is out of range', () => { + assert.throws( + () => sharp().normalise({ lower: -10 }), + /Expected number between 0 and 99 for lower but received -10 of type number/ + ); + }); + it('should throw when upper is out of range', () => { + assert.throws( + () => sharp().normalise({ upper: 110 }), + /Expected number between 1 and 100 for upper but received 110 of type number/ + ); + }); + it('should throw when lower is not a number', () => { + assert.throws( + () => sharp().normalise({ lower: 'fail' }), + /Expected number between 0 and 99 for lower but received fail of type string/ + ); + }); + it('should throw when upper is not a number', () => { + assert.throws( + () => sharp().normalise({ upper: 'fail' }), + /Expected number between 1 and 100 for upper but received fail of type string/ + ); + }); + it('should throw when the lower and upper are equal', () => { + assert.throws( + () => sharp().normalise({ lower: 2, upper: 2 }), + /Expected lower to be less than upper for range but received 2 >= 2/ + ); + }); + it('should throw when the lower is greater than upper', () => { + assert.throws( + () => sharp().normalise({ lower: 3, upper: 2 }), + /Expected lower to be less than upper for range but received 3 >= 2/ + ); + }); }); diff --git a/test/unit/platform.js b/test/unit/platform.js deleted file mode 100644 index 740de373a..000000000 --- a/test/unit/platform.js +++ /dev/null @@ -1,66 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const platform = require('../../lib/platform'); - -describe('Platform-detection', function () { - it('Can override arch with npm_config_arch', function () { - process.env.npm_config_arch = 'test'; - assert.strictEqual('test', platform().split('-')[1]); - delete process.env.npm_config_arch; - }); - - it('Can override platform with npm_config_platform', function () { - process.env.npm_config_platform = 'test'; - assert.strictEqual('test', platform().split('-')[0]); - delete process.env.npm_config_platform; - }); - - it('Can override ARM version via --arm-version', function () { - process.env.npm_config_arch = 'arm'; - process.env.npm_config_arm_version = 'test'; - assert.strictEqual('armvtest', platform().split('-')[1]); - delete process.env.npm_config_arm_version; - delete process.env.npm_config_arch; - }); - - it('Can override ARM64 version via --arm-version', function () { - process.env.npm_config_arch = 'arm64'; - process.env.npm_config_arm_version = 'test'; - assert.strictEqual('arm64vtest', platform().split('-')[1]); - delete process.env.npm_config_arm_version; - delete process.env.npm_config_arch; - }); - - it('Can detect ARM version via process.config', function () { - process.env.npm_config_arch = 'arm'; - const armVersion = process.config.variables.arm_version; - process.config.variables.arm_version = 'test'; - assert.strictEqual('armvtest', platform().split('-')[1]); - process.config.variables.arm_version = armVersion; - delete process.env.npm_config_arch; - }); - - it('Defaults to ARMv6 for 32-bit', function () { - process.env.npm_config_arch = 'arm'; - const armVersion = process.config.variables.arm_version; - delete process.config.variables.arm_version; - assert.strictEqual('armv6', platform().split('-')[1]); - process.config.variables.arm_version = armVersion; - delete process.env.npm_config_arch; - }); - - it('Defaults to ARMv8 for 64-bit', function () { - process.env.npm_config_arch = 'arm64'; - assert.strictEqual('arm64v8', platform().split('-')[1]); - delete process.env.npm_config_arch; - }); - - it('Can ensure version ARMv7 if electron version is present', function () { - process.env.npm_config_arch = 'arm'; - process.versions.electron = 'test'; - assert.strictEqual('armv7', platform().split('-')[1]); - delete process.env.npm_config_arch; - delete process.versions.electron; - }); -}); diff --git a/test/unit/png.js b/test/unit/png.js index 996941339..81800e1a7 100644 --- a/test/unit/png.js +++ b/test/unit/png.js @@ -1,30 +1,34 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('PNG', function () { - it('compression level is valid', function () { - assert.doesNotThrow(function () { +describe('PNG', () => { + it('compression level is valid', () => { + assert.doesNotThrow(() => { sharp().png({ compressionLevel: 0 }); }); }); - it('compression level is invalid', function () { - assert.throws(function () { + it('compression level is invalid', () => { + assert.throws(() => { sharp().png({ compressionLevel: -1 }); }); }); - it('default compressionLevel generates smaller file than compressionLevel=0', function (done) { + it('default compressionLevel generates smaller file than compressionLevel=0', (_t, done) => { // First generate with default compressionLevel sharp(fixtures.inputPng) .resize(320, 240) .png() - .toBuffer(function (err, defaultData, defaultInfo) { + .toBuffer((err, defaultData, defaultInfo) => { if (err) throw err; assert.strictEqual(true, defaultData.length > 0); assert.strictEqual('png', defaultInfo.format); @@ -32,7 +36,7 @@ describe('PNG', function () { sharp(fixtures.inputPng) .resize(320, 240) .png({ compressionLevel: 0 }) - .toBuffer(function (err, largerData, largerInfo) { + .toBuffer((err, largerData, largerInfo) => { if (err) throw err; assert.strictEqual(true, largerData.length > 0); assert.strictEqual('png', largerInfo.format); @@ -42,12 +46,12 @@ describe('PNG', function () { }); }); - it('without adaptiveFiltering generates smaller file', function (done) { + it('without adaptiveFiltering generates smaller file', (_t, done) => { // First generate with adaptive filtering sharp(fixtures.inputPng) .resize(320, 240) .png({ adaptiveFiltering: true }) - .toBuffer(function (err, adaptiveData, adaptiveInfo) { + .toBuffer((err, adaptiveData, adaptiveInfo) => { if (err) throw err; assert.strictEqual(true, adaptiveData.length > 0); assert.strictEqual(adaptiveData.length, adaptiveInfo.size); @@ -58,7 +62,7 @@ describe('PNG', function () { sharp(fixtures.inputPng) .resize(320, 240) .png({ adaptiveFiltering: false }) - .toBuffer(function (err, withoutAdaptiveData, withoutAdaptiveInfo) { + .toBuffer((err, withoutAdaptiveData, withoutAdaptiveInfo) => { if (err) throw err; assert.strictEqual(true, withoutAdaptiveData.length > 0); assert.strictEqual(withoutAdaptiveData.length, withoutAdaptiveInfo.size); @@ -71,17 +75,17 @@ describe('PNG', function () { }); }); - it('Invalid PNG adaptiveFiltering value throws error', function () { - assert.throws(function () { + it('Invalid PNG adaptiveFiltering value throws error', () => { + assert.throws(() => { sharp().png({ adaptiveFiltering: 1 }); }); }); - it('Progressive PNG image', function (done) { + it('Progressive PNG image', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .png({ progressive: false }) - .toBuffer(function (err, nonProgressiveData, nonProgressiveInfo) { + .toBuffer((err, nonProgressiveData, nonProgressiveInfo) => { if (err) throw err; assert.strictEqual(true, nonProgressiveData.length > 0); assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size); @@ -90,7 +94,7 @@ describe('PNG', function () { assert.strictEqual(240, nonProgressiveInfo.height); sharp(nonProgressiveData) .png({ progressive: true }) - .toBuffer(function (err, progressiveData, progressiveInfo) { + .toBuffer((err, progressiveData, progressiveInfo) => { if (err) throw err; assert.strictEqual(true, progressiveData.length > 0); assert.strictEqual(progressiveData.length, progressiveInfo.size); @@ -103,77 +107,146 @@ describe('PNG', function () { }); }); - it('16-bit grey+alpha PNG identity transform', function () { + it('16-bit grey+alpha PNG identity transform', () => { const actual = fixtures.path('output.16-bit-grey-alpha-identity.png'); return sharp(fixtures.inputPng16BitGreyAlpha) .toFile(actual) - .then(function () { + .then(() => { fixtures.assertMaxColourDistance(actual, fixtures.expected('16-bit-grey-alpha-identity.png')); }); }); - it('Valid PNG libimagequant palette value does not throw error', function () { - assert.doesNotThrow(function () { + it('16-bit grey+alpha PNG roundtrip', async () => { + const after = await sharp(fixtures.inputPng16BitGreyAlpha) + .toColourspace('grey16') + .toBuffer(); + + const [alphaMeanBefore, alphaMeanAfter] = ( + await Promise.all([ + sharp(fixtures.inputPng16BitGreyAlpha).stats(), + sharp(after).stats() + ]) + ) + .map(stats => stats.channels[1].mean); + + assert.strictEqual(alphaMeanAfter, alphaMeanBefore); + }); + + it('palette decode/encode roundtrip', async () => { + const data = await sharp(fixtures.inputPngPalette) + .png({ effort: 1, palette: true }) + .toBuffer(); + + const { size, ...metadata } = await sharp(data).metadata(); + void size; + assert.deepStrictEqual(metadata, { + autoOrient: { + height: 68, + width: 68 + }, + format: 'png', + width: 68, + height: 68, + space: 'srgb', + channels: 3, + density: 72, + depth: 'uchar', + isProgressive: false, + isPalette: true, + bitsPerSample: 8, + paletteBitDepth: 8, + hasProfile: false, + hasAlpha: false + }); + }); + + it('Valid PNG libimagequant palette value does not throw error', () => { + assert.doesNotThrow(() => { sharp().png({ palette: false }); }); }); - it('Invalid PNG libimagequant palette value throws error', function () { - assert.throws(function () { + it('Invalid PNG libimagequant palette value throws error', () => { + assert.throws(() => { sharp().png({ palette: 'fail' }); }); }); - it('Valid PNG libimagequant quality value produces image of same size or smaller', function () { + it('Valid PNG libimagequant quality value produces image of same size or smaller', () => { const inputPngBuffer = fs.readFileSync(fixtures.inputPng); return Promise.all([ - sharp(inputPngBuffer).resize(10).png({ quality: 80 }).toBuffer(), - sharp(inputPngBuffer).resize(10).png({ quality: 100 }).toBuffer() - ]).then(function (data) { + sharp(inputPngBuffer).resize(10).png({ effort: 1, quality: 80 }).toBuffer(), + sharp(inputPngBuffer).resize(10).png({ effort: 1, quality: 100 }).toBuffer() + ]).then((data) => { assert.strictEqual(true, data[0].length <= data[1].length); }); }); - it('Invalid PNG libimagequant quality value throws error', function () { - assert.throws(function () { + it('Invalid PNG libimagequant quality value throws error', () => { + assert.throws(() => { sharp().png({ quality: 101 }); }); }); - it('Valid PNG libimagequant colours value produces image of same size or smaller', function () { + it('Invalid effort value throws error', () => { + assert.throws(() => { + sharp().png({ effort: 0.1 }); + }); + }); + + it('Valid PNG libimagequant colours value produces image of same size or smaller', () => { const inputPngBuffer = fs.readFileSync(fixtures.inputPng); return Promise.all([ sharp(inputPngBuffer).resize(10).png({ colours: 100 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ colours: 200 }).toBuffer() - ]).then(function (data) { + ]).then((data) => { assert.strictEqual(true, data[0].length <= data[1].length); }); }); - it('Invalid PNG libimagequant colours value throws error', function () { - assert.throws(function () { + it('Invalid PNG libimagequant colours value throws error', () => { + assert.throws(() => { sharp().png({ colours: -1 }); }); }); - it('Invalid PNG libimagequant colors value throws error', function () { - assert.throws(function () { + it('Invalid PNG libimagequant colors value throws error', () => { + assert.throws(() => { sharp().png({ colors: 0.1 }); }); }); - it('Valid PNG libimagequant dither value produces image of same size or smaller', function () { + it('Can set bitdepth of PNG without palette', async () => { + const data = await sharp({ + create: { + width: 8, height: 8, channels: 3, background: 'red' + } + }) + .toColourspace('b-w') + .png({ colours: 2, palette: false }) + .toBuffer(); + + const { channels, isPalette, bitsPerSample, paletteBitDepth, size, space } = await sharp(data).metadata(); + assert.strictEqual(channels, 1); + assert.strictEqual(isPalette, false); + assert.strictEqual(bitsPerSample, 1); + assert.strictEqual(paletteBitDepth, undefined); + assert.strictEqual(size, 89); + assert.strictEqual(space, 'b-w'); + }); + + it('Valid PNG libimagequant dither value produces image of same size or smaller', () => { const inputPngBuffer = fs.readFileSync(fixtures.inputPng); return Promise.all([ sharp(inputPngBuffer).resize(10).png({ dither: 0.1 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ dither: 0.9 }).toBuffer() - ]).then(function (data) { + ]).then((data) => { assert.strictEqual(true, data[0].length <= data[1].length); }); }); - it('Invalid PNG libimagequant dither value throws error', function () { - assert.throws(function () { + it('Invalid PNG libimagequant dither value throws error', () => { + assert.throws(() => { sharp().png({ dither: 'fail' }); }); }); diff --git a/test/unit/raw.js b/test/unit/raw.js index f3fb3eb2d..a3e9d563d 100644 --- a/test/unit/raw.js +++ b/test/unit/raw.js @@ -1,48 +1,96 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Raw pixel data', function () { - describe('Raw pixel input', function () { - it('Missing options', function () { - assert.throws(function () { +describe('Raw pixel data', () => { + describe('Raw pixel input', () => { + it('Empty data', () => { + assert.throws(() => { + sharp(Buffer.from('')); + }, /empty/); + assert.throws(() => { + sharp(new ArrayBuffer(0)); + }, /empty/); + assert.throws(() => { + sharp(new Uint8Array(0)); + }, /empty/); + assert.throws(() => { + sharp(new Uint8ClampedArray(0)); + }, /empty/); + }); + + it('Missing options', () => { + assert.throws(() => { sharp({ raw: {} }); }); }); - it('Incomplete options', function () { - assert.throws(function () { + it('Incomplete options', () => { + assert.throws(() => { sharp({ raw: { width: 1, height: 1 } }); }); }); - it('Invalid channels', function () { - assert.throws(function () { + it('Invalid channels', () => { + assert.throws(() => { sharp({ raw: { width: 1, height: 1, channels: 5 } }); }); }); - it('Invalid height', function () { - assert.throws(function () { + it('Invalid height', () => { + assert.throws(() => { sharp({ raw: { width: 1, height: 0, channels: 4 } }); }); }); - it('Invalid width', function () { - assert.throws(function () { + it('Invalid width', () => { + assert.throws(() => { sharp({ raw: { width: 'zoinks', height: 1, channels: 4 } }); }); }); - it('RGB', function (done) { + it('Invalid premultiplied', () => { + assert.throws( + () => sharp({ raw: { width: 1, height: 1, channels: 4, premultiplied: 'zoinks' } }), + /Expected boolean for raw\.premultiplied but received zoinks of type string/ + ); + }); + + it('Invalid pageHeight', () => { + const width = 8; + const height = 8; + const channels = 4; + assert.throws( + () => sharp({ raw: { width, height, channels, pageHeight: 'zoinks' } }), + /Expected positive integer for raw\.pageHeight but received zoinks of type string/ + ); + assert.throws( + () => sharp({ raw: { width, height, channels, pageHeight: -1 } }), + /Expected positive integer for raw\.pageHeight but received -1 of type number/ + ); + assert.throws( + () => sharp({ raw: { width, height, channels, pageHeight: 9 } }), + /Expected positive integer for raw\.pageHeight but received 9 of type number/ + ); + assert.throws( + () => sharp({ raw: { width, height, channels, pageHeight: 3 } }), + /Expected raw\.height 8 to be a multiple of raw\.pageHeight 3/ + ); + }); + + it('RGB', (_t, done) => { // Convert to raw pixel data sharp(fixtures.inputJpg) .resize(256) .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(256, info.width); assert.strictEqual(209, info.height); @@ -56,7 +104,7 @@ describe('Raw pixel data', function () { } }) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(256, info.width); assert.strictEqual(209, info.height); @@ -66,12 +114,12 @@ describe('Raw pixel data', function () { }); }); - it('RGBA', function (done) { + it('RGBA', (_t, done) => { // Convert to raw pixel data sharp(fixtures.inputPngOverlayLayer1) .resize(256) .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(256, info.width); assert.strictEqual(192, info.height); @@ -85,7 +133,7 @@ describe('Raw pixel data', function () { } }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(256, info.width); assert.strictEqual(192, info.height); @@ -95,7 +143,53 @@ describe('Raw pixel data', function () { }); }); - it('JPEG to raw Stream and back again', function (done) { + it('RGBA premultiplied', (_t, done) => { + // Convert to raw pixel data + sharp(fixtures.inputPngSolidAlpha) + .resize(256) + .raw() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(256, info.width); + assert.strictEqual(192, info.height); + assert.strictEqual(4, info.channels); + + const originalData = Buffer.from(data); + + // Premultiply image data + for (let i = 0; i < data.length; i += 4) { + const alpha = data[i + 3]; + const norm = alpha / 255; + + if (alpha < 255) { + data[i] = Math.round(data[i] * norm); + data[i + 1] = Math.round(data[i + 1] * norm); + data[i + 2] = Math.round(data[i + 2] * norm); + } + } + + // Convert back to PNG + sharp(data, { + raw: { + width: info.width, + height: info.height, + channels: info.channels, + premultiplied: true + } + }) + .raw() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(256, info.width); + assert.strictEqual(192, info.height); + assert.strictEqual(4, info.channels); + assert.equal(data.compare(originalData), 0, 'output buffer matches unpremultiplied input buffer'); + done(); + }); + }); + }); + + it('JPEG to raw Stream and back again', (_t, done) => { const width = 32; const height = 24; const writable = sharp({ @@ -107,7 +201,7 @@ describe('Raw pixel data', function () { }); writable .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(32, info.width); @@ -121,13 +215,13 @@ describe('Raw pixel data', function () { }); }); - describe('Ouput raw, uncompressed image data', function () { - it('1 channel greyscale image', function (done) { + describe('Output raw, uncompressed image data', () => { + it('1 channel greyscale image', (_t, done) => { sharp(fixtures.inputJpg) .greyscale() .resize(32, 24) .raw() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(32 * 24 * 1, info.size); assert.strictEqual(data.length, info.size); @@ -139,11 +233,11 @@ describe('Raw pixel data', function () { }); }); - it('3 channel colour image without transparency', function (done) { + it('3 channel colour image without transparency', (_t, done) => { sharp(fixtures.inputJpg) .resize(32, 24) .toFormat('raw') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(32 * 24 * 3, info.size); assert.strictEqual(data.length, info.size); @@ -154,11 +248,11 @@ describe('Raw pixel data', function () { }); }); - it('4 channel colour image with transparency', function (done) { + it('4 channel colour image with transparency', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(32, 24) .toFormat(sharp.format.raw) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(32 * 24 * 4, info.size); assert.strictEqual(data.length, info.size); @@ -169,7 +263,7 @@ describe('Raw pixel data', function () { }); }); - it('extract A from RGBA', () => + it('Extract A from RGBA', () => sharp(fixtures.inputPngWithTransparency) .resize(32, 24) .extractChannel(3) @@ -183,4 +277,94 @@ describe('Raw pixel data', function () { }) ); }); + + describe('Raw pixel depths', () => { + it('Invalid depth', () => { + assert.throws(() => { + sharp(Buffer.alloc(3), { raw: { width: 1, height: 1, channels: 3 } }) + .raw({ depth: 'zoinks' }); + }); + }); + + for (const { type, depth, bits } of [ + { type: Uint8Array, depth: undefined, bits: 8 }, + { type: Uint8Array, depth: 'uchar', bits: 8 }, + { type: Uint8ClampedArray, depth: 'uchar', bits: 8 }, + { type: Int8Array, depth: 'char', bits: 8 }, + { type: Uint16Array, depth: 'ushort', bits: 16 }, + { type: Int16Array, depth: 'short', bits: 16 }, + { type: Uint32Array, depth: 'uint', bits: 32 }, + { type: Int32Array, depth: 'int', bits: 32 }, + { type: Float32Array, depth: 'float', bits: 32 }, + { type: Float64Array, depth: 'double', bits: 64 } + ]) { + it(type.name, () => + sharp(new type(3), { raw: { width: 1, height: 1, channels: 3 } }) + .raw({ depth }) + .toBuffer({ resolveWithObject: true }) + .then(({ data, info }) => { + assert.strictEqual(1, info.width); + assert.strictEqual(1, info.height); + assert.strictEqual(3, info.channels); + if (depth !== undefined) { + assert.strictEqual(depth, info.depth); + } + assert.strictEqual(data.length / 3, bits / 8); + }) + ); + } + }); + + it('Animated', async () => { + const gif = await sharp( + Buffer.alloc(8), + { raw: { width: 1, height: 2, channels: 4, pageHeight: 1 }, animated: true } + ) + .gif({ keepDuplicateFrames: true }) + .toBuffer(); + + const { width, height, pages, delay } = await sharp(gif).metadata(); + assert.strictEqual(width, 1); + assert.strictEqual(height, 1); + assert.strictEqual(pages, 2); + assert.strictEqual(delay.length, 2); + }); + + describe('16-bit roundtrip', () => { + it('grey', async () => { + const grey = 42000; + const png = await sharp( + Uint16Array.from([grey]), + { raw: { width: 1, height: 1, channels: 1 } } + ) + .toColourspace('grey16') + .png({ compressionLevel: 0 }) + .toBuffer(); + const raw = await sharp(png) + .toColourspace('grey16') + .raw({ depth: 'ushort' }) + .toBuffer(); + + assert.strictEqual(raw.readUint16LE(0), grey); + }); + + it('RGB', async () => { + const rgb = [10946, 28657, 46368]; + const png = await sharp( + Uint16Array.from(rgb), + { raw: { width: 1, height: 1, channels: 3 } } + ) + .toColourspace('rgb16') + .png({ compressionLevel: 0 }) + .toBuffer(); + const raw = await sharp(png) + .toColourspace('rgb16') + .raw({ depth: 'ushort' }) + .toBuffer(); + + assert.strictEqual(raw.readUint16LE(0), rgb[0]); + assert.strictEqual(raw.readUint16LE(2), rgb[1]); + assert.strictEqual(raw.readUint16LE(4), rgb[2]); + }); + }); }); diff --git a/test/unit/recomb.js b/test/unit/recomb.js index df880fe53..ec05eb502 100644 --- a/test/unit/recomb.js +++ b/test/unit/recomb.js @@ -1,6 +1,10 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); @@ -11,12 +15,12 @@ const sepia = [ [0.2392, 0.4696, 0.0912] ]; -describe('Recomb', function () { - it('applies a sepia filter using recomb', function (done) { +describe('Recomb', () => { + it('applies a sepia filter using recomb', (_t, done) => { const output = fixtures.path('output.recomb-sepia.jpg'); sharp(fixtures.inputJpgWithLandscapeExif1) .recomb(sepia) - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(600, info.width); @@ -30,11 +34,11 @@ describe('Recomb', function () { }); }); - it('applies a sepia filter using recomb to an PNG with Alpha', function (done) { + it('applies a sepia filter using recomb to an PNG with Alpha', (_t, done) => { const output = fixtures.path('output.recomb-sepia.png'); sharp(fixtures.inputPngAlphaPremultiplicationSmall) .recomb(sepia) - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(1024, info.width); @@ -62,7 +66,7 @@ describe('Recomb', function () { assert.strictEqual(3, info.channels); }); - it('applies a different sepia filter using recomb', function (done) { + it('applies a different sepia filter using recomb', (_t, done) => { const output = fixtures.path('output.recomb-sepia2.jpg'); sharp(fixtures.inputJpgWithLandscapeExif1) .recomb([ @@ -70,7 +74,7 @@ describe('Recomb', function () { [0.349, 0.686, 0.168], [0.272, 0.534, 0.131] ]) - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(600, info.width); @@ -83,7 +87,7 @@ describe('Recomb', function () { done(); }); }); - it('increases the saturation of the image', function (done) { + it('increases the saturation of the image', (_t, done) => { const saturationLevel = 1; const output = fixtures.path('output.recomb-saturation.jpg'); sharp(fixtures.inputJpgWithLandscapeExif1) @@ -104,7 +108,7 @@ describe('Recomb', function () { saturationLevel + 1 - 0.114 ] ]) - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(600, info.width); @@ -118,19 +122,42 @@ describe('Recomb', function () { }); }); - describe('invalid matrix specification', function () { - it('missing', function () { - assert.throws(function () { + it('applies opacity 30% to the image', (_t, done) => { + const output = fixtures.path('output.recomb-opacity.png'); + sharp(fixtures.inputPngWithTransparent) + .recomb([ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 0.3] + ]) + .toFile(output, (err, info) => { + if (err) throw err; + assert.strictEqual('png', info.format); + assert.strictEqual(48, info.width); + assert.strictEqual(48, info.height); + fixtures.assertMaxColourDistance( + output, + fixtures.expected('d-opacity-30.png'), + 17 + ); + done(); + }); + }); + + describe('invalid matrix specification', () => { + it('missing', () => { + assert.throws(() => { sharp(fixtures.inputJpg).recomb(); }); }); - it('incorrect flat data', function () { - assert.throws(function () { + it('incorrect flat data', () => { + assert.throws(() => { sharp(fixtures.inputJpg).recomb([1, 2, 3, 4, 5, 6, 7, 8, 9]); }); }); - it('incorrect sub size', function () { - assert.throws(function () { + it('incorrect sub size', () => { + assert.throws(() => { sharp(fixtures.inputJpg).recomb([ [1, 2, 3, 4], [5, 6, 7, 8], @@ -138,8 +165,8 @@ describe('Recomb', function () { ]); }); }); - it('incorrect top size', function () { - assert.throws(function () { + it('incorrect top size', () => { + assert.throws(() => { sharp(fixtures.inputJpg).recomb([[1, 2, 3, 4], [5, 6, 7, 8]]); }); }); diff --git a/test/unit/resize-contain.js b/test/unit/resize-contain.js index d443ed2a4..36cc4518e 100644 --- a/test/unit/resize-contain.js +++ b/test/unit/resize-contain.js @@ -1,19 +1,23 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Resize fit=contain', function () { - it('Allows specifying the position as a string', function (done) { +describe('Resize fit=contain', () => { + it('Allows specifying the position as a string', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240, { fit: 'contain', position: 'center' }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -21,11 +25,11 @@ describe('Resize fit=contain', function () { }); }); - it('JPEG within PNG, no alpha channel', function (done) { + it('JPEG within PNG, no alpha channel', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240, { fit: 'contain' }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -36,14 +40,14 @@ describe('Resize fit=contain', function () { }); }); - it('JPEG within WebP, to include alpha channel', function (done) { + it('JPEG within WebP, to include alpha channel', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } }) .webp() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('webp', info.format); @@ -54,10 +58,10 @@ describe('Resize fit=contain', function () { }); }); - it('PNG with alpha channel', function (done) { + it('PNG with alpha channel', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(50, 50, { fit: 'contain' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -68,10 +72,10 @@ describe('Resize fit=contain', function () { }); }); - it('16-bit PNG with alpha channel', function (done) { + it('16-bit PNG with alpha channel', (_t, done) => { sharp(fixtures.inputPngWithTransparency16bit) .resize(32, 16, { fit: 'contain' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -82,13 +86,13 @@ describe('Resize fit=contain', function () { }); }); - it('16-bit PNG with alpha channel onto RGBA', function (done) { + it('16-bit PNG with alpha channel onto RGBA', (_t, done) => { sharp(fixtures.inputPngWithTransparency16bit) .resize(32, 16, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -99,13 +103,13 @@ describe('Resize fit=contain', function () { }); }); - it('PNG with 2 channels', function (done) { + it('PNG with 2 channels', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .resize(32, 16, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -116,14 +120,14 @@ describe('Resize fit=contain', function () { }); }); - it('TIFF in LAB colourspace onto RGBA background', function (done) { + it('TIFF in LAB colourspace onto RGBA background', (_t, done) => { sharp(fixtures.inputTiffCielab) .resize(64, 128, { fit: 'contain', background: { r: 255, g: 102, b: 0, alpha: 0.5 } }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -134,10 +138,10 @@ describe('Resize fit=contain', function () { }); }); - it('Enlarge', function (done) { + it('Enlarge', (_t, done) => { sharp(fixtures.inputPngWithOneColor) .resize(320, 240, { fit: 'contain' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -148,22 +152,58 @@ describe('Resize fit=contain', function () { }); }); - it('Invalid position values should fail', function () { - [-1, 8.1, 9, 1000000, false, 'vallejo'].forEach(function (position) { - assert.throws(function () { + describe('Animated WebP', () => { + it('Width only', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(320, 240, { + fit: 'contain', + background: { r: 255, g: 0, b: 0 } + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('webp', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240 * 9, info.height); + assert.strictEqual(4, info.channels); + fixtures.assertSimilar(fixtures.expected('embed-animated-width.webp'), data, done); + }); + }); + + it('Height only', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(240, 320, { + fit: 'contain', + background: { r: 255, g: 0, b: 0 } + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('webp', info.format); + assert.strictEqual(240, info.width); + assert.strictEqual(320 * 9, info.height); + assert.strictEqual(4, info.channels); + fixtures.assertSimilar(fixtures.expected('embed-animated-height.webp'), data, done); + }); + }); + }); + + it('Invalid position values should fail', () => { + [-1, 8.1, 9, 1000000, false, 'vallejo'].forEach((position) => { + assert.throws(() => { sharp().resize(null, null, { fit: 'contain', position }); }); }); }); - it('Position horizontal top', function (done) { + it('Position horizontal top', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'top' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -174,14 +214,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal right top', function (done) { + it('Position horizontal right top', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'right top' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -192,14 +232,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal right', function (done) { + it('Position horizontal right', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'right' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -210,14 +250,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal right bottom', function (done) { + it('Position horizontal right bottom', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'right bottom' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -228,14 +268,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal bottom', function (done) { + it('Position horizontal bottom', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'bottom' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -246,14 +286,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal left bottom', function (done) { + it('Position horizontal left bottom', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'left bottom' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -264,14 +304,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal left', function (done) { + it('Position horizontal left', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'left' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -282,14 +322,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal left top', function (done) { + it('Position horizontal left top', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'left top' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -300,14 +340,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal north', function (done) { + it('Position horizontal north', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.north }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -318,14 +358,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal northeast', function (done) { + it('Position horizontal northeast', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.northeast }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -336,14 +376,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal east', function (done) { + it('Position horizontal east', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.east }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -354,14 +394,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal southeast', function (done) { + it('Position horizontal southeast', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.southeast }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -372,14 +412,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal south', function (done) { + it('Position horizontal south', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.south }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -390,14 +430,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal southwest', function (done) { + it('Position horizontal southwest', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.southwest }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -408,14 +448,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal west', function (done) { + it('Position horizontal west', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.west }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -426,14 +466,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal northwest', function (done) { + it('Position horizontal northwest', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.northwest }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -444,14 +484,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position horizontal center', function (done) { + it('Position horizontal center', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 100, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.center }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -462,14 +502,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical top', function (done) { + it('Position vertical top', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'top' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -480,14 +520,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical right top', function (done) { + it('Position vertical right top', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'right top' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -498,14 +538,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical right', function (done) { + it('Position vertical right', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'right' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -516,14 +556,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical right bottom', function (done) { + it('Position vertical right bottom', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'right bottom' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -534,14 +574,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical bottom', function (done) { + it('Position vertical bottom', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'bottom' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -552,14 +592,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical left bottom', function (done) { + it('Position vertical left bottom', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'left bottom' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -570,14 +610,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical left', function (done) { + it('Position vertical left', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'left' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -588,14 +628,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical left top', function (done) { + it('Position vertical left top', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: 'left top' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -606,14 +646,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical north', function (done) { + it('Position vertical north', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.north }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -624,14 +664,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical northeast', function (done) { + it('Position vertical northeast', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.northeast }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -642,14 +682,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical east', function (done) { + it('Position vertical east', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.east }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -660,14 +700,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical southeast', function (done) { + it('Position vertical southeast', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.southeast }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -678,14 +718,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical south', function (done) { + it('Position vertical south', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.south }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -696,14 +736,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical southwest', function (done) { + it('Position vertical southwest', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.southwest }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -714,14 +754,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical west', function (done) { + it('Position vertical west', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.west }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -732,14 +772,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical northwest', function (done) { + it('Position vertical northwest', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.northwest }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -750,14 +790,14 @@ describe('Resize fit=contain', function () { }); }); - it('Position vertical center', function (done) { + it('Position vertical center', (_t, done) => { sharp(fixtures.inputPngEmbed) .resize(200, 200, { fit: sharp.fit.contain, background: { r: 0, g: 0, b: 0, alpha: 0 }, position: sharp.gravity.center }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -767,4 +807,33 @@ describe('Resize fit=contain', function () { fixtures.assertSimilar(fixtures.expected('./embedgravitybird/9-c.png'), data, done); }); }); + + it('multiple alpha channels', async () => { + const create = { + width: 20, + height: 12, + channels: 4, + background: 'green' + }; + const multipleAlphaChannels = await sharp({ create }) + .joinChannel({ create }) + .tiff({ compression: 'deflate' }) + .toBuffer(); + + const data = await sharp(multipleAlphaChannels) + .resize({ + width: 8, + height: 8, + fit: 'contain', + background: 'blue' + }) + .tiff({ compression: 'deflate' }) + .toBuffer(); + const { format, width, height, space, channels } = await sharp(data).metadata(); + assert.deepStrictEqual(format, 'tiff'); + assert.deepStrictEqual(width, 8); + assert.deepStrictEqual(height, 8); + assert.deepStrictEqual(space, 'srgb'); + assert.deepStrictEqual(channels, 8); + }); }); diff --git a/test/unit/resize-cover.js b/test/unit/resize-cover.js index 4d7800007..4e2dda737 100644 --- a/test/unit/resize-cover.js +++ b/test/unit/resize-cover.js @@ -1,11 +1,15 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Resize fit=cover', function () { +describe('Resize fit=cover', () => { [ // Position { @@ -198,14 +202,14 @@ describe('Resize fit=cover', function () { gravity: sharp.gravity.northwest, fixture: 'gravity-west.jpg' } - ].forEach(function (settings) { - it(settings.name, function (done) { + ].forEach((settings) => { + it(settings.name, (_t, done) => { sharp(fixtures.inputJpg) .resize(settings.width, settings.height, { fit: sharp.fit.cover, position: settings.gravity }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(settings.width, info.width); assert.strictEqual(settings.height, info.height); @@ -214,13 +218,13 @@ describe('Resize fit=cover', function () { }); }); - it('Allows specifying the gravity as a string', function (done) { + it('Allows specifying the gravity as a string', (_t, done) => { sharp(fixtures.inputJpg) .resize(80, 320, { fit: sharp.fit.cover, position: 'east' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(80, info.width); assert.strictEqual(320, info.height); @@ -228,55 +232,75 @@ describe('Resize fit=cover', function () { }); }); - it('Invalid position values fail', function () { - assert.throws(function () { + it('Invalid position values fail', () => { + assert.throws(() => { sharp().resize(null, null, { fit: 'cover', position: 9 }); }, /Expected valid position\/gravity\/strategy for position but received 9 of type number/); - assert.throws(function () { + assert.throws(() => { sharp().resize(null, null, { fit: 'cover', position: 1.1 }); }, /Expected valid position\/gravity\/strategy for position but received 1.1 of type number/); - assert.throws(function () { + assert.throws(() => { sharp().resize(null, null, { fit: 'cover', position: -1 }); }, /Expected valid position\/gravity\/strategy for position but received -1 of type number/); - assert.throws(function () { + assert.throws(() => { sharp().resize(null, null, { fit: 'cover', position: 'zoinks' }).crop(); }, /Expected valid position\/gravity\/strategy for position but received zoinks of type string/); }); - it('Uses default value when none specified', function () { - assert.doesNotThrow(function () { + it('Uses default value when none specified', () => { + assert.doesNotThrow(() => { sharp().resize(null, null, { fit: 'cover' }); }); }); - it('Skip crop when post-resize dimensions are at target', function () { - return sharp(fixtures.inputJpg) + it('Skip crop when post-resize dimensions are at target', () => sharp(fixtures.inputJpg) .resize(1600, 1200) .toBuffer() - .then(function (input) { - return sharp(input) + .then((input) => sharp(input) .resize(1110, null, { fit: sharp.fit.cover, position: sharp.strategy.attention }) .toBuffer({ resolveWithObject: true }) - .then(function (result) { + .then((result) => { assert.strictEqual(1110, result.info.width); assert.strictEqual(832, result.info.height); assert.strictEqual(undefined, result.info.cropOffsetLeft); assert.strictEqual(undefined, result.info.cropOffsetTop); - }); - }); + }))); + + describe('Animated WebP', () => { + it('Width only', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(80, 320, { fit: sharp.fit.cover }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(80, info.width); + assert.strictEqual(320 * 9, info.height); + fixtures.assertSimilar(fixtures.expected('gravity-center-width.webp'), data, done); + }); + }); + + it('Height only', (_t, done) => { + sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize(320, 80, { fit: sharp.fit.cover }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(320, info.width); + assert.strictEqual(80 * 9, info.height); + fixtures.assertSimilar(fixtures.expected('gravity-center-height.webp'), data, done); + }); + }); }); - describe('Entropy-based strategy', function () { - it('JPEG', function (done) { + describe('Entropy-based strategy', () => { + it('JPEG', (_t, done) => { sharp(fixtures.inputJpg) .resize(80, 320, { fit: 'cover', position: sharp.strategy.entropy }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(3, info.channels); @@ -288,13 +312,13 @@ describe('Resize fit=cover', function () { }); }); - it('PNG', function (done) { + it('PNG', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 80, { fit: 'cover', position: sharp.strategy.entropy }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels); @@ -306,13 +330,13 @@ describe('Resize fit=cover', function () { }); }); - it('supports the strategy passed as a string', function (done) { + it('supports the strategy passed as a string', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 80, { fit: 'cover', position: 'entropy' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels); @@ -323,16 +347,28 @@ describe('Resize fit=cover', function () { fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done); }); }); + + it('Animated image rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .resize({ + width: 100, + height: 8, + position: sharp.strategy.entropy + }) + .toBuffer(), + /Resize strategy is not supported for multi-page images/ + ) + ); }); - describe('Attention strategy', function () { - it('JPEG', function (done) { + describe('Attention strategy', () => { + it('JPEG', (_t, done) => { sharp(fixtures.inputJpg) .resize(80, 320, { fit: 'cover', position: sharp.strategy.attention }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(3, info.channels); @@ -340,17 +376,19 @@ describe('Resize fit=cover', function () { assert.strictEqual(320, info.height); assert.strictEqual(-107, info.cropOffsetLeft); assert.strictEqual(0, info.cropOffsetTop); + assert.strictEqual(588, info.attentionX); + assert.strictEqual(640, info.attentionY); fixtures.assertSimilar(fixtures.expected('crop-strategy-attention.jpg'), data, done); }); }); - it('PNG', function (done) { + it('PNG', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 80, { fit: 'cover', position: sharp.strategy.attention }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels); @@ -358,17 +396,39 @@ describe('Resize fit=cover', function () { assert.strictEqual(80, info.height); assert.strictEqual(0, info.cropOffsetLeft); assert.strictEqual(0, info.cropOffsetTop); + assert.strictEqual(0, info.attentionX); + assert.strictEqual(0, info.attentionY); fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done); }); }); - it('supports the strategy passed as a string', function (done) { + it('WebP', (_t, done) => { + sharp(fixtures.inputWebP) + .resize(320, 80, { + fit: 'cover', + position: sharp.strategy.attention + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual('webp', info.format); + assert.strictEqual(3, info.channels); + assert.strictEqual(320, info.width); + assert.strictEqual(80, info.height); + assert.strictEqual(0, info.cropOffsetLeft); + assert.strictEqual(-161, info.cropOffsetTop); + assert.strictEqual(288, info.attentionX); + assert.strictEqual(745, info.attentionY); + fixtures.assertSimilar(fixtures.expected('crop-strategy.webp'), data, done); + }); + }); + + it('supports the strategy passed as a string', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 80, { fit: 'cover', position: 'attention' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels); @@ -379,5 +439,17 @@ describe('Resize fit=cover', function () { fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done); }); }); + + it('Animated image rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .resize({ + width: 100, + height: 8, + position: sharp.strategy.attention + }) + .toBuffer(), + /Resize strategy is not supported for multi-page images/ + ) + ); }); }); diff --git a/test/unit/resize.js b/test/unit/resize.js index a293fca40..e0a001543 100644 --- a/test/unit/resize.js +++ b/test/unit/resize.js @@ -1,13 +1,17 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Resize dimensions', function () { - it('Exact crop', function (done) { - sharp(fixtures.inputJpg).resize(320, 240).toBuffer(function (err, data, info) { +describe('Resize dimensions', () => { + it('Exact crop', (_t, done) => { + sharp(fixtures.inputJpg).resize(320, 240).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -17,8 +21,8 @@ describe('Resize dimensions', function () { }); }); - it('Fixed width', function (done) { - sharp(fixtures.inputJpg).resize(320).toBuffer(function (err, data, info) { + it('Fixed width', (_t, done) => { + sharp(fixtures.inputJpg).resize(320).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -28,8 +32,8 @@ describe('Resize dimensions', function () { }); }); - it('Fixed height', function (done) { - sharp(fixtures.inputJpg).resize(null, 320).toBuffer(function (err, data, info) { + it('Fixed height', (_t, done) => { + sharp(fixtures.inputJpg).resize(null, 320).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -39,8 +43,8 @@ describe('Resize dimensions', function () { }); }); - it('Identity transform', function (done) { - sharp(fixtures.inputJpg).toBuffer(function (err, data, info) { + it('Identity transform', (_t, done) => { + sharp(fixtures.inputJpg).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -50,10 +54,10 @@ describe('Resize dimensions', function () { }); }); - it('Upscale', function (done) { + it('Upscale', (_t, done) => { sharp(fixtures.inputJpg) .resize(3000) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -63,26 +67,26 @@ describe('Resize dimensions', function () { }); }); - it('Invalid width - NaN', function () { - assert.throws(function () { + it('Invalid width - NaN', () => { + assert.throws(() => { sharp().resize('spoons', 240); }, /Expected positive integer for width but received spoons of type string/); }); - it('Invalid height - NaN', function () { - assert.throws(function () { + it('Invalid height - NaN', () => { + assert.throws(() => { sharp().resize(320, 'spoons'); }, /Expected positive integer for height but received spoons of type string/); }); - it('Invalid width - float', function () { - assert.throws(function () { + it('Invalid width - float', () => { + assert.throws(() => { sharp().resize(1.5, 240); }, /Expected positive integer for width but received 1.5 of type number/); }); - it('Invalid height - float', function () { - assert.throws(function () { + it('Invalid height - float', () => { + assert.throws(() => { sharp().resize(320, 1.5); }, /Expected positive integer for height but received 1.5 of type number/); }); @@ -99,40 +103,54 @@ describe('Resize dimensions', function () { }, /Expected positive integer for height but received 1.5 of type number/); }); - it('Invalid width - too large', function (done) { + it('Invalid width - too large', (_t, done) => { sharp(fixtures.inputJpg) .resize(0x4000, 1) .webp() - .toBuffer(function (err) { + .toBuffer((err) => { assert.strictEqual(true, err instanceof Error); assert.strictEqual('Processed image is too large for the WebP format', err.message); done(); }); }); - it('Invalid height - too large', function (done) { + it('Invalid height - too large', (_t, done) => { sharp(fixtures.inputJpg) .resize(1, 0x4000) .webp() - .toBuffer(function (err) { + .toBuffer((err) => { assert.strictEqual(true, err instanceof Error); assert.strictEqual('Processed image is too large for the WebP format', err.message); done(); }); }); - it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', function (done) { + it('Webp resize then extract large image', (_t, done) => { + sharp(fixtures.inputWebP) + .resize(0x4000, 0x4000) + .extract({ top: 0x2000, left: 0x2000, width: 256, height: 256 }) + .webp() + .toBuffer((err, _data, info) => { + if (err) throw err; + assert.strictEqual('webp', info.format); + assert.strictEqual(256, info.width); + assert.strictEqual(256, info.height); + done(); + }); + }); + + it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', (_t, done) => { sharp(fixtures.inputJpg) .resize(1080, 607) .webp() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('webp', info.format); assert.strictEqual(1080, info.width); assert.strictEqual(607, info.height); sharp(data) .resize(233, 131) - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual('webp', info.format); assert.strictEqual(233, info.width); @@ -142,17 +160,17 @@ describe('Resize dimensions', function () { }); }); - it('JPEG shrink-on-load with 90 degree rotation, ensure recalculation is correct', function (done) { + it('JPEG shrink-on-load with 90 degree rotation, ensure recalculation is correct', (_t, done) => { sharp(fixtures.inputJpg) .resize(1920, 1280) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(1920, info.width); assert.strictEqual(1280, info.height); sharp(data) .rotate(90) .resize(533, 800) - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual(533, info.width); assert.strictEqual(800, info.height); @@ -161,11 +179,11 @@ describe('Resize dimensions', function () { }); }); - it('TIFF embed known to cause rounding errors', function (done) { + it('TIFF embed known to cause rounding errors', (_t, done) => { sharp(fixtures.inputTiff) .resize(240, 320, { fit: sharp.fit.contain }) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -175,11 +193,11 @@ describe('Resize dimensions', function () { }); }); - it('TIFF known to cause rounding errors', function (done) { + it('TIFF known to cause rounding errors', (_t, done) => { sharp(fixtures.inputTiff) .resize(240, 320) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -189,11 +207,11 @@ describe('Resize dimensions', function () { }); }); - it('fit=inside, portrait', function (done) { + it('fit=inside, portrait', (_t, done) => { sharp(fixtures.inputTiff) .resize(320, 320, { fit: sharp.fit.inside }) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -203,11 +221,11 @@ describe('Resize dimensions', function () { }); }); - it('fit=outside, portrait', function (done) { + it('fit=outside, portrait', (_t, done) => { sharp(fixtures.inputTiff) .resize(320, 320, { fit: sharp.fit.outside }) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -217,10 +235,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=inside, landscape', function (done) { + it('fit=inside, landscape', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 320, { fit: sharp.fit.inside }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -230,10 +248,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=outside, landscape', function (done) { + it('fit=outside, landscape', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 320, { fit: sharp.fit.outside }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -243,13 +261,13 @@ describe('Resize dimensions', function () { }); }); - it('fit=inside, provide only one dimension', function (done) { + it('fit=inside, provide only one dimension', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 320, fit: sharp.fit.inside }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -259,13 +277,13 @@ describe('Resize dimensions', function () { }); }); - it('fit=outside, provide only one dimension', function (done) { + it('fit=outside, provide only one dimension', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 320, fit: sharp.fit.outside }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -275,13 +293,13 @@ describe('Resize dimensions', function () { }); }); - it('Do not enlarge when input width is already less than output width', function (done) { + it('Do not enlarge when input width is already less than output width', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 2800, withoutEnlargement: true }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -291,13 +309,13 @@ describe('Resize dimensions', function () { }); }); - it('Do not enlarge when input height is already less than output height', function (done) { + it('Do not enlarge when input height is already less than output height', (_t, done) => { sharp(fixtures.inputJpg) .resize({ height: 2300, withoutEnlargement: true }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -307,14 +325,14 @@ describe('Resize dimensions', function () { }); }); - it('Do crop when fit = cover and withoutEnlargement = true and width >= outputWidth, and height < outputHeight', function (done) { + it('Do crop when fit = cover and withoutEnlargement = true and width >= outputWidth, and height < outputHeight', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 3000, height: 1000, withoutEnlargement: true }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -324,14 +342,14 @@ describe('Resize dimensions', function () { }); }); - it('Do crop when fit = cover and withoutEnlargement = true and width < outputWidth, and height >= outputHeight', function (done) { + it('Do crop when fit = cover and withoutEnlargement = true and width < outputWidth, and height >= outputHeight', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 1500, height: 2226, withoutEnlargement: true }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -341,13 +359,61 @@ describe('Resize dimensions', function () { }); }); - it('Do enlarge when input width is less than output width', function (done) { + it('Do enlarge when input width is less than output width', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 2800, withoutEnlargement: false }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(2800, info.width); + assert.strictEqual(2286, info.height); + done(); + }); + }); + + it('Do enlarge when input width is less than output width', (_t, done) => { + sharp(fixtures.inputJpg) + .resize({ + width: 2800, + withoutReduction: true + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(2800, info.width); + assert.strictEqual(2286, info.height); + done(); + }); + }); + + it('Do enlarge when input height is less than output height', (_t, done) => { + sharp(fixtures.inputJpg) + .resize({ + height: 2300, + withoutReduction: true + }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(2817, info.width); + assert.strictEqual(2300, info.height); + done(); + }); + }); + + it('Do enlarge when input width is less than output width', (_t, done) => { + sharp(fixtures.inputJpg) + .resize({ + width: 2800, + withoutReduction: false + }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -357,10 +423,49 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, downscale width and height', function (done) { + it('Do not resize when both withoutEnlargement and withoutReduction are true', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 320, { fit: 'fill', withoutEnlargement: true, withoutReduction: true }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + done(); + }); + }); + + it('Do not reduce size when fit = outside and withoutReduction are true and height > outputHeight and width > outputWidth', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 320, { fit: 'outside', withoutReduction: true }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + done(); + }); + }); + + it('Do resize when fit = outside and withoutReduction are true and input height > height and input width > width ', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(3000, 3000, { fit: 'outside', withoutReduction: true }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(3674, info.width); + assert.strictEqual(3000, info.height); + done(); + }); + }); + + it('fit=fill, downscale width and height', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 320, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -370,13 +475,13 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, downscale width', function (done) { + it('fit=fill, downscale width', (_t, done) => { sharp(fixtures.inputJpg) .resize({ width: 320, fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -386,13 +491,13 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, downscale height', function (done) { + it('fit=fill, downscale height', (_t, done) => { sharp(fixtures.inputJpg) .resize({ height: 320, fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -402,10 +507,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, upscale width and height', function (done) { + it('fit=fill, upscale width and height', (_t, done) => { sharp(fixtures.inputJpg) .resize(3000, 3000, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -415,10 +520,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, upscale width', function (done) { + it('fit=fill, upscale width', (_t, done) => { sharp(fixtures.inputJpg) .resize(3000, null, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -428,10 +533,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, upscale height', function (done) { + it('fit=fill, upscale height', (_t, done) => { sharp(fixtures.inputJpg) .resize(null, 3000, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -441,10 +546,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, downscale width, upscale height', function (done) { + it('fit=fill, downscale width, upscale height', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 3000, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -454,10 +559,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, upscale width, downscale height', function (done) { + it('fit=fill, upscale width, downscale height', (_t, done) => { sharp(fixtures.inputJpg) .resize(3000, 320, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -467,10 +572,10 @@ describe('Resize dimensions', function () { }); }); - it('fit=fill, identity transform', function (done) { + it('fit=fill, identity transform', (_t, done) => { sharp(fixtures.inputJpg) .resize(null, null, { fit: 'fill' }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -480,16 +585,16 @@ describe('Resize dimensions', function () { }); }); - it('Dimensions that result in differing even shrinks on each axis', function (done) { + it('Dimensions that result in differing even shrinks on each axis', (_t, done) => { sharp(fixtures.inputJpg) .resize(645, 399) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(645, info.width); assert.strictEqual(399, info.height); sharp(data) .resize(150, 100) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(150, info.width); assert.strictEqual(100, info.height); @@ -498,46 +603,37 @@ describe('Resize dimensions', function () { }); }); - it('Dimensions that result in differing odd shrinks on each axis', function (done) { - return sharp(fixtures.inputJpg) + it('Dimensions that result in differing odd shrinks on each axis', (_t, done) => sharp(fixtures.inputJpg) .resize(600, 399) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(600, info.width); assert.strictEqual(399, info.height); sharp(data) .resize(200) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(200, info.width); assert.strictEqual(133, info.height); fixtures.assertSimilar(fixtures.expected('resize-diff-shrink-odd.jpg'), data, done); }); - }); - }); + })); - it('fastShrinkOnLoad: false ensures image is not shifted', function (done) { - return sharp(fixtures.inputJpgCenteredImage) - .resize(9, 8, { fastShrinkOnLoad: false }) - .png() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(9, info.width); - assert.strictEqual(8, info.height); - fixtures.assertSimilar(fixtures.expected('fast-shrink-on-load-false.png'), data, done); - }); - }); - - it('fastShrinkOnLoad: true (default) might result in shifted image', function (done) { - return sharp(fixtures.inputJpgCenteredImage) - .resize(9, 8) - .png() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(9, info.width); - assert.strictEqual(8, info.height); - fixtures.assertSimilar(fixtures.expected('fast-shrink-on-load-true.png'), data, done); - }); + [ + true, + false + ].forEach((value) => { + it(`fastShrinkOnLoad: ${value} does not causes image shifts`, (_t, done) => { + sharp(fixtures.inputJpgCenteredImage) + .resize(9, 8, { fastShrinkOnLoad: value }) + .png() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(9, info.width); + assert.strictEqual(8, info.height); + fixtures.assertSimilar(fixtures.expected('fast-shrink-on-load.png'), data, done); + }); + }); }); [ @@ -546,11 +642,11 @@ describe('Resize dimensions', function () { sharp.kernel.mitchell, sharp.kernel.lanczos2, sharp.kernel.lanczos3 - ].forEach(function (kernel) { - it(`kernel ${kernel}`, function (done) { + ].forEach((kernel) => { + it(`kernel ${kernel}`, (_t, done) => { sharp(fixtures.inputJpg) - .resize(320, null, { kernel: kernel }) - .toBuffer(function (err, data, info) { + .resize(320, null, { kernel }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -559,11 +655,11 @@ describe('Resize dimensions', function () { }); }); - it('nearest upsampling with integral factor', function (done) { + it('nearest upsampling with integral factor', (_t, done) => { sharp(fixtures.inputTiff8BitDepth) .resize(210, 210, { kernel: 'nearest' }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual(210, info.width); assert.strictEqual(210, info.height); @@ -571,8 +667,7 @@ describe('Resize dimensions', function () { }); }); - it('Ensure shortest edge (height) is at least 1 pixel', function () { - return sharp({ + it('Ensure shortest edge (height) is at least 1 pixel', () => sharp({ create: { width: 10, height: 2, @@ -582,14 +677,12 @@ describe('Resize dimensions', function () { }) .resize(2) .toBuffer({ resolveWithObject: true }) - .then(function (output) { + .then((output) => { assert.strictEqual(2, output.info.width); assert.strictEqual(1, output.info.height); - }); - }); + })); - it('Ensure shortest edge (width) is at least 1 pixel', function () { - return sharp({ + it('Ensure shortest edge (width) is at least 1 pixel', () => sharp({ create: { width: 2, height: 10, @@ -599,27 +692,107 @@ describe('Resize dimensions', function () { }) .resize(null, 2) .toBuffer({ resolveWithObject: true }) - .then(function (output) { + .then((output) => { assert.strictEqual(1, output.info.width); assert.strictEqual(2, output.info.height); - }); + })); + + it('Ensure embedded shortest edge (height) is at least 1 pixel', () => sharp({ + create: { + width: 200, + height: 1, + channels: 3, + background: 'red' + } + }) + .resize({ width: 50, height: 50, fit: sharp.fit.contain }) + .toBuffer({ resolveWithObject: true }) + .then((output) => { + assert.strictEqual(50, output.info.width); + assert.strictEqual(50, output.info.height); + })); + + it('Ensure embedded shortest edge (width) is at least 1 pixel', () => sharp({ + create: { + width: 1, + height: 200, + channels: 3, + background: 'red' + } + }) + .resize({ width: 50, height: 50, fit: sharp.fit.contain }) + .toBuffer({ resolveWithObject: true }) + .then((output) => { + assert.strictEqual(50, output.info.width); + assert.strictEqual(50, output.info.height); + })); + + it('Skip shrink-on-load where one dimension <4px', async () => { + const jpeg = await sharp({ + create: { + width: 100, + height: 3, + channels: 3, + background: 'red' + } + }) + .jpeg() + .toBuffer(); + + const { info } = await sharp(jpeg) + .resize(8) + .toBuffer({ resolveWithObject: true }); + + assert.strictEqual(info.width, 8); + assert.strictEqual(info.height, 1); + }); + + it('Skip JPEG shrink-on-load for known libjpeg rounding errors', async () => { + const input = await sharp({ + create: { + width: 1000, + height: 667, + channels: 3, + background: 'red' + } + }) + .jpeg() + .toBuffer(); + + const output = await sharp(input) + .resize({ width: 500 }) + .toBuffer(); + + const { width, height } = await sharp(output).metadata(); + assert.strictEqual(width, 500); + assert.strictEqual(height, 334); }); - it('unknown kernel throws', function () { - assert.throws(function () { + it('unknown kernel throws', () => { + assert.throws(() => { sharp().resize(null, null, { kernel: 'unknown' }); }); }); - it('unknown fit throws', function () { - assert.throws(function () { + it('unknown fit throws', () => { + assert.throws(() => { sharp().resize(null, null, { fit: 'unknown' }); }); }); - it('unknown position throws', function () { - assert.throws(function () { + it('unknown position throws', () => { + assert.throws(() => { sharp().resize(null, null, { position: 'unknown' }); }); }); + + it('Multiple resize emits warning', () => { + let warningMessage = ''; + const s = sharp(); + s.on('warning', (msg) => { warningMessage = msg; }); + s.resize(1); + assert.strictEqual(warningMessage, ''); + s.resize(2); + assert.strictEqual(warningMessage, 'ignoring previous resize options'); + }); }); diff --git a/test/unit/rotate.js b/test/unit/rotate.js index 51bea7d9b..ae8b755c0 100644 --- a/test/unit/rotate.js +++ b/test/unit/rotate.js @@ -1,34 +1,129 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Rotation', function () { - ['Landscape', 'Portrait'].forEach(function (orientation) { - [1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) { - it('Input image has Orientation EXIF tag value of (' + exifTag + '), auto-rotate', function (done) { - sharp(fixtures['inputJpgWith' + orientation + 'Exif' + exifTag]) - .rotate() - .resize(320) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('jpeg', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(orientation === 'Landscape' ? 240 : 427, info.height); - fixtures.assertSimilar(fixtures.expected(orientation + '_' + exifTag + '-out.jpg'), data, done); +describe('Rotation', () => { + ['autoOrient', 'constructor'].forEach((rotateMethod) => { + describe(`Auto orientation via ${rotateMethod}:`, () => { + const options = rotateMethod === 'constructor' ? { autoOrient: true } : {}; + + ['Landscape', 'Portrait'].forEach((orientation) => { + [1, 2, 3, 4, 5, 6, 7, 8].forEach((exifTag) => { + const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`]; + const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, (_t, done) => { + const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; + + const img = sharp(input, options); + rotateMethod === 'autoOrient' && img.autoOrient(); + + img.toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, (_t, done) => { + const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427]; + + const img = sharp(input, options); + rotateMethod === 'autoOrient' && img.autoOrient(); + + img.resize({ width: 320 }) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); }); + + if (rotateMethod !== 'constructor') { + it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, (_t, done) => { + const [expectedWidth, expectedHeight] = orientation === 'Landscape' + ? (exifTag < 5) ? [320, 240] : [320, 240] + : [320, 427]; + + const img = sharp(input, options) + .resize({ width: 320 }); + + rotateMethod === 'autoOrient' && img.autoOrient(); + img.toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + } + + [true, false].forEach((doResize) => { + [90, 180, 270, 45].forEach((angle) => { + const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; + const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, (_t, done) => { + const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x); + const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width]; + + const img = sharp(input, options); + rotateMethod === 'autoOrient' && img.autoOrient(); + + img.rotate(angle); + doResize && img.resize(expectedWidth); + + img.toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + }); + + [[true, true], [true, false], [false, true]].forEach(([flip, flop]) => { + const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; + const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_'); + const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & '); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, (_t, done) => { + const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`); + + const img = sharp(input, options); + + rotateMethod === 'autoOrient' && img.autoOrient(); + + flip && img.flip(); + flop && img.flop(); + doResize && img.resize(orientation === 'Landscape' ? 320 : 240); + + img.toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1)); + assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1)); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + }); + }); + }); }); }); }); - it('Rotate by 30 degrees with semi-transparent background', function (done) { + it('Rotate by 30 degrees with semi-transparent background', (_t, done) => { sharp(fixtures.inputJpg) - .rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } }) .resize(320) + .rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } }) .png() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(408, info.width); @@ -37,11 +132,11 @@ describe('Rotation', function () { }); }); - it('Rotate by 30 degrees with solid background', function (done) { + it('Rotate by 30 degrees with solid background', (_t, done) => { sharp(fixtures.inputJpg) - .rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } }) .resize(320) - .toBuffer(function (err, data, info) { + .rotate(30, { background: { r: 255, g: 0, b: 0 } }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(408, info.width); @@ -50,31 +145,37 @@ describe('Rotation', function () { }); }); - it('Rotate by 90 degrees, respecting output input size', function (done) { - sharp(fixtures.inputJpg).rotate(90).resize(320, 240).toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(true, data.length > 0); - assert.strictEqual('jpeg', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); - done(); - }); + it('Rotate by 90 degrees, respecting output input size', (_t, done) => { + sharp(fixtures.inputJpg) + .rotate(90) + .resize(320, 240) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + done(); + }); }); - it('Rotate by 30 degrees, respecting output input size', function (done) { - sharp(fixtures.inputJpg).rotate(30).resize(320, 240).toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(true, data.length > 0); - assert.strictEqual('jpeg', info.format); - assert.strictEqual(397, info.width); - assert.strictEqual(368, info.height); - done(); - }); + it('Resize then rotate by 30 degrees, respecting output input size', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 240) + .rotate(30) + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(true, data.length > 0); + assert.strictEqual('jpeg', info.format); + assert.strictEqual(397, info.width); + assert.strictEqual(368, info.height); + done(); + }); }); - [-3690, -450, -90, 90, 450, 3690].forEach(function (angle) { - it('Rotate by any 90-multiple angle (' + angle + 'deg)', function (done) { - sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) { + [-3690, -450, -90, 90, 450, 3690].forEach((angle) => { + it(`Rotate by any 90-multiple angle (${angle}deg)`, (_t, done) => { + sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual(240, info.width); assert.strictEqual(320, info.height); @@ -83,9 +184,9 @@ describe('Rotation', function () { }); }); - [-3750, -510, -150, 30, 390, 3630].forEach(function (angle) { - it('Rotate by any 30-multiple angle (' + angle + 'deg)', function (done) { - sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) { + [-3750, -510, -150, 30, 390, 3630].forEach((angle) => { + it(`Rotate by any 30-multiple angle (${angle}deg)`, (_t, done) => { + sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual(397, info.width); assert.strictEqual(368, info.height); @@ -94,9 +195,9 @@ describe('Rotation', function () { }); }); - [-3780, -540, 0, 180, 540, 3780].forEach(function (angle) { - it('Rotate by any 180-multiple angle (' + angle + 'deg)', function (done) { - sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) { + [-3780, -540, 0, 180, 540, 3780].forEach((angle) => { + it(`Rotate by any 180-multiple angle (${angle}deg)`, (_t, done) => { + sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer((err, _data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -105,15 +206,15 @@ describe('Rotation', function () { }); }); - it('Rotate by 270 degrees, square output ignoring aspect ratio', function (done) { + it('Rotate by 270 degrees, square output ignoring aspect ratio', (_t, done) => { sharp(fixtures.inputJpg) .resize(240, 240, { fit: sharp.fit.fill }) .rotate(270) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(240, info.width); assert.strictEqual(240, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(240, metadata.width); assert.strictEqual(240, metadata.height); @@ -122,15 +223,15 @@ describe('Rotation', function () { }); }); - it('Rotate by 315 degrees, square output ignoring aspect ratio', function (done) { + it('Rotate by 315 degrees, square output ignoring aspect ratio', (_t, done) => { sharp(fixtures.inputJpg) .resize(240, 240, { fit: sharp.fit.fill }) .rotate(315) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(339, info.width); assert.strictEqual(339, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(339, metadata.width); assert.strictEqual(339, metadata.height); @@ -139,15 +240,15 @@ describe('Rotation', function () { }); }); - it('Rotate by 270 degrees, rectangular output ignoring aspect ratio', function (done) { + it('Rotate by 270 degrees, rectangular output ignoring aspect ratio', (_t, done) => { sharp(fixtures.inputJpg) - .resize(320, 240, { fit: sharp.fit.fill }) .rotate(270) - .toBuffer(function (err, data, info) { + .resize(320, 240, { fit: sharp.fit.fill }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(320, metadata.width); assert.strictEqual(240, metadata.height); @@ -156,15 +257,32 @@ describe('Rotation', function () { }); }); - it('Rotate by 30 degrees, rectangular output ignoring aspect ratio', function (done) { + it('Auto-rotate by 270 degrees, rectangular output ignoring aspect ratio', (_t, done) => { + sharp(fixtures.inputJpgWithLandscapeExif8) + .resize(320, 240, { fit: sharp.fit.fill }) + .rotate() + .toBuffer((err, data, info) => { + if (err) throw err; + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + sharp(data).metadata((err, metadata) => { + if (err) throw err; + assert.strictEqual(320, metadata.width); + assert.strictEqual(240, metadata.height); + done(); + }); + }); + }); + + it('Rotate by 30 degrees, rectangular output ignoring aspect ratio', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240, { fit: sharp.fit.fill }) .rotate(30) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(397, info.width); assert.strictEqual(368, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(397, metadata.width); assert.strictEqual(368, metadata.height); @@ -173,17 +291,17 @@ describe('Rotation', function () { }); }); - it('Input image has Orientation EXIF tag but do not rotate output', function (done) { + it('Input image has Orientation EXIF tag but do not rotate output', (_t, done) => { sharp(fixtures.inputJpgWithExif) .resize(320) .withMetadata() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(427, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(8, metadata.orientation); done(); @@ -191,11 +309,11 @@ describe('Rotation', function () { }); }); - it('Input image has Orientation EXIF tag value of 8 (270 degrees), auto-rotate', function (done) { + it('Input image has Orientation EXIF tag value of 8 (270 degrees), auto-rotate', (_t, done) => { sharp(fixtures.inputJpgWithExif) .rotate() .resize(320) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -204,17 +322,17 @@ describe('Rotation', function () { }); }); - it('Override EXIF Orientation tag metadata after auto-rotate', function (done) { + it('Override EXIF Orientation tag metadata after auto-rotate', (_t, done) => { sharp(fixtures.inputJpgWithExif) .rotate() .resize(320) .withMetadata({ orientation: 3 }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(3, metadata.orientation); fixtures.assertSimilar(fixtures.expected('exif-8.jpg'), data, done); @@ -222,17 +340,17 @@ describe('Rotation', function () { }); }); - it('Input image has Orientation EXIF tag value of 5 (270 degrees + flip), auto-rotate', function (done) { + it('Input image has Orientation EXIF tag value of 5 (270 degrees + flip), auto-rotate', (_t, done) => { sharp(fixtures.inputJpgWithExifMirroring) .rotate() .resize(320) .withMetadata() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(1, metadata.orientation); fixtures.assertSimilar(fixtures.expected('exif-5.jpg'), data, done); @@ -240,8 +358,8 @@ describe('Rotation', function () { }); }); - it('Attempt to auto-rotate using image that has no EXIF', function (done) { - sharp(fixtures.inputJpg).rotate().resize(320).toBuffer(function (err, data, info) { + it('Attempt to auto-rotate using image that has no EXIF', (_t, done) => { + sharp(fixtures.inputJpg).rotate().resize(320).toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -251,12 +369,12 @@ describe('Rotation', function () { }); }); - it('Attempt to auto-rotate image format without EXIF support', function (done) { + it('Attempt to auto-rotate image format without EXIF support', (_t, done) => { sharp(fixtures.inputPng) .rotate() .resize(320) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('jpeg', info.format); @@ -266,23 +384,100 @@ describe('Rotation', function () { }); }); - it('Rotate with a string argument, should fail', function () { - assert.throws(function () { + it('Rotate with a string argument, should fail', () => { + assert.throws(() => { sharp(fixtures.inputJpg).rotate('not-a-number'); }); }); - it('Flip - vertical', function (done) { + it('Animated image rotate-then-extract rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .rotate(1) + .extract({ + top: 1, + left: 1, + width: 10, + height: 10 + }) + .toBuffer(), + /Rotate is not supported for multi-page images/ + ) + ); + + it('Animated image extract-then-rotate rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .extract({ + top: 1, + left: 1, + width: 10, + height: 10 + }) + .rotate(1) + .toBuffer(), + /Rotate is not supported for multi-page images/ + ) + ); + + it('Animated image rotate 180', () => + assert.doesNotReject(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .rotate(180) + .toBuffer() + ) + ); + + it('Animated image rotate non-180 rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .rotate(90) + .toBuffer(), + /Rotate is not supported for multi-page images/ + ) + ); + + it('Multiple rotate emits warning', () => { + let warningMessage = ''; + const s = sharp(); + s.on('warning', (msg) => { warningMessage = msg; }); + s.rotate(90); + assert.strictEqual(warningMessage, ''); + s.rotate(180); + assert.strictEqual(warningMessage, 'ignoring previous rotate options'); + }); + + it('Multiple rotate: last one wins (cardinal)', (_t, done) => { + sharp(fixtures.inputJpg) + .rotate(45) + .rotate(90) + .toBuffer((err, _data, info) => { + if (err) throw err; + assert.strictEqual(2225, info.width); + assert.strictEqual(2725, info.height); + done(); + }); + }); + + it('Multiple rotate: last one wins (non cardinal)', (_t, done) => { + sharp(fixtures.inputJpg) + .rotate(90) + .rotate(45) + .toBuffer((err, _data, info) => { + if (err) throw err; + assert.strictEqual(3500, info.width); + assert.strictEqual(3500, info.height); + done(); + }); + }); + + it('Flip - vertical', (_t, done) => { sharp(fixtures.inputJpg) .resize(320) .flip() .withMetadata() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(261, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(1, metadata.orientation); fixtures.assertSimilar(fixtures.expected('flip.jpg'), data, done); @@ -290,17 +485,17 @@ describe('Rotation', function () { }); }); - it('Flop - horizontal', function (done) { + it('Flop - horizontal', (_t, done) => { sharp(fixtures.inputJpg) .resize(320) .flop() .withMetadata() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(261, info.height); - sharp(data).metadata(function (err, metadata) { + sharp(data).metadata((err, metadata) => { if (err) throw err; assert.strictEqual(1, metadata.orientation); fixtures.assertSimilar(fixtures.expected('flop.jpg'), data, done); @@ -308,11 +503,12 @@ describe('Rotation', function () { }); }); - it('Flip and flop', function (done) { + it('Flip and flop', (_t, done) => { sharp(fixtures.inputJpg) .resize(320) + .flip() .flop() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -321,12 +517,12 @@ describe('Rotation', function () { }); }); - it('Neither flip nor flop', function (done) { + it('Neither flip nor flop', (_t, done) => { sharp(fixtures.inputJpg) .resize(320) .flip(false) .flop(false) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -335,12 +531,12 @@ describe('Rotation', function () { }); }); - it('Auto-rotate and flip', function (done) { + it('Auto-rotate and flip', (_t, done) => { sharp(fixtures.inputJpgWithExif) .rotate() .flip() .resize(320) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -349,12 +545,12 @@ describe('Rotation', function () { }); }); - it('Auto-rotate and flop', function (done) { + it('Auto-rotate and flop', (_t, done) => { sharp(fixtures.inputJpgWithExif) .rotate() .flop() .resize(320) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -362,4 +558,122 @@ describe('Rotation', function () { fixtures.assertSimilar(fixtures.expected('rotate-and-flop.jpg'), data, done); }); }); + + it('Auto-rotate and shrink-on-load', async () => { + const [r, g, b] = await sharp(fixtures.inputJpgWithLandscapeExif3) + .rotate() + .resize(8) + .raw() + .toBuffer(); + + assert.strictEqual(r, 61); + assert.strictEqual(g, 74); + assert.strictEqual(b, 51); + }); + + it('Flip and rotate ordering', async () => { + const [r, g, b] = await sharp(fixtures.inputJpgWithPortraitExif5) + .flip() + .rotate(90) + .raw() + .toBuffer(); + + assert.strictEqual(r, 55); + assert.strictEqual(g, 65); + assert.strictEqual(b, 31); + }); + + it('Flip, rotate and resize ordering', async () => { + const [r, g, b] = await sharp(fixtures.inputJpgWithPortraitExif5) + .flip() + .rotate(90) + .resize(449) + .raw() + .toBuffer(); + + assert.strictEqual(r, 54); + assert.strictEqual(g, 64); + assert.strictEqual(b, 30); + }); + + it('Resize after affine-based rotation does not overcompute', async () => + sharp({ + create: { + width: 4640, + height: 2610, + channels: 3, + background: 'black' + } + }) + .rotate(28) + .resize({ width: 640, height: 360 }) + .raw() + .timeout({ seconds: 5 }) + .toBuffer() + ); + + it('Rotate 90 then resize with inside fit', async () => { + const data = await sharp({ create: { width: 16, height: 8, channels: 3, background: 'red' } }) + .rotate(90) + .resize({ width: 6, fit: 'inside' }) + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(width, 6); + assert.strictEqual(height, 12); + }); + + it('Resize with inside fit then rotate 90', async () => { + const data = await sharp({ create: { width: 16, height: 8, channels: 3, background: 'red' } }) + .resize({ width: 6, fit: 'inside' }) + .rotate(90) + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(width, 3); + assert.strictEqual(height, 6); + }); + + it('Shrink-on-load with autoOrient', async () => { + const data = await sharp(fixtures.inputJpgWithLandscapeExif6) + .resize(8) + .autoOrient() + .avif({ effort: 0 }) + .toBuffer(); + + const { width, height, orientation } = await sharp(data).metadata(); + assert.strictEqual(width, 8); + assert.strictEqual(height, 6); + assert.strictEqual(orientation, undefined); + }); + + it('Auto-orient and rotate 45', async () => { + const data = await sharp(fixtures.inputJpgWithLandscapeExif2, { autoOrient: true }) + .rotate(45) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(width, 742); + assert.strictEqual(height, 742); + }); + + it('Auto-orient, extract and rotate 45', async () => { + const data = await sharp(fixtures.inputJpgWithLandscapeExif2, { autoOrient: true }) + .extract({ left: 20, top: 20, width: 200, height: 100 }) + .rotate(45) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(width, 212); + assert.strictEqual(height, 212); + }); + + it('Invalid autoOrient throws', () => + assert.throws( + () => sharp({ autoOrient: 'fail' }), + /Expected boolean for autoOrient but received fail of type string/ + ) + ); }); diff --git a/test/unit/sharpen.js b/test/unit/sharpen.js index ab3b134e5..9ea354a71 100644 --- a/test/unit/sharpen.js +++ b/test/unit/sharpen.js @@ -1,16 +1,20 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Sharpen', function () { - it('specific radius 10 (sigma 6)', function (done) { +describe('Sharpen', () => { + it('specific radius 10 (sigma 6)', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .sharpen(6) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -19,11 +23,11 @@ describe('Sharpen', function () { }); }); - it('specific radius 3 (sigma 1.5) and levels 0.5, 2.5', function (done) { + it('specific radius 3 (sigma 1.5) and levels 0.5, 2.5', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .sharpen(1.5, 0.5, 2.5) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -32,11 +36,11 @@ describe('Sharpen', function () { }); }); - it('specific radius 5 (sigma 3.5) and levels 2, 4', function (done) { + it('specific radius 5 (sigma 3.5) and levels 2, 4', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .sharpen(3.5, 2, 4) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -45,12 +49,28 @@ describe('Sharpen', function () { }); }); + it('sigma=3.5, m1=2, m2=4', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 240) + .sharpen({ sigma: 3.5, m1: 2, m2: 4 }) + .toBuffer() + .then(data => fixtures.assertSimilar(fixtures.expected('sharpen-5-2-4.jpg'), data, done)); + }); + + it('sigma=3.5, m1=2, m2=4, x1=2, y2=5, y3=25', (_t, done) => { + sharp(fixtures.inputJpg) + .resize(320, 240) + .sharpen({ sigma: 3.5, m1: 2, m2: 4, x1: 2, y2: 5, y3: 25 }) + .toBuffer() + .then(data => fixtures.assertSimilar(fixtures.expected('sharpen-5-2-4.jpg'), data, done)); + }); + if (!process.env.SHARP_TEST_WITHOUT_CACHE) { - it('specific radius/levels with alpha channel', function (done) { + it('specific radius/levels with alpha channel', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 240) .sharpen(5, 4, 8) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels); @@ -61,11 +81,11 @@ describe('Sharpen', function () { }); } - it('mild sharpen', function (done) { + it('mild sharpen', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .sharpen() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -74,29 +94,59 @@ describe('Sharpen', function () { }); }); - it('invalid sigma', function () { - assert.throws(function () { + it('invalid sigma', () => { + assert.throws(() => { sharp(fixtures.inputJpg).sharpen(-1.5); }); }); - it('invalid flat', function () { - assert.throws(function () { + it('invalid flat', () => { + assert.throws(() => { sharp(fixtures.inputJpg).sharpen(1, -1); }); }); - it('invalid jagged', function () { - assert.throws(function () { + it('invalid jagged', () => { + assert.throws(() => { sharp(fixtures.inputJpg).sharpen(1, 1, -1); }); }); - it('sharpened image is larger than non-sharpened', function (done) { + it('invalid options.sigma', () => assert.throws( + () => sharp().sharpen({ sigma: -1 }), + /Expected number between 0\.000001 and 10 for options\.sigma but received -1 of type number/ + )); + + it('invalid options.m1', () => assert.throws( + () => sharp().sharpen({ sigma: 1, m1: -1 }), + /Expected number between 0 and 1000000 for options\.m1 but received -1 of type number/ + )); + + it('invalid options.m2', () => assert.throws( + () => sharp().sharpen({ sigma: 1, m2: -1 }), + /Expected number between 0 and 1000000 for options\.m2 but received -1 of type number/ + )); + + it('invalid options.x1', () => assert.throws( + () => sharp().sharpen({ sigma: 1, x1: -1 }), + /Expected number between 0 and 1000000 for options\.x1 but received -1 of type number/ + )); + + it('invalid options.y2', () => assert.throws( + () => sharp().sharpen({ sigma: 1, y2: -1 }), + /Expected number between 0 and 1000000 for options\.y2 but received -1 of type number/ + )); + + it('invalid options.y3', () => assert.throws( + () => sharp().sharpen({ sigma: 1, y3: -1 }), + /Expected number between 0 and 1000000 for options\.y3 but received -1 of type number/ + )); + + it('sharpened image is larger than non-sharpened', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .sharpen(false) - .toBuffer(function (err, notSharpened, info) { + .toBuffer((err, notSharpened, info) => { if (err) throw err; assert.strictEqual(true, notSharpened.length > 0); assert.strictEqual('jpeg', info.format); @@ -105,7 +155,7 @@ describe('Sharpen', function () { sharp(fixtures.inputJpg) .resize(320, 240) .sharpen(true) - .toBuffer(function (err, sharpened, info) { + .toBuffer((err, sharpened, info) => { if (err) throw err; assert.strictEqual(true, sharpened.length > 0); assert.strictEqual(true, sharpened.length > notSharpened.length); diff --git a/test/unit/stats.js b/test/unit/stats.js index 342b27dce..9164787e3 100644 --- a/test/unit/stats.js +++ b/test/unit/stats.js @@ -1,7 +1,11 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); @@ -18,13 +22,13 @@ function isInteger (val) { return Number.isInteger(val); } -describe('Image Stats', function () { - it('JPEG', function (done) { - sharp(fixtures.inputJpg).stats(function (err, stats) { +describe('Image Stats', () => { + it('JPEG', (_t, done) => { + sharp(fixtures.inputJpg).stats((err, stats) => { if (err) throw err; assert.strictEqual(true, stats.isOpaque); - assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541)); + assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659)); assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.7883011147075762)); const { r, g, b } = stats.dominant; @@ -84,8 +88,8 @@ describe('Image Stats', function () { }); }); - it('PNG without transparency', function (done) { - sharp(fixtures.inputPng).stats(function (err, stats) { + it('PNG without transparency', (_t, done) => { + sharp(fixtures.inputPng).stats((err, stats) => { if (err) throw err; assert.strictEqual(true, stats.isOpaque); @@ -116,8 +120,8 @@ describe('Image Stats', function () { }); }); - it('PNG with transparency', function (done) { - sharp(fixtures.inputPngWithTransparency).stats(function (err, stats) { + it('PNG with transparency', (_t, done) => { + sharp(fixtures.inputPngWithTransparency).stats((err, stats) => { if (err) throw err; assert.strictEqual(false, stats.isOpaque); @@ -197,8 +201,8 @@ describe('Image Stats', function () { }); }); - it('PNG fully transparent', function (done) { - sharp(fixtures.inputPngCompleteTransparency).stats(function (err, stats) { + it('PNG fully transparent', (_t, done) => { + sharp(fixtures.inputPngCompleteTransparency).stats((err, stats) => { if (err) throw err; assert.strictEqual(false, stats.isOpaque); @@ -230,8 +234,8 @@ describe('Image Stats', function () { }); }); - it('Tiff', function (done) { - sharp(fixtures.inputTiff).stats(function (err, stats) { + it('Tiff', (_t, done) => { + sharp(fixtures.inputTiff).stats((err, stats) => { if (err) throw err; assert.strictEqual(true, stats.isOpaque); @@ -263,13 +267,13 @@ describe('Image Stats', function () { }); }); - it('WebP', function (done) { - sharp(fixtures.inputWebP).stats(function (err, stats) { + it('WebP', (_t, done) => { + sharp(fixtures.inputWebP).stats((err, stats) => { if (err) throw err; assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.51758075132966)); - assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 9.959951636662941)); + assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 9.971384105278734)); const { r, g, b } = stats.dominant; assert.strictEqual(40, r); @@ -328,13 +332,13 @@ describe('Image Stats', function () { }); }); - it('GIF', function (done) { - sharp(fixtures.inputGif).stats(function (err, stats) { + it('GIF', (_t, done) => { + sharp(fixtures.inputGif).stats((err, stats) => { if (err) throw err; assert.strictEqual(true, stats.isOpaque); - assert.strictEqual(true, isInAcceptableRange(stats.entropy, 6.087309412541799)); - assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 2.9250574456255682)); + assert.strictEqual(true, isInAcceptableRange(stats.entropy, 6.08118048729375)); + assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 2.936767879098001)); const { r, g, b } = stats.dominant; assert.strictEqual(120, r); @@ -393,8 +397,8 @@ describe('Image Stats', function () { }); }); - it('Grayscale GIF with alpha', function (done) { - sharp(fixtures.inputGifGreyPlusAlpha).stats(function (err, stats) { + it('Grayscale GIF with alpha', (_t, done) => { + sharp(fixtures.inputGifGreyPlusAlpha).stats((err, stats) => { if (err) throw err; assert.strictEqual(false, stats.isOpaque); @@ -423,20 +427,20 @@ describe('Image Stats', function () { assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 1)); // alpha channel - assert.strictEqual(0, stats.channels[1].min); - assert.strictEqual(255, stats.channels[1].max); - assert.strictEqual(true, isInAcceptableRange(stats.channels[1].sum, 255)); - assert.strictEqual(true, isInAcceptableRange(stats.channels[1].squaresSum, 65025)); - assert.strictEqual(true, isInAcceptableRange(stats.channels[1].mean, 127.5)); - assert.strictEqual(true, isInAcceptableRange(stats.channels[1].stdev, 180.31222920256963)); - assert.strictEqual(true, isInteger(stats.channels[1].minX)); - assert.strictEqual(true, isInRange(stats.channels[1].minX, 0, 2)); - assert.strictEqual(true, isInteger(stats.channels[1].minY)); - assert.strictEqual(true, isInRange(stats.channels[1].minY, 0, 1)); - assert.strictEqual(true, isInteger(stats.channels[1].maxX)); - assert.strictEqual(true, isInRange(stats.channels[1].maxX, 0, 2)); - assert.strictEqual(true, isInteger(stats.channels[1].maxY)); - assert.strictEqual(true, isInRange(stats.channels[1].maxY, 0, 1)); + assert.strictEqual(0, stats.channels[3].min); + assert.strictEqual(255, stats.channels[3].max); + assert.strictEqual(true, isInAcceptableRange(stats.channels[3].sum, 255)); + assert.strictEqual(true, isInAcceptableRange(stats.channels[3].squaresSum, 65025)); + assert.strictEqual(true, isInAcceptableRange(stats.channels[3].mean, 127.5)); + assert.strictEqual(true, isInAcceptableRange(stats.channels[3].stdev, 180.31222920256963)); + assert.strictEqual(true, isInteger(stats.channels[3].minX)); + assert.strictEqual(true, isInRange(stats.channels[3].minX, 0, 2)); + assert.strictEqual(true, isInteger(stats.channels[3].minY)); + assert.strictEqual(true, isInRange(stats.channels[3].minY, 0, 1)); + assert.strictEqual(true, isInteger(stats.channels[3].maxX)); + assert.strictEqual(true, isInRange(stats.channels[3].maxX, 0, 2)); + assert.strictEqual(true, isInteger(stats.channels[3].maxY)); + assert.strictEqual(true, isInRange(stats.channels[3].maxY, 0, 1)); done(); }); @@ -475,13 +479,13 @@ describe('Image Stats', function () { assert.strictEqual(sharpness, 0); }); - it('Stream in, Callback out', function (done) { + it('Stream in, Callback out', (_t, done) => { const readable = fs.createReadStream(fixtures.inputJpg); - const pipeline = sharp().stats(function (err, stats) { + const pipeline = sharp().stats((err, stats) => { if (err) throw err; assert.strictEqual(true, stats.isOpaque); - assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541)); + assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659)); assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569)); const { r, g, b } = stats.dominant; @@ -542,14 +546,14 @@ describe('Image Stats', function () { readable.pipe(pipeline); }); - it('Stream in, Promise out', function () { + it('Stream in, Promise out', () => { const pipeline = sharp(); fs.createReadStream(fixtures.inputJpg).pipe(pipeline); - return pipeline.stats().then(function (stats) { + return pipeline.stats().then((stats) => { assert.strictEqual(true, stats.isOpaque); - assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541)); + assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659)); assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569)); const { r, g, b } = stats.dominant; @@ -604,15 +608,14 @@ describe('Image Stats', function () { assert.strictEqual(true, isInRange(stats.channels[0].maxX, 0, 2725)); assert.strictEqual(true, isInteger(stats.channels[0].maxY)); assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 2725)); - }).catch(function (err) { + }).catch((err) => { throw err; }); }); - it('File in, Promise out', function () { - return sharp(fixtures.inputJpg).stats().then(function (stats) { + it('File in, Promise out', () => sharp(fixtures.inputJpg).stats().then((stats) => { assert.strictEqual(true, stats.isOpaque); - assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541)); + assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659)); assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569)); const { r, g, b } = stats.dominant; @@ -667,10 +670,9 @@ describe('Image Stats', function () { assert.strictEqual(true, isInRange(stats.channels[0].maxX, 0, 2725)); assert.strictEqual(true, isInteger(stats.channels[0].maxY)); assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 2725)); - }).catch(function (err) { + }).catch((err) => { throw err; - }); - }); + })); it('Blurred image has lower sharpness than original', () => { const original = sharp(fixtures.inputJpg).stats(); @@ -679,55 +681,72 @@ describe('Image Stats', function () { return Promise .all([original, blurred]) .then(([original, blurred]) => { - assert.strictEqual(true, isInAcceptableRange(original.sharpness, 0.7883011147075476)); - assert.strictEqual(true, isInAcceptableRange(blurred.sharpness, 0.4791559805997398)); + assert.strictEqual(true, isInAcceptableRange(original.sharpness, 0.789046400439488)); + assert.strictEqual(true, isInAcceptableRange(blurred.sharpness, 0.47985138441709047)); }); }); - it('File input with corrupt header fails gracefully', function (done) { + it('File input with corrupt header fails gracefully', (_t, done) => { sharp(fixtures.inputJpgWithCorruptHeader) - .stats(function (err) { - assert.strictEqual(true, !!err); + .stats((err) => { + assert(err.message.includes('Input file has corrupt header')); + assert(err.stack.includes('at Sharp.stats')); + assert(err.stack.includes(__filename)); done(); }); }); - it('File input with corrupt header fails gracefully, Promise out', function () { - return sharp(fixtures.inputJpgWithCorruptHeader) - .stats().then(function (stats) { + it('Stream input with corrupt header fails gracefully', (_t, done) => { + fs.createReadStream(fixtures.inputJpgWithCorruptHeader).pipe( + sharp() + .stats((err) => { + assert(err.message.includes('Input buffer has corrupt header')); + assert(err.stack.includes('at Sharp.stats')); + assert(err.stack.includes(__filename)); + done(); + }) + ); + }); + + it('File input with corrupt header fails gracefully, Promise out', () => sharp(fixtures.inputJpgWithCorruptHeader) + .stats().then(() => { throw new Error('Corrupt Header file'); - }).catch(function (err) { + }).catch((err) => { assert.ok(!!err); - }); - }); + })); - it('File input with corrupt header fails gracefully, Stream In, Promise Out', function () { + it('File input with corrupt header fails gracefully, Stream In, Promise Out', () => { const pipeline = sharp(); fs.createReadStream(fixtures.inputJpgWithCorruptHeader).pipe(pipeline); return pipeline - .stats().then(function (stats) { + .stats().then(() => { throw new Error('Corrupt Header file'); - }).catch(function (err) { + }).catch((err) => { assert.ok(!!err); }); }); - it('Buffer input with corrupt header fails gracefully', function (done) { + it('Buffer input with corrupt header fails gracefully', (_t, done) => { sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) - .stats(function (err) { + .stats((err) => { assert.strictEqual(true, !!err); done(); }); }); - it('Non-existent file in, Promise out', function (done) { - sharp('fail').stats().then(function (stats) { + it('Non-existent file in, Promise out', (_t, done) => { + sharp('fail').stats().then(() => { throw new Error('Non-existent file'); - }, function (err) { + }, (err) => { assert.ok(!!err); done(); }); }); + + it('Sequential read option is ignored', async () => { + const { isOpaque } = await sharp(fixtures.inputJpg, { sequentialRead: true }).stats(); + assert.strictEqual(isOpaque, true); + }); }); diff --git a/test/unit/svg.js b/test/unit/svg.js index fd436e279..d11b61895 100644 --- a/test/unit/svg.js +++ b/test/unit/svg.js @@ -1,25 +1,29 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('SVG input', function () { - it('Convert SVG to PNG at default 72DPI', function (done) { +describe('SVG input', () => { + it('Convert SVG to PNG at default 72DPI', (_t, done) => { sharp(fixtures.inputSvg) .resize(1024) .extract({ left: 290, top: 760, width: 40, height: 40 }) .toFormat('png') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(40, info.width); assert.strictEqual(40, info.height); - fixtures.assertSimilar(fixtures.expected('svg72.png'), data, function (err) { + fixtures.assertSimilar(fixtures.expected('svg72.png'), data, (err) => { if (err) throw err; - sharp(data).metadata(function (err, info) { + sharp(data).metadata((err, info) => { if (err) throw err; assert.strictEqual(72, info.density); done(); @@ -28,19 +32,19 @@ describe('SVG input', function () { }); }); - it('Convert SVG to PNG at 1200DPI', function (done) { + it('Convert SVG to PNG at 1200DPI', (_t, done) => { sharp(fixtures.inputSvg, { density: 1200 }) .resize(1024) .extract({ left: 290, top: 760, width: 40, height: 40 }) .toFormat('png') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(40, info.width); assert.strictEqual(40, info.height); - fixtures.assertSimilar(fixtures.expected('svg1200.png'), data, function (err) { + fixtures.assertSimilar(fixtures.expected('svg1200.png'), data, (err) => { if (err) throw err; - sharp(data).metadata(function (err, info) { + sharp(data).metadata((err, info) => { if (err) throw err; assert.strictEqual(1200, info.density); done(); @@ -49,22 +53,22 @@ describe('SVG input', function () { }); }); - it('Convert SVG to PNG at DPI larger than 2400', function (done) { + it('Convert SVG to PNG at DPI larger than 2400', (_t, done) => { const size = 1024; - sharp(fixtures.inputSvgSmallViewBox).metadata(function (err, metadata) { + sharp(fixtures.inputSvgSmallViewBox).metadata((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) { + .toBuffer((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) { + fixtures.assertSimilar(fixtures.expected('circle.png'), data, (err) => { if (err) throw err; - sharp(data).metadata(function (err, info) { + sharp(data).metadata((err, info) => { if (err) throw err; assert.strictEqual(9216, info.density); done(); @@ -74,24 +78,45 @@ describe('SVG input', function () { }); }); - it('Convert SVG to PNG at 14.4DPI', function (done) { + it('Convert SVG to PNG utilizing scale-on-load', (_t, done) => { + const size = 1024; + sharp(fixtures.inputSvgSmallViewBox) + .resize(size) + .toFormat('png') + .toBuffer((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, (err) => { + if (err) throw err; + sharp(data).metadata((err, info) => { + if (err) throw err; + assert.strictEqual(72, info.density); + done(); + }); + }); + }); + }); + + it('Convert SVG to PNG at 14.4DPI', (_t, done) => { sharp(fixtures.inputSvg, { density: 14.4 }) .toFormat('png') - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(20, info.width); assert.strictEqual(20, info.height); - fixtures.assertSimilar(fixtures.expected('svg14.4.png'), data, function (err) { + fixtures.assertSimilar(fixtures.expected('svg14.4.png'), data, (err) => { if (err) throw err; done(); }); }); }); - it('Convert SVG with embedded images to PNG, respecting dimensions, autoconvert to PNG', function (done) { + it('Convert SVG with embedded images to PNG, respecting dimensions, autoconvert to PNG', (_t, done) => { sharp(fixtures.inputSvgWithEmbeddedImages) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(480, info.width); @@ -114,4 +139,60 @@ describe('SVG input', function () { assert.strictEqual(info.height, 240); assert.strictEqual(info.channels, 4); }); + + it('Can apply custom CSS', async () => { + const svg = `<?xml version="1.0" encoding="UTF-8"?> + <svg width="10" height="10" xmlns="http://www.w3.org/2000/svg"> + <circle cx="5" cy="5" r="4" fill="green" /> + </svg>`; + const stylesheet = 'circle { fill: red }'; + + const [r, g, b, a] = await sharp(Buffer.from(svg), { svg: { stylesheet } }) + .extract({ left: 5, top: 5, width: 1, height: 1 }) + .raw() + .toBuffer(); + + assert.deepEqual([r, g, b, a], [255, 0, 0, 255]); + }); + + it('Invalid stylesheet input option throws', () => + assert.throws( + () => sharp({ svg: { stylesheet: 123 } }), + /Expected string for svg\.stylesheet but received 123 of type number/ + ) + ); + + it('Valid highBitdepth input option does not throw', () => + assert.doesNotThrow( + () => sharp({ svg: { highBitdepth: true } }) + ) + ); + + it('Invalid highBitdepth input option throws', () => + assert.throws( + () => sharp({ svg: { highBitdepth: 123 } }), + /Expected boolean for svg\.highBitdepth but received 123 of type number/ + ) + ); + + it('Fails to render SVG larger than 32767x32767', () => + assert.rejects( + () => sharp(Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="32768" height="1" />')).toBuffer(), + /Input SVG image exceeds 32767x32767 pixel limit/ + ) + ); + + it('Fails to render scaled SVG larger than 32767x32767', () => + assert.rejects( + () => sharp(Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="32767" height="1" />')).resize(32768).toBuffer(), + /Input SVG image will exceed 32767x32767 pixel limit when scaled/ + ) + ); + + it('Detects SVG passed as a string', () => + assert.rejects( + () => sharp('<svg xmlns="http://www.w3.org/2000/svg"></svg>').toBuffer(), + /Input file is missing, did you mean/ + ) + ); }); diff --git a/test/unit/text.js b/test/unit/text.js new file mode 100644 index 000000000..029b45f1f --- /dev/null +++ b/test/unit/text.js @@ -0,0 +1,333 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); +const fixtures = require('../fixtures'); +const { inRange } = require('../../lib/is'); + +describe('Text to image', () => { + it('text with default values', async (t) => { + const output = fixtures.path('output.text-default.png'); + const text = sharp({ + text: { + text: 'Hello, world !' + } + }); + if (!sharp.versions.pango) { + return t.skip(); + } + const info = await text.png().toFile(output); + assert.strictEqual('png', info.format); + assert.strictEqual(3, info.channels); + assert.strictEqual(false, info.premultiplied); + assert.ok(info.width > 10); + assert.ok(info.height > 8); + const metadata = await sharp(output).metadata(); + assert.strictEqual('uchar', metadata.depth); + assert.strictEqual('srgb', metadata.space); + assert.strictEqual(72, metadata.density); + const stats = await sharp(output).stats(); + assert.strictEqual(0, stats.channels[0].min); + assert.strictEqual(255, stats.channels[0].max); + assert.strictEqual(0, stats.channels[1].min); + assert.strictEqual(255, stats.channels[1].max); + assert.strictEqual(0, stats.channels[2].min); + assert.strictEqual(255, stats.channels[2].max); + assert.ok(info.textAutofitDpi > 0); + }); + + it('text with width and height', async (t) => { + const output = fixtures.path('output.text-width-height.png'); + const text = sharp({ + text: { + text: 'Hello, world!', + width: 500, + height: 400 + } + }); + if (!sharp.versions.pango) { + return t.skip(); + } + const info = await text.toFile(output); + assert.strictEqual('png', info.format); + assert.strictEqual(3, info.channels); + assert.ok(inRange(info.width, 400, 600), `Actual width ${info.width}`); + assert.ok(inRange(info.height, 290, 500), `Actual height ${info.height}`); + assert.ok(inRange(info.textAutofitDpi, 900, 1300), `Actual textAutofitDpi ${info.textAutofitDpi}`); + }); + + it('text with dpi', async (t) => { + const output = fixtures.path('output.text-dpi.png'); + const dpi = 300; + const text = sharp({ + text: { + text: 'Hello, world!', + dpi + } + }); + if (!sharp.versions.pango) { + return t.skip(); + } + const info = await text.toFile(output); + assert.strictEqual('png', info.format); + const metadata = await sharp(output).metadata(); + assert.strictEqual(dpi, metadata.density); + }); + + it('text with color and pango markup', async (t) => { + const output = fixtures.path('output.text-color-pango.png'); + const dpi = 300; + const text = sharp({ + text: { + text: '<span foreground="red" font="100">red</span><span font="50" background="cyan">blue</span>', + rgba: true, + dpi + } + }); + if (!sharp.versions.pango) { + return t.skip(); + } + const info = await text.toFile(output); + assert.strictEqual('png', info.format); + assert.strictEqual(4, info.channels); + const metadata = await sharp(output).metadata(); + assert.strictEqual(dpi, metadata.density); + assert.strictEqual('uchar', metadata.depth); + assert.strictEqual(true, metadata.hasAlpha); + }); + + it('text with font', async (t) => { + const output = fixtures.path('output.text-with-font.png'); + const text = sharp({ + text: { + text: 'Hello, world!', + font: 'sans 100' + } + }); + if (!sharp.versions.pango) { + return t.skip(); + } + const info = await text.toFile(output); + assert.strictEqual('png', info.format); + assert.strictEqual(3, info.channels); + assert.ok(info.width > 30); + assert.ok(info.height > 10); + }); + + it('text with justify and composite', async (t) => { + const output = fixtures.path('output.text-composite.png'); + const width = 500; + const dpi = 300; + const text = sharp(fixtures.inputJpg) + .resize(width) + .composite([{ + input: { + text: { + text: '<span foreground="#ffff00">Watermark</span> <span foreground="white"><i>is cool</i></span>', + width: 300, + height: 300, + justify: true, + align: 'right', + spacing: 50, + rgba: true + } + }, + gravity: 'northeast' + }, { + input: { + text: { + text: '<span background="cyan">cool</span>', + font: 'sans 30', + dpi, + rgba: true + } + }, + left: 30, + top: 250 + }]); + if (!sharp.versions.pango) { + return t.skip(); + } + const info = await text.toFile(output); + assert.strictEqual('png', info.format); + assert.strictEqual(4, info.channels); + assert.strictEqual(width, info.width); + assert.strictEqual(true, info.premultiplied); + const metadata = await sharp(output).metadata(); + assert.strictEqual('srgb', metadata.space); + assert.strictEqual('uchar', metadata.depth); + assert.strictEqual(true, metadata.hasAlpha); + }); + + it('bad text input', () => { + assert.throws(() => { + sharp({ + text: { + } + }); + }); + }); + + it('fontfile input', () => { + assert.doesNotThrow(() => { + sharp({ + text: { + text: 'text', + fontfile: 'UnknownFont.ttf' + } + }); + }); + }); + + it('bad font input', () => { + assert.throws(() => { + sharp({ + text: { + text: 'text', + font: 12 + } + }); + }); + }); + + it('bad fontfile input', () => { + assert.throws(() => { + sharp({ + text: { + text: 'text', + fontfile: true + } + }); + }); + }); + + it('invalid width', () => { + assert.throws( + () => sharp({ text: { text: 'text', width: 'bad' } }), + /Expected positive integer for text\.width but received bad of type string/ + ); + assert.throws( + () => sharp({ text: { text: 'text', width: 0.1 } }), + /Expected positive integer for text\.width but received 0.1 of type number/ + ); + assert.throws( + () => sharp({ text: { text: 'text', width: -1 } }), + /Expected positive integer for text\.width but received -1 of type number/ + ); + }); + + it('invalid height', () => { + assert.throws( + () => sharp({ text: { text: 'text', height: 'bad' } }), + /Expected positive integer for text\.height but received bad of type string/ + ); + assert.throws( + () => sharp({ text: { text: 'text', height: 0.1 } }), + /Expected positive integer for text\.height but received 0.1 of type number/ + ); + assert.throws( + () => sharp({ text: { text: 'text', height: -1 } }), + /Expected positive integer for text\.height but received -1 of type number/ + ); + }); + + it('bad align input', () => { + assert.throws(() => { + sharp({ + text: { + text: 'text', + align: 'unknown' + } + }); + }); + }); + + it('bad justify input', () => { + assert.throws(() => { + sharp({ + text: { + text: 'text', + justify: 'unknown' + } + }); + }); + }); + + it('invalid dpi', () => { + assert.throws( + () => sharp({ text: { text: 'text', dpi: 'bad' } }), + /Expected integer between 1 and 1000000 for text\.dpi but received bad of type string/ + ); + assert.throws( + () => sharp({ text: { text: 'text', dpi: 0.1 } }), + /Expected integer between 1 and 1000000 for text\.dpi but received 0.1 of type number/ + ); + assert.throws( + () => sharp({ text: { text: 'text', dpi: -1 } }), + /Expected integer between 1 and 1000000 for text\.dpi but received -1 of type number/ + ); + }); + + it('bad rgba input', () => { + assert.throws(() => { + sharp({ + text: { + text: 'text', + rgba: -10 + } + }); + }); + }); + + it('invalid spacing', () => { + assert.throws( + () => sharp({ text: { text: 'text', spacing: 'bad' } }), + /Expected integer between -1000000 and 1000000 for text\.spacing but received bad of type string/ + ); + assert.throws( + () => sharp({ text: { text: 'text', spacing: 0.1 } }), + /Expected integer between -1000000 and 1000000 for text\.spacing but received 0.1 of type number/ + ); + assert.throws( + () => sharp({ text: { text: 'text', spacing: -1000001 } }), + /Expected integer between -1000000 and 1000000 for text\.spacing but received -1000001 of type number/ + ); + }); + + it('only height or dpi not both', () => { + assert.throws(() => { + sharp({ + text: { + text: 'text', + height: 400, + dpi: 100 + } + }); + }); + }); + + it('valid wrap throws', () => { + assert.doesNotThrow(() => sharp({ text: { text: 'text', wrap: 'none' } })); + assert.doesNotThrow(() => sharp({ text: { text: 'text', wrap: 'word-char' } })); + }); + + it('invalid wrap throws', () => { + assert.throws( + () => sharp({ text: { text: 'text', wrap: 1 } }), + /Expected one of: word, char, word-char, none for text\.wrap but received 1 of type number/ + ); + assert.throws( + () => sharp({ text: { text: 'text', wrap: false } }), + /Expected one of: word, char, word-char, none for text\.wrap but received false of type boolean/ + ); + assert.throws( + () => sharp({ text: { text: 'text', wrap: 'invalid' } }), + /Expected one of: word, char, word-char, none for text\.wrap but received invalid of type string/ + ); + }); +}); diff --git a/test/unit/threshold.js b/test/unit/threshold.js index f1d6af77f..1ffb0f9cc 100644 --- a/test/unit/threshold.js +++ b/test/unit/threshold.js @@ -1,16 +1,20 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Threshold', function () { - it('threshold 1 jpeg', function (done) { +describe('Threshold', () => { + it('threshold 1 jpeg', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold(1) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -19,11 +23,11 @@ describe('Threshold', function () { }); }); - it('threshold 40 jpeg', function (done) { + it('threshold 40 jpeg', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold(40) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -32,11 +36,11 @@ describe('Threshold', function () { }); }); - it('threshold 128', function (done) { + it('threshold 128', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold(128) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -45,11 +49,11 @@ describe('Threshold', function () { }); }); - it('threshold true (=128)', function (done) { + it('threshold true (=128)', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold(true) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -58,20 +62,20 @@ describe('Threshold', function () { }); }); - it('threshold false (=0)', function (done) { + it('threshold false (=0)', (_t, done) => { sharp(fixtures.inputJpg) .threshold(false) - .toBuffer(function (err, data, info) { + .toBuffer((err, data) => { if (err) throw err; fixtures.assertSimilar(fixtures.inputJpg, data, done); }); }); - it('threshold grayscale: true (=128)', function (done) { + it('threshold grayscale: true (=128)', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold(128, { grayscale: true }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -80,11 +84,11 @@ describe('Threshold', function () { }); }); - it('threshold default jpeg', function (done) { + it('threshold default jpeg', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -93,11 +97,11 @@ describe('Threshold', function () { }); }); - it('threshold default png transparency', function (done) { + it('threshold default png transparency', (_t, done) => { sharp(fixtures.inputPngWithTransparency) .resize(320, 240) .threshold() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -106,11 +110,11 @@ describe('Threshold', function () { }); }); - it('threshold default png alpha', function (done) { + it('threshold default png alpha', (_t, done) => { sharp(fixtures.inputPngWithGreyAlpha) .resize(320, 240) .threshold() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); @@ -119,21 +123,21 @@ describe('Threshold', function () { }); }); - it('threshold default webp transparency', function (done) { + it('threshold default webp transparency', (_t, done) => { sharp(fixtures.inputWebPWithTransparency) .threshold() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('webp', info.format); fixtures.assertSimilar(fixtures.expected('threshold-128-transparency.webp'), data, done); }); }); - it('color threshold', function (done) { + it('color threshold', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .threshold(128, { grayscale: false }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); @@ -142,14 +146,14 @@ describe('Threshold', function () { }); }); - it('invalid threshold -1', function () { - assert.throws(function () { + it('invalid threshold -1', () => { + assert.throws(() => { sharp().threshold(-1); }); }); - it('invalid threshold 256', function () { - assert.throws(function () { + it('invalid threshold 256', () => { + assert.throws(() => { sharp().threshold(256); }); }); diff --git a/test/unit/tiff.js b/test/unit/tiff.js index b3d3abbd0..be5e5535f 100644 --- a/test/unit/tiff.js +++ b/test/unit/tiff.js @@ -1,22 +1,24 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); -const promisify = require('util').promisify; -const rimraf = require('rimraf'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); const outputTiff = fixtures.path('output.tiff'); -describe('TIFF', function () { - it('Load TIFF from Buffer', function (done) { +describe('TIFF', () => { + it('Load TIFF from Buffer', (_t, done) => { const inputTiffBuffer = fs.readFileSync(fixtures.inputTiff); sharp(inputTiffBuffer) .resize(320, 240) .jpeg() - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); @@ -27,10 +29,10 @@ describe('TIFF', function () { }); }); - it('Load multi-page TIFF from file', function (done) { + it('Load multi-page TIFF from file', (_t, done) => { sharp(fixtures.inputTiffMultipage) // defaults to page 0 .jpeg() - .toBuffer(function (err, defaultData, defaultInfo) { + .toBuffer((err, defaultData, defaultInfo) => { if (err) throw err; assert.strictEqual(true, defaultData.length > 0); assert.strictEqual(defaultData.length, defaultInfo.size); @@ -38,7 +40,7 @@ describe('TIFF', function () { sharp(fixtures.inputTiffMultipage, { page: 1 }) // 50%-scale copy of page 0 .jpeg() - .toBuffer(function (err, scaledData, scaledInfo) { + .toBuffer((err, scaledData, scaledInfo) => { if (err) throw err; assert.strictEqual(true, scaledData.length > 0); assert.strictEqual(scaledData.length, scaledInfo.size); @@ -50,11 +52,11 @@ describe('TIFF', function () { }); }); - it('Load multi-page TIFF from Buffer', function (done) { + it('Load multi-page TIFF from Buffer', (_t, done) => { const inputTiffBuffer = fs.readFileSync(fixtures.inputTiffMultipage); sharp(inputTiffBuffer) // defaults to page 0 .jpeg() - .toBuffer(function (err, defaultData, defaultInfo) { + .toBuffer((err, defaultData, defaultInfo) => { if (err) throw err; assert.strictEqual(true, defaultData.length > 0); assert.strictEqual(defaultData.length, defaultInfo.size); @@ -62,7 +64,7 @@ describe('TIFF', function () { sharp(inputTiffBuffer, { page: 1 }) // 50%-scale copy of page 0 .jpeg() - .toBuffer(function (err, scaledData, scaledInfo) { + .toBuffer((err, scaledData, scaledInfo) => { if (err) throw err; assert.strictEqual(true, scaledData.length > 0); assert.strictEqual(scaledData.length, scaledInfo.size); @@ -74,10 +76,10 @@ describe('TIFF', function () { }); }); - it('Save TIFF to Buffer', function (done) { + it('Save TIFF to Buffer', (_t, done) => { sharp(fixtures.inputTiff) .resize(320, 240) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual(data.length, info.size); @@ -103,19 +105,19 @@ describe('TIFF', function () { ) ); - it('Invalid TIFF quality throws error', function () { - assert.throws(function () { + it('Invalid TIFF quality throws error', () => { + assert.throws(() => { sharp().tiff({ quality: 101 }); }); }); - it('Missing TIFF quality does not throw error', function () { - assert.doesNotThrow(function () { + it('Missing TIFF quality does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff(); }); }); - it('Not squashing TIFF to a bit depth of 1 should not change the file size', function (done) { + it('Not squashing TIFF to a bit depth of 1 should not change the file size', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size; sharp(fixtures.inputTiff8BitDepth) .toColourspace('b-w') // can only squash 1 band uchar images @@ -128,11 +130,11 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert.strictEqual(startSize, info.size); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('Squashing TIFF to a bit depth of 1 should significantly reduce file size', function (done) { + it('Squashing TIFF to a bit depth of 1 should significantly reduce file size', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size; sharp(fixtures.inputTiff8BitDepth) .toColourspace('b-w') // can only squash 1 band uchar images @@ -145,12 +147,12 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(info.size < (startSize / 2)); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('Invalid TIFF bitdepth value throws error', function () { - assert.throws(function () { + it('Invalid TIFF bitdepth value throws error', () => { + assert.throws(() => { sharp().tiff({ bitdepth: 3 }); }, /Error: Expected 1, 2, 4 or 8 for bitdepth but received 3 of type number/); }); @@ -167,7 +169,7 @@ describe('TIFF', function () { .metadata() .then(({ density }) => { assert.strictEqual(25400, density); - return promisify(rimraf)(outputTiff); + return fs.promises.rm(outputTiff); }) ) ); @@ -188,19 +190,39 @@ describe('TIFF', function () { ) ); - it('TIFF invalid xres value should throw an error', function () { - assert.throws(function () { + it('TIFF imputes xres and yres from withMetadataDensity if not explicitly provided', async () => { + const data = await sharp(fixtures.inputTiff) + .resize(8, 8) + .tiff() + .withMetadata({ density: 600 }) + .toBuffer(); + const { density } = await sharp(data).metadata(); + assert.strictEqual(600, density); + }); + + it('TIFF uses xres and yres over withMetadataDensity if explicitly provided', async () => { + const data = await sharp(fixtures.inputTiff) + .resize(8, 8) + .tiff({ xres: 1000, yres: 1000 }) + .withMetadata({ density: 600 }) + .toBuffer(); + const { density } = await sharp(data).metadata(); + assert.strictEqual(25400, density); + }); + + it('TIFF invalid xres value should throw an error', () => { + assert.throws(() => { sharp().tiff({ xres: '1000.0' }); }); }); - it('TIFF invalid yres value should throw an error', function () { - assert.throws(function () { + it('TIFF invalid yres value should throw an error', () => { + assert.throws(() => { sharp().tiff({ yres: '1000.0' }); }); }); - it('TIFF lzw compression with horizontal predictor shrinks test file', function (done) { + it('TIFF lzw compression with horizontal predictor shrinks test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; sharp(fixtures.inputTiffUncompressed) .tiff({ @@ -212,7 +234,7 @@ describe('TIFF', function () { assert.strictEqual('tiff', info.format); assert.strictEqual(3, info.channels); assert(info.size < startSize); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); @@ -252,7 +274,7 @@ describe('TIFF', function () { }) ); - it('TIFF ccittfax4 compression shrinks b-w test file', function (done) { + it('TIFF ccittfax4 compression shrinks b-w test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiff).size; sharp(fixtures.inputTiff) .toColourspace('b-w') @@ -264,11 +286,35 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(info.size < startSize); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('TIFF deflate compression with horizontal predictor shrinks test file', function (done) { + it('TIFF resolutionUnit of inch (default)', async () => { + const data = await sharp({ create: { width: 8, height: 8, channels: 3, background: 'red' } }) + .tiff() + .toBuffer(); + const { resolutionUnit } = await sharp(data).metadata(); + assert.strictEqual(resolutionUnit, 'inch'); + }); + + it('TIFF resolutionUnit of inch', async () => { + const data = await sharp({ create: { width: 8, height: 8, channels: 3, background: 'red' } }) + .tiff({ resolutionUnit: 'inch' }) + .toBuffer(); + const { resolutionUnit } = await sharp(data).metadata(); + assert.strictEqual(resolutionUnit, 'inch'); + }); + + it('TIFF resolutionUnit of cm', async () => { + const data = await sharp({ create: { width: 8, height: 8, channels: 3, background: 'red' } }) + .tiff({ resolutionUnit: 'cm' }) + .toBuffer(); + const { resolutionUnit } = await sharp(data).metadata(); + assert.strictEqual(resolutionUnit, 'cm'); + }); + + it('TIFF deflate compression with horizontal predictor shrinks test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; sharp(fixtures.inputTiffUncompressed) .tiff({ @@ -279,11 +325,11 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(info.size < startSize); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('TIFF deflate compression with float predictor shrinks test file', function (done) { + it('TIFF deflate compression with float predictor shrinks test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; sharp(fixtures.inputTiffUncompressed) .tiff({ @@ -294,11 +340,11 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(startSize > info.size); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('TIFF deflate compression without predictor shrinks test file', function (done) { + it('TIFF deflate compression without predictor shrinks test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; sharp(fixtures.inputTiffUncompressed) .tiff({ @@ -309,11 +355,11 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(info.size < startSize); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('TIFF jpeg compression shrinks test file', function (done) { + it('TIFF jpeg compression shrinks test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; sharp(fixtures.inputTiffUncompressed) .tiff({ @@ -323,65 +369,83 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(info.size < startSize); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('TIFF none compression does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF none compression does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ compression: 'none' }); }); }); - it('TIFF lzw compression does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF lzw compression does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ compression: 'lzw' }); }); }); - it('TIFF deflate compression does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF deflate compression does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ compression: 'deflate' }); }); }); - it('TIFF invalid compression option throws', function () { - assert.throws(function () { + it('TIFF invalid compression option throws', () => { + assert.throws(() => { sharp().tiff({ compression: 0 }); }); }); - it('TIFF invalid compression option throws', function () { - assert.throws(function () { + it('TIFF invalid compression option throws', () => { + assert.throws(() => { sharp().tiff({ compression: 'a' }); }); }); - it('TIFF invalid predictor option throws', function () { - assert.throws(function () { + it('TIFF bigtiff true value does not throw error', () => { + assert.doesNotThrow(() => { + sharp().tiff({ bigtiff: true }); + }); + }); + + it('Invalid TIFF bigtiff value throws error', () => { + assert.throws(() => { + sharp().tiff({ bigtiff: 'true' }); + }); + }); + + it('TIFF invalid predictor option throws', () => { + assert.throws(() => { sharp().tiff({ predictor: 'a' }); }); }); - it('TIFF horizontal predictor does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF invalid resolutionUnit option throws', () => { + assert.throws(() => { + sharp().tiff({ resolutionUnit: 'none' }); + }); + }); + + it('TIFF horizontal predictor does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ predictor: 'horizontal' }); }); }); - it('TIFF float predictor does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF float predictor does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ predictor: 'float' }); }); }); - it('TIFF none predictor does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF none predictor does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ predictor: 'none' }); }); }); - it('TIFF tiled pyramid image without compression enlarges test file', function (done) { + it('TIFF tiled pyramid image without compression enlarges test file', (_t, done) => { const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; sharp(fixtures.inputTiffUncompressed) .tiff({ @@ -395,81 +459,93 @@ describe('TIFF', function () { if (err) throw err; assert.strictEqual('tiff', info.format); assert(info.size > startSize); - rimraf(outputTiff, done); + fs.rm(outputTiff, done); }); }); - it('TIFF pyramid true value does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF pyramid true value does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ pyramid: true }); }); }); - it('Invalid TIFF pyramid value throws error', function () { - assert.throws(function () { + it('Invalid TIFF pyramid value throws error', () => { + assert.throws(() => { sharp().tiff({ pyramid: 'true' }); }); }); - it('Invalid TIFF tile value throws error', function () { - assert.throws(function () { + it('TIFF miniswhite true value does not throw error', () => { + assert.doesNotThrow(() => { + sharp().tiff({ miniswhite: true }); + }); + }); + + it('Invalid TIFF miniswhite value throws error', () => { + assert.throws(() => { + sharp().tiff({ miniswhite: 'true' }); + }); + }); + + it('Invalid TIFF tile value throws error', () => { + assert.throws(() => { sharp().tiff({ tile: 'true' }); }); }); - it('TIFF tile true value does not throw error', function () { - assert.doesNotThrow(function () { + it('TIFF tile true value does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ tile: true }); }); }); - it('Valid TIFF tileHeight value does not throw error', function () { - assert.doesNotThrow(function () { + it('Valid TIFF tileHeight value does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ tileHeight: 512 }); }); }); - it('Valid TIFF tileWidth value does not throw error', function () { - assert.doesNotThrow(function () { + it('Valid TIFF tileWidth value does not throw error', () => { + assert.doesNotThrow(() => { sharp().tiff({ tileWidth: 512 }); }); }); - it('Invalid TIFF tileHeight value throws error', function () { - assert.throws(function () { + it('Invalid TIFF tileHeight value throws error', () => { + assert.throws(() => { sharp().tiff({ tileHeight: '256' }); }); }); - it('Invalid TIFF tileWidth value throws error', function () { - assert.throws(function () { + it('Invalid TIFF tileWidth value throws error', () => { + assert.throws(() => { sharp().tiff({ tileWidth: '256' }); }); }); - it('Invalid TIFF tileHeight value throws error', function () { - assert.throws(function () { + it('Invalid TIFF tileHeight value throws error', () => { + assert.throws(() => { sharp().tiff({ tileHeight: 0 }); }); }); - it('Invalid TIFF tileWidth value throws error', function () { - assert.throws(function () { + it('Invalid TIFF tileWidth value throws error', () => { + assert.throws(() => { sharp().tiff({ tileWidth: 0 }); }); }); - it('TIFF file input with invalid page fails gracefully', function (done) { + it('TIFF file input with invalid page fails gracefully', (_t, done) => { sharp(fixtures.inputTiffMultipage, { page: 2 }) - .toBuffer(function (err) { + .toBuffer((err) => { assert.strictEqual(true, !!err); done(); }); }); - it('TIFF buffer input with invalid page fails gracefully', function (done) { + it('TIFF buffer input with invalid page fails gracefully', (_t, done) => { sharp(fs.readFileSync(fixtures.inputTiffMultipage), { page: 2 }) - .toBuffer(function (err) { + .toBuffer((err) => { assert.strictEqual(true, !!err); done(); }); diff --git a/test/unit/tile.js b/test/unit/tile.js index 171305c92..8d477ebfa 100644 --- a/test/unit/tile.js +++ b/test/unit/tile.js @@ -1,38 +1,39 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); +const fs = require('node:fs'); +const path = require('node:path'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); -const eachLimit = require('async/eachLimit'); -const rimraf = require('rimraf'); -const DecompressZip = require('decompress-zip'); +const extractZip = require('extract-zip'); const sharp = require('../../'); const fixtures = require('../fixtures'); // Verifies all tiles in a given dz output directory are <= size -const assertDeepZoomTiles = function (directory, expectedSize, expectedLevels, done) { +const assertDeepZoomTiles = (directory, expectedSize, expectedLevels, done) => { // Get levels - const levels = fs.readdirSync(directory); + const dirents = fs.readdirSync(directory, { withFileTypes: true }); + const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name); assert.strictEqual(expectedLevels, levels.length); // Get tiles const tiles = []; - levels.forEach(function (level) { + levels.forEach((level) => { // Verify level directory name assert.strictEqual(true, /^[0-9]+$/.test(level)); - fs.readdirSync(path.join(directory, level)).forEach(function (tile) { + fs.readdirSync(path.join(directory, level)).forEach((tile) => { // Verify tile file name assert.strictEqual(true, /^[0-9]+_[0-9]+\.jpeg$/.test(tile)); tiles.push(path.join(directory, level, tile)); }); }); // Verify each tile is <= expectedSize - eachLimit(tiles, 8, function (tile, done) { - sharp(tile).metadata(function (err, metadata) { - if (err) { - done(err); - } else { + Promise.all(tiles.map((tile) => sharp(tile) + .metadata() + .then((metadata) => { assert.strictEqual('jpeg', metadata.format); assert.strictEqual('srgb', metadata.space); assert.strictEqual(3, metadata.channels); @@ -40,23 +41,22 @@ const assertDeepZoomTiles = function (directory, expectedSize, expectedLevels, d assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(true, metadata.width <= expectedSize); assert.strictEqual(true, metadata.height <= expectedSize); - done(); - } - }); - }, done); + }))) + .then(() => done()) + .catch(done); }; -const assertZoomifyTiles = function (directory, expectedTileSize, expectedLevels, done) { - fs.stat(path.join(directory, 'ImageProperties.xml'), function (err, stat) { +const assertZoomifyTiles = (directory, expectedLevels, done) => { + fs.stat(path.join(directory, 'ImageProperties.xml'), (err, stat) => { if (err) throw err; assert.ok(stat.isFile()); assert.ok(stat.size > 0); let maxTileLevel = -1; - fs.readdirSync(path.join(directory, 'TileGroup0')).forEach(function (tile) { + fs.readdirSync(path.join(directory, 'TileGroup0')).forEach((tile) => { // Verify tile file name assert.ok(/^[0-9]+-[0-9]+-[0-9]+\.jpg$/.test(tile)); - const level = parseInt(tile.split('-')[0]); + const level = Number(tile.split('-')[0]); maxTileLevel = Math.max(maxTileLevel, level); }); @@ -66,22 +66,24 @@ const assertZoomifyTiles = function (directory, expectedTileSize, expectedLevels }); }; -const assertGoogleTiles = function (directory, expectedTileSize, expectedLevels, done) { - const levels = fs.readdirSync(directory); - assert.strictEqual(expectedLevels, levels.length - 1); // subtract one to account for default blank tile +const assertGoogleTiles = (directory, expectedLevels, done) => { + // Get levels + const dirents = fs.readdirSync(directory, { withFileTypes: true }); + const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name); + assert.strictEqual(expectedLevels, levels.length); - fs.stat(path.join(directory, 'blank.png'), function (err, stat) { + fs.stat(path.join(directory, 'blank.png'), (err, stat) => { if (err) throw err; assert.ok(stat.isFile()); assert.ok(stat.size > 0); // Basic check to confirm lowest and highest level tiles exist - fs.stat(path.join(directory, '0', '0', '0.jpg'), function (err, stat) { + fs.stat(path.join(directory, '0', '0', '0.jpg'), (err, stat) => { if (err) throw err; assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.size > 0); - fs.stat(path.join(directory, (expectedLevels - 1).toString(), '0', '0.jpg'), function (err, stat) { + fs.stat(path.join(directory, (expectedLevels - 1).toString(), '0', '0.jpg'), (err, stat) => { if (err) throw err; assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.size > 0); @@ -92,9 +94,10 @@ const assertGoogleTiles = function (directory, expectedTileSize, expectedLevels, }; // Verifies tiles at specified level in a given output directory are > size+overlap -const assertTileOverlap = function (directory, tileSize, done) { +const assertTileOverlap = (directory, tileSize, done) => { // Get sorted levels - const levels = fs.readdirSync(directory).sort((a, b) => a - b); + const dirents = fs.readdirSync(directory, { withFileTypes: true }); + const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name).sort((a, b) => a - b); // Select the highest tile level const highestLevel = levels[levels.length - 1]; // Get sorted tiles from greatest level @@ -102,7 +105,7 @@ const assertTileOverlap = function (directory, tileSize, done) { // Select a tile from the approximate center of the image const squareTile = path.join(directory, highestLevel, tiles[Math.floor(tiles.length / 2)]); - sharp(squareTile).metadata(function (err, metadata) { + sharp(squareTile).metadata((err, metadata) => { if (err) { throw err; } else { @@ -114,134 +117,129 @@ const assertTileOverlap = function (directory, tileSize, done) { }); }; -describe('Tile', function () { - it('Valid size values pass', function () { - [1, 8192].forEach(function (size) { - assert.doesNotThrow(function () { +describe('Tile', () => { + it('Valid size values pass', () => { + [1, 8192].forEach((size) => { + assert.doesNotThrow(() => { sharp().tile({ - size: size + size }); }); }); }); - it('Invalid size values fail', function () { - ['zoinks', 1.1, -1, 0, 8193].forEach(function (size) { - assert.throws(function () { + it('Invalid size values fail', () => { + ['zoinks', 1.1, -1, 0, 8193].forEach((size) => { + assert.throws(() => { sharp().tile({ - size: size + size }); }); }); }); - it('Valid overlap values pass', function () { - [0, 8192].forEach(function (overlap) { - assert.doesNotThrow(function () { + it('Valid overlap values pass', () => { + [0, 8192].forEach((overlap) => { + assert.doesNotThrow(() => { sharp().tile({ size: 8192, - overlap: overlap + overlap }); }); }); }); - it('Invalid overlap values fail', function () { - ['zoinks', 1.1, -1, 8193].forEach(function (overlap) { - assert.throws(function () { + it('Invalid overlap values fail', () => { + ['zoinks', 1.1, -1, 8193].forEach((overlap) => { + assert.throws(() => { sharp().tile({ - overlap: overlap + overlap }); }); }); }); - it('Valid container values pass', function () { - ['fs', 'zip'].forEach(function (container) { - assert.doesNotThrow(function () { + it('Valid container values pass', () => { + ['fs', 'zip'].forEach((container) => { + assert.doesNotThrow(() => { sharp().tile({ - container: container + container }); }); }); }); - it('Invalid container values fail', function () { - ['zoinks', 1].forEach(function (container) { - assert.throws(function () { + it('Invalid container values fail', () => { + ['zoinks', 1].forEach((container) => { + assert.throws(() => { sharp().tile({ - container: container + container }); }); }); }); - it('Valid layout values pass', function () { - ['dz', 'google', 'zoomify'].forEach(function (layout) { - assert.doesNotThrow(function () { + it('Valid layout values pass', () => { + ['dz', 'google', 'zoomify'].forEach((layout) => { + assert.doesNotThrow(() => { sharp().tile({ - layout: layout + layout }); }); }); }); - it('Invalid layout values fail', function () { - ['zoinks', 1].forEach(function (layout) { - assert.throws(function () { + it('Invalid layout values fail', () => { + ['zoinks', 1].forEach((layout) => { + assert.throws(() => { sharp().tile({ - layout: layout + layout }); }); }); }); - it('Valid formats pass', function () { - ['jpeg', 'png', 'webp'].forEach(function (format) { - assert.doesNotThrow(function () { + it('Valid formats pass', () => { + ['jpeg', 'png', 'webp'].forEach((format) => { + assert.doesNotThrow(() => { sharp().toFormat(format).tile(); }); }); }); - it('Invalid formats fail', function () { - ['tiff', 'raw'].forEach(function (format) { - assert.throws(function () { + it('Invalid formats fail', () => { + ['tiff', 'raw'].forEach((format) => { + assert.throws(() => { sharp().toFormat(format).tile(); }); }); }); - it('Valid depths pass', function () { - ['onepixel', 'onetile', 'one'].forEach(function (depth) { - assert.doesNotThrow(function (depth) { - sharp().tile({ - depth: depth - }); - }); + it('Valid depths pass', () => { + ['onepixel', 'onetile', 'one'].forEach((depth) => { + assert.doesNotThrow(() => sharp().tile({ depth })); }); }); - it('Invalid depths fail', function () { - ['depth', 1].forEach(function (depth) { - assert.throws(function () { - sharp().tile({ - depth: depth - }); - }); + it('Invalid depths fail', () => { + ['depth', 1].forEach((depth) => { + assert.throws( + () => sharp().tile({ depth }), + /Expected one of: onepixel, onetile, one for depth but received/ + ); }); }); - it('Prevent larger overlap than default size', function () { - assert.throws(function () { + it('Prevent larger overlap than default size', () => { + assert.throws(() => { sharp().tile({ overlap: 257 }); }); }); - it('Prevent larger overlap than provided size', function () { - assert.throws(function () { + it('Prevent larger overlap than provided size', () => { + assert.throws(() => { sharp().tile({ size: 512, overlap: 513 @@ -249,29 +247,29 @@ describe('Tile', function () { }); }); - it('Valid rotation angle values pass', function () { - [90, 270, -90].forEach(function (angle) { - assert.doesNotThrow(function () { + it('Valid rotation angle values pass', () => { + [90, 270, -90].forEach((angle) => { + assert.doesNotThrow(() => { sharp().tile({ - angle: angle + angle }); }); }); }); - it('Invalid rotation angle values fail', function () { - ['zoinks', 1.1, -1, 27].forEach(function (angle) { - assert.throws(function () { + it('Invalid rotation angle values fail', () => { + ['zoinks', 1.1, -1, 27].forEach((angle) => { + assert.throws(() => { sharp().tile({ - angle: angle + angle }); }); }); }); - it('Valid skipBlanks threshold values pass', function () { - [-1, 0, 255, 65535].forEach(function (skipBlanksThreshold) { - assert.doesNotThrow(function () { + it('Valid skipBlanks threshold values pass', () => { + [-1, 0, 255, 65535].forEach((skipBlanksThreshold) => { + assert.doesNotThrow(() => { sharp().tile({ skipBlanks: skipBlanksThreshold }); @@ -279,9 +277,9 @@ describe('Tile', function () { }); }); - it('InvalidskipBlanks threshold values fail', function () { - ['zoinks', -2, 65536].forEach(function (skipBlanksThreshold) { - assert.throws(function () { + it('InvalidskipBlanks threshold values fail', () => { + ['zoinks', -2, 65536].forEach((skipBlanksThreshold) => { + assert.throws(() => { sharp().tile({ skipBlanks: skipBlanksThreshold }); @@ -289,637 +287,687 @@ describe('Tile', function () { }); }); - it('Invalid center parameter value fail', function () { - assert.throws(function () { - sharp().tile({ - centre: 'true' - }); - }); + it('Valid center parameter value passes', () => { + assert.doesNotThrow( + () => sharp().tile({ center: true }) + ); + }); + + it('Invalid centre parameter value fails', () => { + assert.throws( + () => sharp().tile({ centre: 'true' }), + /Expected boolean for tileCentre but received true of type string/ + ); }); - it('Valid id parameter value passes', function () { - assert.doesNotThrow(function () { + it('Valid id parameter value passes', () => { + assert.doesNotThrow(() => { sharp().tile({ id: 'test' }); }); }); - it('Invalid id parameter value fails', function () { - assert.throws(function () { + it('Invalid id parameter value fails', () => { + assert.throws(() => { sharp().tile({ id: true }); }); }); - it('Deep Zoom layout', function (done) { - const directory = fixtures.path('output.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .toFile(fixtures.path('output.dzi'), 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('undefined', typeof info.size); - assertDeepZoomTiles(directory, 256, 13, done); - }); - }); + it('Valid basename parameter value passes', () => { + assert.doesNotThrow( + () => sharp().tile({ basename: 'pass' }) + ); }); - it('Deep Zoom layout with custom size+overlap', function (done) { - const directory = fixtures.path('output.512.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 512, - overlap: 16 - }) - .toFile(fixtures.path('output.512.dzi'), 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('undefined', typeof info.size); - assertDeepZoomTiles(directory, 512 + (2 * 16), 13, function () { - assertTileOverlap(directory, 512, done); + it('Invalid basename parameter value fails', () => { + assert.throws( + () => sharp().tile({ basename: true }), + /Expected string for basename but received/ + ); + }); + + if (sharp.format.dz.output.file) { + it('Deep Zoom layout', (_t, done) => { + const directory = fixtures.path('output.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .toFile(fixtures.path('output.dzi'), (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('undefined', typeof info.size); + assertDeepZoomTiles(directory, 256, 13, done); }); - }); + }); }); - }); - it('Deep Zoom layout with custom size+angle', function (done) { - const directory = fixtures.path('output.512_90.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 512, - angle: 90 - }) - .toFile(fixtures.path('output.512_90.dzi'), 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('undefined', typeof info.size); - assertDeepZoomTiles(directory, 512, 13, done); - // Verifies tiles in 10th level are rotated - const tile = path.join(directory, '10', '0_1.jpeg'); - // verify that the width and height correspond to the rotated image - // expected are w=512 and h=170 for the 0_1.jpeg. - // if a 0 angle is supplied to the .tile function - // the expected values are w=170 and h=512 for the 1_0.jpeg - sharp(tile).metadata(function (err, metadata) { - if (err) { - throw err; - } else { - assert.strictEqual(true, metadata.width === 512); - assert.strictEqual(true, metadata.height === 170); - } + it('Deep Zoom layout with custom size+overlap', (_t, done) => { + const directory = fixtures.path('output.512.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 512, + overlap: 16 + }) + .toFile(fixtures.path('output.512.dzi'), (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('undefined', typeof info.size); + assertDeepZoomTiles(directory, 512 + (2 * 16), 13, () => { + assertTileOverlap(directory, 512, done); + }); }); - }); + }); }); - }); - it('Deep Zoom layout with depth of one', function (done) { - const directory = fixtures.path('output.512_depth_one.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 512, - depth: 'one' - }) - .toFile(fixtures.path('output.512_depth_one.dzi'), function (err, info) { - if (err) throw err; - // Verify only one depth generated - assertDeepZoomTiles(directory, 512, 1, done); - }); + it('Deep Zoom layout with custom size+angle', (_t, done) => { + const directory = fixtures.path('output.512_90.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 512, + angle: 90 + }) + .toFile(fixtures.path('output.512_90.dzi'), (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('undefined', typeof info.size); + assertDeepZoomTiles(directory, 512, 13, done); + // Verifies tiles in 10th level are rotated + const tile = path.join(directory, '10', '0_1.jpeg'); + // verify that the width and height correspond to the rotated image + // expected are w=512 and h=170 for the 0_1.jpeg. + // if a 0 angle is supplied to the .tile function + // the expected values are w=170 and h=512 for the 1_0.jpeg + sharp(tile).metadata((err, metadata) => { + if (err) { + throw err; + } else { + assert.strictEqual(true, metadata.width === 512); + assert.strictEqual(true, metadata.height === 170); + } + }); + }); + }); }); - }); - it('Deep Zoom layout with depth of onepixel', function (done) { - const directory = fixtures.path('output.512_depth_onepixel.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 512, - depth: 'onepixel' - }) - .toFile(fixtures.path('output.512_depth_onepixel.dzi'), function (err, info) { - if (err) throw err; - // Verify only one depth generated - assertDeepZoomTiles(directory, 512, 13, done); - }); + it('Deep Zoom layout with depth of one', (_t, done) => { + const directory = fixtures.path('output.512_depth_one.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 512, + depth: 'one' + }) + .toFile(fixtures.path('output.512_depth_one.dzi'), (err) => { + if (err) throw err; + // Verify only one depth generated + assertDeepZoomTiles(directory, 512, 1, done); + }); + }); }); - }); - it('Deep Zoom layout with depth of onetile', function (done) { - const directory = fixtures.path('output.256_depth_onetile.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 256, - depth: 'onetile' - }) - .toFile(fixtures.path('output.256_depth_onetile.dzi'), function (err, info) { - if (err) throw err; - // Verify only one depth generated - assertDeepZoomTiles(directory, 256, 5, done); - }); + it('Deep Zoom layout with depth of onepixel', (_t, done) => { + const directory = fixtures.path('output.512_depth_onepixel.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 512, + depth: 'onepixel' + }) + .toFile(fixtures.path('output.512_depth_onepixel.dzi'), (err) => { + if (err) throw err; + // Verify only one depth generated + assertDeepZoomTiles(directory, 512, 13, done); + }); + }); }); - }); - it('Deep Zoom layout with skipBlanks', function (done) { - const directory = fixtures.path('output.256_skip_blanks.dzi_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpgOverlayLayer2) - .tile({ - size: 256, - skipBlanks: 0 - }) - .toFile(fixtures.path('output.256_skip_blanks.dzi'), function (err, info) { - if (err) throw err; - // assert them 0_0.jpeg doesn't exist because it's a white tile - const whiteTilePath = path.join(directory, '11', '0_0.jpeg'); - assert.strictEqual(fs.existsSync(whiteTilePath), false, 'Tile should not exist'); - // Verify only one depth generated - assertDeepZoomTiles(directory, 256, 12, done); - }); + it('Deep Zoom layout with depth of onetile', (_t, done) => { + const directory = fixtures.path('output.256_depth_onetile.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 256, + depth: 'onetile' + }) + .toFile(fixtures.path('output.256_depth_onetile.dzi'), (err) => { + if (err) throw err; + // Verify only one depth generated + assertDeepZoomTiles(directory, 256, 5, done); + }); + }); }); - }); - it('Zoomify layout', function (done) { - const directory = fixtures.path('output.zoomify.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - layout: 'zoomify' - }) - .toFile(fixtures.path('output.zoomify.dzi'), 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); - fs.stat(path.join(directory, 'ImageProperties.xml'), function (err, stat) { + it('Deep Zoom layout with skipBlanks', (_t, done) => { + const directory = fixtures.path('output.256_skip_blanks.dzi_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpgOverlayLayer2) + .tile({ + size: 256, + skipBlanks: 0 + }) + .toFile(fixtures.path('output.256_skip_blanks.dzi'), (err) => { if (err) throw err; - assert.strictEqual(true, stat.isFile()); - assert.strictEqual(true, stat.size > 0); - done(); + // assert them 0_0.jpeg doesn't exist because it's a white tile + const whiteTilePath = path.join(directory, '11', '0_0.jpeg'); + assert.strictEqual(fs.existsSync(whiteTilePath), false, 'Tile should not exist'); + // Verify only one depth generated + assertDeepZoomTiles(directory, 256, 12, done); }); - }); + }); }); - }); - it('Zoomify layout with depth one', function (done) { - const directory = fixtures.path('output.zoomify.depth_one.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 256, - layout: 'zoomify', - depth: 'one' - }) - .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); - assertZoomifyTiles(directory, 256, 1, done); - }); + it('Zoomify layout', (_t, done) => { + const directory = fixtures.path('output.zoomify.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + layout: 'zoomify' + }) + .toFile(fixtures.path('output.zoomify.dzi'), (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(undefined, info.size); + fs.stat(path.join(directory, 'ImageProperties.xml'), (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); + done(); + }); + }); + }); }); - }); - it('Zoomify layout with depth onetile', function (done) { - const directory = fixtures.path('output.zoomify.depth_onetile.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 256, - layout: 'zoomify', - depth: 'onetile' - }) - .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); - assertZoomifyTiles(directory, 256, 5, done); - }); + it('Zoomify layout with depth one', (_t, done) => { + const directory = fixtures.path('output.zoomify.depth_one.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 256, + layout: 'zoomify', + depth: 'one' + }) + .toFile(directory, (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(undefined, info.size); + assertZoomifyTiles(directory, 1, done); + }); + }); }); - }); - it('Zoomify layout with depth onepixel', function (done) { - const directory = fixtures.path('output.zoomify.depth_onepixel.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - size: 256, - layout: 'zoomify', - depth: 'onepixel' - }) - .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); - assertZoomifyTiles(directory, 256, 13, done); - }); + it('Zoomify layout with depth onetile', (_t, done) => { + const directory = fixtures.path('output.zoomify.depth_onetile.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 256, + layout: 'zoomify', + depth: 'onetile' + }) + .toFile(directory, (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(undefined, info.size); + assertZoomifyTiles(directory, 5, done); + }); + }); }); - }); - it('Zoomify layout with skip blanks', function (done) { - const directory = fixtures.path('output.zoomify.skipBlanks.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpgOverlayLayer2) - .tile({ - size: 256, - layout: 'zoomify', - skipBlanks: 0 - }) - .toFile(directory, function (err, info) { - if (err) throw err; - // assert them 0_0.jpeg doesn't exist because it's a white tile - const whiteTilePath = path.join(directory, 'TileGroup0', '2-0-0.jpg'); - assert.strictEqual(fs.existsSync(whiteTilePath), false, 'Tile should not exist'); - assert.strictEqual('dz', info.format); - assert.strictEqual(2048, info.width); - assert.strictEqual(1536, info.height); - assert.strictEqual(3, info.channels); - assert.strictEqual('number', typeof info.size); - assertZoomifyTiles(directory, 256, 4, done); - }); + it('Zoomify layout with depth onepixel', (_t, done) => { + const directory = fixtures.path('output.zoomify.depth_onepixel.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + size: 256, + layout: 'zoomify', + depth: 'onepixel' + }) + .toFile(directory, (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(undefined, info.size); + assertZoomifyTiles(directory, 13, done); + }); + }); }); - }); - it('Google layout', function (done) { - const directory = fixtures.path('output.google.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - 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); - fs.stat(path.join(directory, '0', '0', '0.jpg'), function (err, stat) { + it('Zoomify layout with skip blanks', (_t, done) => { + const directory = fixtures.path('output.zoomify.skipBlanks.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpgOverlayLayer2) + .tile({ + size: 256, + layout: 'zoomify', + skipBlanks: 0 + }) + .toFile(directory, (err, info) => { if (err) throw err; - assert.strictEqual(true, stat.isFile()); - assert.strictEqual(true, stat.size > 0); - done(); + // assert them 0_0.jpeg doesn't exist because it's a white tile + const whiteTilePath = path.join(directory, 'TileGroup0', '2-0-0.jpg'); + assert.strictEqual(fs.existsSync(whiteTilePath), false, 'Tile should not exist'); + assert.strictEqual('dz', info.format); + assert.strictEqual(2048, info.width); + assert.strictEqual(1536, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + assertZoomifyTiles(directory, 4, done); }); - }); + }); }); - }); - it('Google layout with jpeg format', function (done) { - const directory = fixtures.path('output.jpg.google.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .jpeg({ - quality: 1 - }) - .tile({ - 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); - const sample = path.join(directory, '0', '0', '0.jpg'); - sharp(sample).metadata(function (err, metadata) { + it('Google layout', (_t, done) => { + const directory = fixtures.path('output.google.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + layout: 'google' + }) + .toFile(directory, (err, info) => { if (err) throw err; - assert.strictEqual('jpeg', metadata.format); - assert.strictEqual('srgb', metadata.space); - assert.strictEqual(3, metadata.channels); - assert.strictEqual(false, metadata.hasProfile); - assert.strictEqual(false, metadata.hasAlpha); - assert.strictEqual(256, metadata.width); - assert.strictEqual(256, metadata.height); - fs.stat(sample, function (err, stat) { + assert.strictEqual('dz', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + fs.stat(path.join(directory, '0', '0', '0.jpg'), (err, stat) => { if (err) throw err; - assert.strictEqual(true, stat.size < 2000); + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); done(); }); }); - }); + }); }); - }); - it('Google layout with png format', function (done) { - const directory = fixtures.path('output.png.google.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .png({ - compressionLevel: 0 - }) - .tile({ - 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); - const sample = path.join(directory, '0', '0', '0.png'); - sharp(sample).metadata(function (err, metadata) { + it('Google layout with jpeg format', (_t, done) => { + const directory = fixtures.path('output.jpg.google.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .jpeg({ + quality: 1 + }) + .tile({ + layout: 'google' + }) + .toFile(directory, (err, info) => { if (err) throw err; - assert.strictEqual('png', metadata.format); - assert.strictEqual('srgb', metadata.space); - assert.strictEqual(3, metadata.channels); - assert.strictEqual(false, metadata.hasProfile); - assert.strictEqual(false, metadata.hasAlpha); - assert.strictEqual(256, metadata.width); - assert.strictEqual(256, metadata.height); - fs.stat(sample, function (err, stat) { + assert.strictEqual('dz', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + const sample = path.join(directory, '0', '0', '0.jpg'); + sharp(sample).metadata((err, metadata) => { if (err) throw err; - assert.strictEqual(true, stat.size > 44000); - done(); + assert.strictEqual('jpeg', metadata.format); + assert.strictEqual('srgb', metadata.space); + assert.strictEqual(3, metadata.channels); + assert.strictEqual(false, metadata.hasProfile); + assert.strictEqual(false, metadata.hasAlpha); + assert.strictEqual(256, metadata.width); + assert.strictEqual(256, metadata.height); + fs.stat(sample, (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.size < 2000); + done(); + }); }); }); - }); + }); }); - }); - it('Google layout with webp format', function (done) { - const directory = fixtures.path('output.webp.google.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .webp({ - quality: 1, - reductionEffort: 0 - }) - .tile({ - 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); - const sample = path.join(directory, '0', '0', '0.webp'); - sharp(sample).metadata(function (err, metadata) { + it('Google layout with png format', (_t, done) => { + const directory = fixtures.path('output.png.google.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .png({ + compressionLevel: 0 + }) + .tile({ + layout: 'google' + }) + .toFile(directory, (err, info) => { if (err) throw err; - assert.strictEqual('webp', metadata.format); - assert.strictEqual('srgb', metadata.space); - assert.strictEqual(3, metadata.channels); - assert.strictEqual(false, metadata.hasProfile); - assert.strictEqual(false, metadata.hasAlpha); - assert.strictEqual(256, metadata.width); - assert.strictEqual(256, metadata.height); - fs.stat(sample, function (err, stat) { + assert.strictEqual('dz', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + const sample = path.join(directory, '0', '0', '0.png'); + sharp(sample).metadata((err, metadata) => { if (err) throw err; - assert.strictEqual(true, stat.size < 2000); - done(); + assert.strictEqual('png', metadata.format); + assert.strictEqual('srgb', metadata.space); + assert.strictEqual(3, metadata.channels); + assert.strictEqual(false, metadata.hasProfile); + assert.strictEqual(false, metadata.hasAlpha); + assert.strictEqual(256, metadata.width); + assert.strictEqual(256, metadata.height); + fs.stat(sample, (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.size > 44000); + done(); + }); }); }); - }); + }); }); - }); - it('Google layout with depth one', function (done) { - const directory = fixtures.path('output.google_depth_one.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - layout: 'google', - depth: 'one', - size: 256 - }) - .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); - - assertGoogleTiles(directory, 256, 1, done); - }); + it('Google layout with webp format', (_t, done) => { + const directory = fixtures.path('output.webp.google.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .webp({ + quality: 1, + effort: 0 + }) + .tile({ + layout: 'google' + }) + .toFile(directory, (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(undefined, info.size); + const sample = path.join(directory, '0', '0', '0.webp'); + sharp(sample).metadata((err, metadata) => { + if (err) throw err; + assert.strictEqual('webp', metadata.format); + assert.strictEqual('srgb', metadata.space); + assert.strictEqual(3, metadata.channels); + assert.strictEqual(false, metadata.hasProfile); + assert.strictEqual(false, metadata.hasAlpha); + assert.strictEqual(256, metadata.width); + assert.strictEqual(256, metadata.height); + fs.stat(sample, (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.size < 2000); + done(); + }); + }); + }); + }); }); - }); - it('Google layout with depth onepixel', function (done) { - const directory = fixtures.path('output.google_depth_onepixel.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - layout: 'google', - depth: 'onepixel', - size: 256 - }) - .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); - - assertGoogleTiles(directory, 256, 13, done); - }); + it('Google layout with depth one', (_t, done) => { + const directory = fixtures.path('output.google_depth_one.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + layout: 'google', + depth: 'one', + size: 256 + }) + .toFile(directory, (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(undefined, info.size); + assertGoogleTiles(directory, 1, done); + }); + }); }); - }); - it('Google layout with depth onetile', function (done) { - const directory = fixtures.path('output.google_depth_onetile.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - layout: 'google', - depth: 'onetile', - size: 256 - }) - .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); - - assertGoogleTiles(directory, 256, 5, done); - }); + it('Google layout with depth onetile', (_t, done) => { + const directory = fixtures.path('output.google_depth_onetile.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + layout: 'google', + depth: 'onetile', + size: 256 + }) + .toFile(directory, (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(undefined, info.size); + assertGoogleTiles(directory, 5, done); + }); + }); }); - }); - it('Google layout with default skip Blanks', function (done) { - const directory = fixtures.path('output.google_depth_skipBlanks.dzi'); - rimraf(directory, function () { - sharp(fixtures.inputPng) - .tile({ - layout: 'google', - size: 256 - }) - .toFile(directory, function (err, info) { - if (err) throw err; - - const whiteTilePath = path.join(directory, '4', '8', '0.jpg'); - assert.strictEqual(fs.existsSync(whiteTilePath), false, 'Tile should not exist'); - - assert.strictEqual('dz', info.format); - assert.strictEqual(2809, info.width); - assert.strictEqual(2074, info.height); - assert.strictEqual(3, info.channels); - assert.strictEqual('number', typeof info.size); - - assertGoogleTiles(directory, 256, 5, done); - }); + it('Google layout with default skip Blanks', (_t, done) => { + const directory = fixtures.path('output.google_depth_skipBlanks.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputPng) + .tile({ + layout: 'google', + size: 256 + }) + .toFile(directory, (err, info) => { + if (err) throw err; + + const whiteTilePath = path.join(directory, '4', '8', '0.jpg'); + assert.strictEqual(fs.existsSync(whiteTilePath), false, 'Tile should not exist'); + + assert.strictEqual('dz', info.format); + assert.strictEqual(2809, info.width); + assert.strictEqual(2074, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + assertGoogleTiles(directory, 5, done); + }); + }); }); - }); - 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', (_t, done) => { + const directory = fixtures.path('output.google_center.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + center: true, + layout: 'google' + }) + .toFile(directory, (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(undefined, 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('Google layout with center image in tile centre', (_t, done) => { + const directory = fixtures.path('output.google_center.dzi'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + centre: true, + layout: 'google' + }) + .toFile(directory, (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(undefined, 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 name = 'output.iiif.info'; - const directory = fixtures.path(name); - rimraf(directory, function () { - const id = 'https://sharp.test.com/iiif'; - sharp(fixtures.inputJpg) - .tile({ - layout: 'iiif', - id - }) - .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); - const infoJson = require(path.join(directory, 'info.json')); - assert.strictEqual(`${id}/${name}`, infoJson['@id']); - fs.stat(path.join(directory, '0,0,256,256', '256,', '0', 'default.jpg'), function (err, stat) { + it('IIIFv2 layout', (_t, done) => { + const name = 'output.iiif.info'; + const directory = fixtures.path(name); + fs.rm(directory, { recursive: true }, () => { + const id = 'https://sharp.test.com/iiif'; + sharp(fixtures.inputJpg) + .tile({ + layout: 'iiif', + id + }) + .toFile(directory, (err, info) => { if (err) throw err; - assert.strictEqual(true, stat.isFile()); - assert.strictEqual(true, stat.size > 0); - done(); + assert.strictEqual('dz', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + const infoJson = require(path.join(directory, 'info.json')); + assert.strictEqual('http://iiif.io/api/image/2/context.json', infoJson['@context']); + assert.strictEqual(`${id}/${name}`, infoJson['@id']); + fs.stat(path.join(directory, '0,0,256,256', '256,', '0', 'default.jpg'), (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); + done(); + }); }); - }); + }); }); - }); - it('Write to ZIP container using file extension', function (done) { - const container = fixtures.path('output.dz.container.zip'); - const extractTo = fixtures.path('output.dz.container'); - const directory = path.join(extractTo, 'output.dz.container_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .toFile(container, 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); - fs.stat(container, function (err, stat) { + it('IIIFv3 layout', (_t, done) => { + const name = 'output.iiif3.info'; + const directory = fixtures.path(name); + fs.rm(directory, { recursive: true }, () => { + const id = 'https://sharp.test.com/iiif3'; + sharp(fixtures.inputJpg) + .tile({ + layout: 'iiif3', + id + }) + .toFile(directory, (err, info) => { if (err) throw err; - assert.strictEqual(true, stat.isFile()); - assert.strictEqual(true, stat.size > 0); - new DecompressZip(container) - .on('extract', function () { - assertDeepZoomTiles(directory, 256, 13, done); - }) - .on('error', function (err) { - throw err; - }) - .extract({ path: path.dirname(extractTo) }); + assert.strictEqual('dz', info.format); + assert.strictEqual(2725, info.width); + assert.strictEqual(2225, info.height); + assert.strictEqual(3, info.channels); + assert.strictEqual(undefined, info.size); + const infoJson = require(path.join(directory, 'info.json')); + assert.strictEqual('http://iiif.io/api/image/3/context.json', infoJson['@context']); + assert.strictEqual('ImageService3', infoJson.type); + assert.strictEqual(`${id}/${name}`, infoJson.id); + fs.stat(path.join(directory, '0,0,256,256', '256,256', '0', 'default.jpg'), (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); + done(); + }); }); - }); + }); }); - }); - it('Write to ZIP container using container tile option', function (done) { - const container = fixtures.path('output.dz.containeropt.zip'); - const extractTo = fixtures.path('output.dz.containeropt'); - const directory = path.join(extractTo, 'output.dz.containeropt_files'); - rimraf(directory, function () { - sharp(fixtures.inputJpg) - .tile({ - container: 'zip' - }) - .toFile(container, function (err, info) { - // Vips overrides .dzi extension to .zip used by container var below - 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); - fs.stat(container, function (err, stat) { + it('Write to ZIP container using file extension', (_t, done) => { + const container = fixtures.path('output.dz.container.zip'); + const extractTo = fixtures.path('output.dz.container'); + const directory = path.join(extractTo, 'output.dz.container_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .toFile(container, (err, info) => { if (err) throw err; - assert.strictEqual(true, stat.isFile()); - assert.strictEqual(true, stat.size > 0); - new DecompressZip(container) - .on('extract', function () { - assertDeepZoomTiles(directory, 256, 13, done); - }) - .on('error', function (err) { - throw err; - }) - .extract({ path: path.dirname(extractTo) }); + 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); + fs.stat(container, (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); + extractZip(container, { dir: extractTo }) + .then(() => { + assertDeepZoomTiles(directory, 256, 13, done); + }) + .catch(_t, done); + }); }); - }); + }); }); - }); + + it('Write to ZIP container using container tile option', (_t, done) => { + const container = fixtures.path('output.dz.containeropt.zip'); + const extractTo = fixtures.path('output.dz.containeropt'); + const directory = path.join(extractTo, 'output.dz.containeropt_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ + container: 'zip' + }) + .toFile(container, (err, info) => { + // Vips overrides .dzi extension to .zip used by container var below + 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); + fs.stat(container, (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); + extractZip(container, { dir: extractTo }) + .then(() => { + assertDeepZoomTiles(directory, 256, 13, done); + }) + .catch(_t, done); + }); + }); + }); + }); + + it('Write ZIP container to Buffer', (_t, done) => { + const container = fixtures.path('output.dz.tiles.zip'); + const extractTo = fixtures.path('output.dz.tiles'); + const directory = path.join(extractTo, 'output.dz.tiles_files'); + fs.rm(directory, { recursive: true }, () => { + sharp(fixtures.inputJpg) + .tile({ basename: 'output.dz.tiles' }) + .toBuffer((err, data, 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); + fs.writeFileSync(container, data); + fs.stat(container, (err, stat) => { + if (err) throw err; + assert.strictEqual(true, stat.isFile()); + assert.strictEqual(true, stat.size > 0); + extractZip(container, { dir: extractTo }) + .then(() => { + assertDeepZoomTiles(directory, 256, 13, done); + }) + .catch(_t, done); + }); + }); + }); + }); + } }); diff --git a/test/unit/timeout.js b/test/unit/timeout.js new file mode 100644 index 000000000..77aad8fa6 --- /dev/null +++ b/test/unit/timeout.js @@ -0,0 +1,30 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('Timeout', () => { + it('Will timeout after 1s when performing slow blur operation', () => assert.rejects( + () => sharp(fixtures.inputJpg) + .blur(200) + .timeout({ seconds: 1 }) + .toBuffer(), + /timeout: [0-9]+% complete/ + )); + + it('invalid object', () => assert.throws( + () => sharp().timeout('fail'), + /Expected object for options but received fail of type string/ + )); + + it('invalid seconds', () => assert.throws( + () => sharp().timeout({ seconds: 'fail' }), + /Expected integer between 0 and 3600 for seconds but received fail of type string/ + )); +}); diff --git a/test/unit/tint.js b/test/unit/tint.js index fcadb789f..db3f4984f 100644 --- a/test/unit/tint.js +++ b/test/unit/tint.js @@ -1,101 +1,108 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Tint', function () { - it('tints rgb image red', function (done) { +// Allow for small rounding differences between platforms +const maxDistance = 6; + +describe('Tint', () => { + it('tints rgb image red', (_t, done) => { const output = fixtures.path('output.tint-red.jpg'); sharp(fixtures.inputJpg) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint('#FF0000') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(true, info.size > 0); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-red.jpg'), 18); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-red.jpg'), maxDistance); done(); }); }); - it('tints rgb image green', function (done) { + it('tints rgb image green', (_t, done) => { const output = fixtures.path('output.tint-green.jpg'); sharp(fixtures.inputJpg) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint('#00FF00') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(true, info.size > 0); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-green.jpg'), 27); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-green.jpg'), maxDistance); done(); }); }); - it('tints rgb image blue', function (done) { + it('tints rgb image blue', (_t, done) => { const output = fixtures.path('output.tint-blue.jpg'); sharp(fixtures.inputJpg) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint('#0000FF') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(true, info.size > 0); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-blue.jpg'), 14); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-blue.jpg'), maxDistance); done(); }); }); - it('tints rgb image with sepia tone', function (done) { - const output = fixtures.path('output.tint-sepia.jpg'); + it('tints rgb image with sepia tone', (_t, done) => { + const output = fixtures.path('output.tint-sepia-hex.jpg'); sharp(fixtures.inputJpg) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint('#704214') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-sepia.jpg'), 10); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-sepia.jpg'), maxDistance); done(); }); }); - it('tints rgb image with sepia tone with rgb colour', function (done) { - const output = fixtures.path('output.tint-sepia.jpg'); + it('tints rgb image with sepia tone with rgb colour', (_t, done) => { + const output = fixtures.path('output.tint-sepia-rgb.jpg'); sharp(fixtures.inputJpg) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint([112, 66, 20]) - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-sepia.jpg'), 10); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-sepia.jpg'), maxDistance); done(); }); }); - it('tints rgb image with alpha channel', function (done) { + it('tints rgb image with alpha channel', (_t, done) => { const output = fixtures.path('output.tint-alpha.png'); sharp(fixtures.inputPngRGBWithAlpha) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint('#704214') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-alpha.png'), 10); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-alpha.png'), maxDistance); done(); }); }); - it('tints cmyk image red', function (done) { + it('tints cmyk image red', (_t, done) => { const output = fixtures.path('output.tint-cmyk.jpg'); sharp(fixtures.inputJpgWithCmykProfile) - .resize(320, 240, { fastShrinkOnLoad: false }) + .resize(320, 240) .tint('#FF0000') - .toFile(output, function (err, info) { + .toFile(output, (err, info) => { if (err) throw err; assert.strictEqual(true, info.size > 0); - fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), 15); + fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), maxDistance); done(); }); }); diff --git a/test/unit/toBuffer.js b/test/unit/toBuffer.js index 8c9da6efd..7e4b1d452 100644 --- a/test/unit/toBuffer.js +++ b/test/unit/toBuffer.js @@ -1,6 +1,10 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); diff --git a/test/unit/toFormat.js b/test/unit/toFormat.js index acc680d54..0974e4604 100644 --- a/test/unit/toFormat.js +++ b/test/unit/toFormat.js @@ -1,6 +1,10 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); diff --git a/test/unit/trim.js b/test/unit/trim.js index 2591c0137..99b14a8d9 100644 --- a/test/unit/trim.js +++ b/test/unit/trim.js @@ -1,29 +1,17 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const inRange = require('../../lib/is').inRange; const fixtures = require('../fixtures'); -describe('Trim borders', function () { - it('Threshold default', function (done) { - const expected = fixtures.expected('alpha-layer-1-fill-trim-resize.png'); - sharp(fixtures.inputPngOverlayLayer1) - .resize(450, 322) - .trim() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual('png', info.format); - assert.strictEqual(450, info.width); - assert.strictEqual(322, info.height); - assert.strictEqual(-204, info.trimOffsetLeft); - assert.strictEqual(0, info.trimOffsetTop); - fixtures.assertSimilar(expected, data, done); - }); - }); - - it('Skip shrink-on-load', function (done) { +describe('Trim borders', () => { + it('Skip shrink-on-load', (_t, done) => { const expected = fixtures.expected('alpha-layer-2-trim-resize.jpg'); sharp(fixtures.inputJpgOverlayLayer2) .trim() @@ -31,7 +19,7 @@ describe('Trim borders', function () { width: 300, fastShrinkOnLoad: false }) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(300, info.width); @@ -41,7 +29,7 @@ describe('Trim borders', function () { }); }); - it('single colour PNG where alpha channel provides the image', () => + it('Single colour PNG where alpha channel provides the image', () => sharp(fixtures.inputPngImageInAlpha) .trim() .toBuffer({ resolveWithObject: true }) @@ -56,11 +44,13 @@ describe('Trim borders', function () { }) ); - it('16-bit PNG with alpha channel', function (done) { + it('16-bit PNG with alpha channel', (_t, done) => { sharp(fixtures.inputPngWithTransparency16bit) .resize(32, 32) - .trim(20) - .toBuffer(function (err, data, info) { + .trim({ + threshold: 20 + }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('png', info.format); @@ -73,7 +63,7 @@ describe('Trim borders', function () { }); }); - it('Attempt to trim 2x2 pixel image fails', function (done) { + it('Attempt to trim 2x2 pixel image fails', (_t, done) => { sharp({ create: { width: 2, @@ -91,10 +81,10 @@ describe('Trim borders', function () { assert.strictEqual('Image to trim must be at least 3x3 pixels', err.message); done(); }) - .catch(done); + .catch(_t, done); }); - it('should rotate before trim', () => + it('Should rotate before trim', () => sharp({ create: { width: 20, @@ -109,7 +99,9 @@ describe('Trim borders', function () { .then(rotated30 => sharp(rotated30) .rotate(-30) - .trim(128) + .trim({ + threshold: 128 + }) .toBuffer({ resolveWithObject: true }) .then(({ info }) => { assert.strictEqual(20, info.width); @@ -120,13 +112,181 @@ describe('Trim borders', function () { ) ); - describe('Invalid thresholds', function () { - [-1, 'fail', {}].forEach(function (threshold) { - it(JSON.stringify(threshold), function () { - assert.throws(function () { - sharp().trim(threshold); + it('Animated image rejects', () => + assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true }) + .trim() + .toBuffer(), + /Trim is not supported for multi-page images/ + ) + ); + + it('Ensure trim uses bounding box of alpha and non-alpha channels', async () => { + const { info } = await sharp(fixtures.inputPngTrimIncludeAlpha) + .trim() + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 179); + assert.strictEqual(height, 123); + assert.strictEqual(trimOffsetTop, -44); + assert.strictEqual(trimOffsetLeft, -13); + }); + + it('Ensure greyscale image can be trimmed', async () => { + const greyscale = await sharp({ + create: { + width: 16, + height: 8, + channels: 3, + background: 'silver' + } + }) + .extend({ left: 12, right: 24, background: 'gray' }) + .toColourspace('b-w') + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { info } = await sharp(greyscale) + .trim() + .raw() + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 16); + assert.strictEqual(height, 8); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, -12); + }); + + it('Ensure CMYK image can be trimmed', async () => { + const cmyk = await sharp({ + create: { + width: 16, + height: 8, + channels: 3, + background: 'red' + } + }) + .extend({ left: 12, right: 24, background: 'blue' }) + .toColourspace('cmyk') + .jpeg() + .toBuffer(); + + const { info } = await sharp(cmyk) + .trim() + .raw() + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 16); + assert.strictEqual(height, 8); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, -12); + }); + + it('Ensure trim of image with all pixels same is no-op', async () => { + const { info } = await sharp({ + create: { + width: 5, + height: 5, + channels: 3, + background: 'red' + } + }) + .trim() + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 5); + assert.strictEqual(height, 5); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, 0); + }); + + it('Works with line-art', async () => { + const { info } = await sharp(fixtures.inputJpgOverlayLayer2) + .trim({ lineArt: true }) + .toBuffer({ resolveWithObject: true }); + + assert.strictEqual(info.trimOffsetTop, -552); + }); + + describe('Invalid parameters', () => { + Object.entries({ + 'Invalid string': 'fail', + 'Invalid background option': { + background: 'fail' + }, + 'Negative threshold option': { + threshold: -1 + }, + 'Invalid lineArt': { + lineArt: 'fail' + } + }).forEach(([description, parameter]) => { + it(description, () => { + assert.throws(() => { + sharp().trim(parameter); }); }); }); }); + + describe('Specific background colour', () => { + it('Doesn\'t trim at all', async () => { + const { info } = await sharp(fixtures.inputPngTrimSpecificColour) + .trim({ + background: 'yellow' + }) + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 900); + assert.strictEqual(height, 600); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, 0); + }); + + it('Only trims the bottom', async () => { + const { info } = await sharp(fixtures.inputPngTrimSpecificColour) + .trim({ + background: '#21468B' + }) + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 900); + assert.strictEqual(height, 401); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, 0); + }); + + it('Only trims the bottom, in 16-bit', async () => { + const { info } = await sharp(fixtures.inputPngTrimSpecificColour16bit) + .trim({ + background: '#21468B' + }) + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 900); + assert.strictEqual(height, 401); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, 0); + }); + + it('Only trims the bottom, including alpha', async () => { + const { info } = await sharp(fixtures.inputPngTrimSpecificColourIncludeAlpha) + .trim({ + background: '#21468B80' + }) + .toBuffer({ resolveWithObject: true }); + + const { width, height, trimOffsetTop, trimOffsetLeft } = info; + assert.strictEqual(width, 900); + assert.strictEqual(height, 401); + assert.strictEqual(trimOffsetTop, 0); + assert.strictEqual(trimOffsetLeft, 0); + }); + }); }); diff --git a/test/unit/unflatten.js b/test/unit/unflatten.js new file mode 100644 index 000000000..1db60af33 --- /dev/null +++ b/test/unit/unflatten.js @@ -0,0 +1,32 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +const { describe, it } = require('node:test'); +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('Unflatten', () => { + it('unflatten white background', (_t, done) => { + sharp(fixtures.inputPng).unflatten() + .toBuffer((err, data) => { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('unflatten-white-transparent.png'), data, { threshold: 0 }, done); + }); + }); + it('unflatten transparent image', (_t, done) => { + sharp(fixtures.inputPngTrimSpecificColourIncludeAlpha).unflatten() + .toBuffer((err, data) => { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('unflatten-flag-white-transparent.png'), data, { threshold: 0 }, done); + }); + }); + it('unflatten using threshold', (_t, done) => { + sharp(fixtures.inputPngPalette).unflatten().threshold(128, { grayscale: false }) + .toBuffer((err, data) => { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('unflatten-swiss.png'), data, { threshold: 1 }, done); + }); + }); +}); diff --git a/test/unit/util.js b/test/unit/util.js index 1c3cfc44f..e3676db6d 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -1,28 +1,38 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const assert = require('assert'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); +const semver = require('semver'); const sharp = require('../../'); -describe('Utilities', function () { - describe('Cache', function () { - it('Can be disabled', function () { - sharp.cache(false); - const cache = sharp.cache(false); - assert.strictEqual(cache.memory.current, 0); - assert.strictEqual(cache.memory.max, 0); - assert.strictEqual(typeof cache.memory.high, 'number'); - assert.strictEqual(cache.files.current, 0); - assert.strictEqual(cache.files.max, 0); - assert.strictEqual(cache.items.current, 0); - assert.strictEqual(cache.items.max, 0); - }); - it('Can be enabled with defaults', function () { +describe('Utilities', () => { + describe('Cache', () => { + it('Can be disabled', (_t, done) => { + const check = setInterval(() => { + const cache = sharp.cache(false); + const empty = + cache.memory.current + + cache.memory.max + + cache.files.current + + cache.files.max + + cache.items.current + + cache.items.max === 0; + if (empty) { + clearInterval(check); + done(); + } + }, 2000); + }); + it('Can be enabled with defaults', () => { const cache = sharp.cache(true); assert.strictEqual(cache.memory.max, 50); assert.strictEqual(cache.files.max, 20); assert.strictEqual(cache.items.max, 100); }); - it('Can be set to zero', function () { + it('Can be set to zero', () => { const cache = sharp.cache({ memory: 0, files: 0, @@ -32,7 +42,7 @@ describe('Utilities', function () { assert.strictEqual(cache.files.max, 0); assert.strictEqual(cache.items.max, 0); }); - it('Can be set to a maximum of 10MB, 100 files and 1000 items', function () { + it('Can be set to a maximum of 10MB, 100 files and 1000 items', () => { const cache = sharp.cache({ memory: 10, files: 100, @@ -42,7 +52,7 @@ describe('Utilities', function () { assert.strictEqual(cache.files.max, 100); assert.strictEqual(cache.items.max, 1000); }); - it('Ignores invalid values', function () { + it('Ignores invalid values', () => { sharp.cache(true); const cache = sharp.cache('spoons'); assert.strictEqual(cache.memory.max, 50); @@ -51,55 +61,58 @@ describe('Utilities', function () { }); }); - describe('Concurrency', function () { - it('Can be set to use 16 threads', function () { + describe('Concurrency', () => { + it('Can be set to use 16 threads', () => { sharp.concurrency(16); assert.strictEqual(16, sharp.concurrency()); }); - it('Can be reset to default', function () { + it('Can be reset to default', () => { sharp.concurrency(0); assert.strictEqual(true, sharp.concurrency() > 0); }); - it('Ignores invalid values', function () { + it('Ignores invalid values', () => { const defaultConcurrency = sharp.concurrency(); sharp.concurrency('spoons'); assert.strictEqual(defaultConcurrency, sharp.concurrency()); }); }); - describe('Counters', function () { - it('Have zero value at rest', function () { - const counters = sharp.counters(); - assert.strictEqual(0, counters.queue); - assert.strictEqual(0, counters.process); + describe('Counters', () => { + it('Have zero value at rest', (_t, done) => { + queueMicrotask(() => { + const counters = sharp.counters(); + assert.strictEqual(0, counters.queue); + assert.strictEqual(0, counters.process); + done(); + }); }); }); - describe('SIMD', function () { - it('Can get current state', function () { + describe('SIMD', () => { + it('Can get current state', () => { const simd = sharp.simd(); assert.strictEqual(typeof simd, 'boolean'); }); - it('Can disable', function () { + it('Can disable', () => { const simd = sharp.simd(false); assert.strictEqual(simd, false); }); - it('Can attempt to enable', function () { + it('Can attempt to enable', () => { const simd = sharp.simd(true); assert.strictEqual(typeof simd, 'boolean'); }); }); - describe('Format', function () { - it('Contains expected attributes', function () { + describe('Format', () => { + it('Contains expected attributes', () => { assert.strictEqual('object', typeof sharp.format); - Object.keys(sharp.format).forEach(function (format) { + Object.keys(sharp.format).forEach((format) => { assert.strictEqual(true, 'id' in sharp.format[format]); assert.strictEqual(format, sharp.format[format].id); - ['input', 'output'].forEach(function (direction) { + ['input', 'output'].forEach((direction) => { assert.strictEqual(true, direction in sharp.format[format]); assert.strictEqual('object', typeof sharp.format[format][direction]); - assert.strictEqual(3, Object.keys(sharp.format[format][direction]).length); + assert.strictEqual(true, [3, 4].includes(Object.keys(sharp.format[format][direction]).length)); assert.strictEqual(true, 'file' in sharp.format[format][direction]); assert.strictEqual(true, 'buffer' in sharp.format[format][direction]); assert.strictEqual(true, 'stream' in sharp.format[format][direction]); @@ -109,26 +122,70 @@ describe('Utilities', function () { }); }); }); - it('Raw file=false, buffer=true, stream=true', function () { - ['input', 'output'].forEach(function (direction) { + it('Raw file=false, buffer=true, stream=true', () => { + ['input', 'output'].forEach((direction) => { assert.strictEqual(false, sharp.format.raw[direction].file); assert.strictEqual(true, sharp.format.raw[direction].buffer); assert.strictEqual(true, sharp.format.raw[direction].stream); }); }); - it('vips format supports filesystem only', function () { - ['input', 'output'].forEach(function (direction) { + it('vips format supports filesystem only', () => { + ['input', 'output'].forEach((direction) => { assert.strictEqual(true, sharp.format.vips[direction].file); assert.strictEqual(false, sharp.format.vips[direction].buffer); assert.strictEqual(false, sharp.format.vips[direction].stream); }); }); + it('input fileSuffix', () => { + assert.deepStrictEqual(['.jpg', '.jpeg', '.jpe', '.jfif'], sharp.format.jpeg.input.fileSuffix); + }); + it('output alias', () => { + assert.deepStrictEqual(['jpe', 'jpg'], sharp.format.jpeg.output.alias); + }); }); - describe('Versions', function () { - it('Contains expected attributes', function () { + describe('Versions', () => { + it('Contains expected attributes', () => { assert.strictEqual('object', typeof sharp.versions); - assert.strictEqual('string', typeof sharp.versions.vips); + assert(semver.valid(sharp.versions.vips)); + assert(semver.valid(sharp.versions.sharp)); + }); + }); + + describe('Block', () => { + it('Can block a named operation', () => { + sharp.block({ operation: ['test'] }); + }); + it('Can unblock a named operation', () => { + sharp.unblock({ operation: ['test'] }); + }); + it('Invalid block operation throws', () => { + assert.throws(() => sharp.block(1), + /Expected object for options but received 1 of type number/ + ); + assert.throws(() => sharp.block({}), + /Expected Array<string> for operation but received undefined of type undefined/ + ); + assert.throws(() => sharp.block({ operation: 'fail' }), + /Expected Array<string> for operation but received fail of type string/ + ); + assert.throws(() => sharp.block({ operation: ['maybe', false] }), + /Expected Array<string> for operation but received maybe,false of type object/ + ); + }); + it('Invalid unblock operation throws', () => { + assert.throws(() => sharp.unblock(1), + /Expected object for options but received 1 of type number/ + ); + assert.throws(() => sharp.unblock({}), + /Expected Array<string> for operation but received undefined of type undefined/ + ); + assert.throws(() => sharp.unblock({ operation: 'fail' }), + /Expected Array<string> for operation but received fail of type string/ + ); + assert.throws(() => sharp.unblock({ operation: ['maybe', false] }), + /Expected Array<string> for operation but received maybe,false of type object/ + ); }); }); }); diff --git a/test/unit/webp.js b/test/unit/webp.js index c66eb435d..22d37a522 100644 --- a/test/unit/webp.js +++ b/test/unit/webp.js @@ -1,17 +1,21 @@ -'use strict'; +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ -const fs = require('fs'); -const assert = require('assert'); +const fs = require('node:fs'); +const { describe, it } = require('node:test'); +const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('WebP', function () { - it('WebP output', function (done) { +describe('WebP', () => { + it('WebP output', (_t, done) => { sharp(fixtures.inputJpg) .resize(320, 240) .toFormat(sharp.format.webp) - .toBuffer(function (err, data, info) { + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('webp', info.format); @@ -21,22 +25,22 @@ describe('WebP', function () { }); }); - it('Invalid WebP quality throws error', function () { - assert.throws(function () { + it('Invalid WebP quality throws error', () => { + assert.throws(() => { sharp().webp({ quality: 101 }); }); }); - it('Invalid WebP alpha quality throws error', function () { - assert.throws(function () { + it('Invalid WebP alpha quality throws error', () => { + assert.throws(() => { sharp().webp({ alphaQuality: 101 }); }); }); - it('should work for webp alpha quality', function (done) { + it('should work for webp alpha quality', (_t, done) => { sharp(fixtures.inputPngAlphaPremultiplicationSmall) - .webp({ alphaQuality: 80 }) - .toBuffer(function (err, data, info) { + .webp({ alphaQuality: 80, effort: 0 }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('webp', info.format); @@ -44,10 +48,10 @@ describe('WebP', function () { }); }); - it('should work for webp lossless', function (done) { + it('should work for webp lossless', (_t, done) => { sharp(fixtures.inputPngAlphaPremultiplicationSmall) - .webp({ lossless: true }) - .toBuffer(function (err, data, info) { + .webp({ lossless: true, effort: 0 }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('webp', info.format); @@ -55,10 +59,10 @@ describe('WebP', function () { }); }); - it('should work for webp near-lossless', function (done) { + it('should work for webp near-lossless', (_t, done) => { sharp(fixtures.inputPngAlphaPremultiplicationSmall) - .webp({ nearLossless: true, quality: 50 }) - .toBuffer(function (err50, data50, info50) { + .webp({ nearLossless: true, quality: 50, effort: 0 }) + .toBuffer((err50, data50, info50) => { if (err50) throw err50; assert.strictEqual(true, data50.length > 0); assert.strictEqual('webp', info50.format); @@ -66,10 +70,10 @@ describe('WebP', function () { }); }); - it('should use near-lossless when both lossless and nearLossless are specified', function (done) { + it('should use near-lossless when both lossless and nearLossless are specified', (_t, done) => { sharp(fixtures.inputPngAlphaPremultiplicationSmall) - .webp({ nearLossless: true, quality: 50, lossless: true }) - .toBuffer(function (err50, data50, info50) { + .webp({ nearLossless: true, quality: 50, lossless: true, effort: 0 }) + .toBuffer((err50, data50, info50) => { if (err50) throw err50; assert.strictEqual(true, data50.length > 0); assert.strictEqual('webp', info50.format); @@ -99,40 +103,116 @@ describe('WebP', function () { }); }); - it('should produce a smaller file size with increased reductionEffort', () => + it('can produce a different file size using smartDeblock', () => + sharp(fixtures.inputPngOverlayLayer0) + .resize(320, 240) + .webp({ quality: 30, smartDeblock: false }) + .toBuffer() + .then(withoutSmartDeblock => + sharp(fixtures.inputPngOverlayLayer0) + .resize(320, 240) + .webp({ quality: 30, smartDeblock: true }) + .toBuffer() + .then(withSmartDeblock => { + assert.strictEqual(true, withSmartDeblock.length !== withoutSmartDeblock.length); + }) + ) + ); + + it('invalid smartDeblock throws', () => { + assert.throws( + () => sharp().webp({ smartDeblock: 1 }), + /Expected boolean for webpSmartDeblock but received 1 of type number/ + ); + }); + + it('should produce a different file size with specific preset', () => + sharp(fixtures.inputJpg) + .resize(320, 240) + .webp({ preset: 'default' }) + .toBuffer() + .then(presetDefault => + sharp(fixtures.inputJpg) + .resize(320, 240) + .webp({ preset: 'picture' }) + .toBuffer() + .then(presetPicture => { + assert.notStrictEqual(presetDefault.length, presetPicture.length); + }) + ) + ); + + it('invalid preset throws', () => { + assert.throws( + () => sharp().webp({ preset: 'fail' }), + /Expected one of: default, photo, picture, drawing, icon, text for preset but received fail of type string/ + ); + }); + + it('should produce a smaller file size with increased effort', () => sharp(fixtures.inputJpg) .resize(320, 240) .webp() .toBuffer() - .then(reductionEffort4 => + .then(effort4 => sharp(fixtures.inputJpg) .resize(320, 240) - .webp({ reductionEffort: 6 }) + .webp({ effort: 6 }) .toBuffer() - .then(reductionEffort6 => { - assert.strictEqual(true, reductionEffort4.length > reductionEffort6.length); + .then(effort6 => { + assert.strictEqual(true, effort4.length > effort6.length); }) ) ); - it('invalid reductionEffort throws', () => { - assert.throws(() => { - sharp().webp({ reductionEffort: true }); - }); + it('should produce different file size with/out shrink-on-load', async () => { + const [shrunk, resized] = await Promise.all([ + sharp(fixtures.inputWebP).resize({ width: 16 }).toBuffer(), + sharp(fixtures.inputWebP).resize({ width: 16, fastShrinkOnLoad: false, kernel: 'nearest' }).toBuffer() + ]); + assert.notStrictEqual(shrunk.length, resized.length); }); - it('out of range reductionEffort throws', () => { + it('invalid effort throws', () => { assert.throws(() => { - sharp().webp({ reductionEffort: -1 }); + sharp().webp({ effort: true }); }); }); - it('invalid pageHeight throws', () => { + it('out of range effort throws', () => { assert.throws(() => { - sharp().webp({ pageHeight: 0 }); + sharp().webp({ effort: -1 }); }); }); + it('should set effort to 0', () => { + const effort = sharp().webp({ effort: 0 }).options.webpEffort; + + assert.strictEqual(effort, 0); + }); + + it('valid minSize', () => { + assert.doesNotThrow(() => sharp().webp({ minSize: true })); + }); + + it('invalid minSize throws', () => { + assert.throws( + () => sharp().webp({ minSize: 1 }), + /Expected boolean for webpMinSize but received 1 of type number/ + ); + }); + + it('valid mixed', () => { + assert.doesNotThrow(() => sharp().webp({ mixed: true })); + }); + + it('invalid mixed throws', () => { + assert.throws( + () => sharp().webp({ mixed: 'fail' }), + /Expected boolean for webpMixed but received fail of type string/ + ); + }); + it('invalid loop throws', () => { assert.throws(() => { sharp().webp({ loop: -1 }); @@ -145,7 +225,7 @@ describe('WebP', function () { it('invalid delay throws', () => { assert.throws(() => { - sharp().webp({ delay: [-1] }); + sharp().webp({ delay: -1 }); }); assert.throws(() => { @@ -153,16 +233,13 @@ describe('WebP', function () { }); }); - it('should double the number of frames with default delay', async () => { - const original = await sharp(fixtures.inputWebPAnimated, { pages: -1 }).metadata(); + it('should repeat a single delay for all frames', async () => { const updated = await sharp(fixtures.inputWebPAnimated, { pages: -1 }) - .webp({ pageHeight: original.pageHeight / 2 }) + .webp({ delay: 100 }) .toBuffer() .then(data => sharp(data, { pages: -1 }).metadata()); - assert.strictEqual(updated.pages, original.pages * 2); - assert.strictEqual(updated.pageHeight, original.pageHeight / 2); - assert.deepStrictEqual(updated.delay, [...original.delay, ...Array(9).fill(120)]); + assert.deepStrictEqual(updated.delay, Array(updated.pages).fill(100)); }); it('should limit animation loop', async () => { @@ -186,11 +263,20 @@ describe('WebP', function () { assert.deepStrictEqual(updated.delay, expectedDelay); }); - it('should work with streams when only animated is set', function (done) { + it('should preserve delay between frames', async () => { + const updated = await sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .webp() + .toBuffer() + .then(data => sharp(data, { pages: -1 }).metadata()); + + assert.deepStrictEqual(updated.delay, [120, 120, 90, 120, 120, 90, 120, 90, 30]); + }); + + it('should work with streams when only animated is set', (_t, done) => { fs.createReadStream(fixtures.inputWebPAnimated) .pipe(sharp({ animated: true })) - .webp({ lossless: true }) - .toBuffer(function (err, data, info) { + .webp({ lossless: true, effort: 0 }) + .toBuffer((err, data, info) => { if (err) throw err; assert.strictEqual(true, data.length > 0); assert.strictEqual('webp', info.format); @@ -198,15 +284,37 @@ describe('WebP', function () { }); }); - it('should work with streams when only pages is set', function (done) { + it('should work with streams when only pages is set', (_t, done) => { fs.createReadStream(fixtures.inputWebPAnimated) .pipe(sharp({ pages: -1 })) - .webp({ lossless: true }) - .toBuffer(function (err, data, info) { + .webp({ lossless: true, effort: 0 }) + .toBuffer((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 resize animated image to page height', async () => { + const updated = await sharp(fixtures.inputWebPAnimated, { pages: -1 }) + .resize({ height: 570 }) + .webp({ effort: 0 }) + .toBuffer() + .then(data => sharp(data, { pages: -1 }).metadata()); + + assert.strictEqual(updated.height, 570 * 9); + assert.strictEqual(updated.pageHeight, 570); + }); + + it('should take page parameter into account when animated is set', async () => { + const updated = await sharp(fixtures.inputWebPAnimated, { animated: true, page: 2 }) + .resize({ height: 570 }) + .webp({ effort: 0 }) + .toBuffer() + .then(data => sharp(data, { pages: -1 }).metadata()); + + assert.strictEqual(updated.height, 570 * 7); + assert.strictEqual(updated.pageHeight, 570); + }); });