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/README.md b/README.md index 84690edf5..ba0db79c0 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,6 @@ readableStream A [guide for contributors](https://github.com/lovell/sharp/blob/main/.github/CONTRIBUTING.md) covers reporting bugs, requesting features and submitting code changes. -[![Node-API v5](https://img.shields.io/badge/Node--API-v5-green.svg)](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix) - ## Licensing Copyright 2013 Lovell Fuller and others. diff --git a/docs/api-colour.md b/docs/api-colour.md index 4594a6e31..87af7e2ec 100644 --- a/docs/api-colour.md +++ b/docs/api-colour.md @@ -55,7 +55,8 @@ Alternative spelling of `greyscale`. 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). +All operations will use this colourspace before converting to the output colourspace, +as defined by [toColourspace](#tocolourspace). This feature is experimental and has not yet been fully-tested with all operations. diff --git a/docs/api-constructor.md b/docs/api-constructor.md index 30cbb3daa..57d64d223 100644 --- a/docs/api-constructor.md +++ b/docs/api-constructor.md @@ -51,9 +51,9 @@ Implements the [stream.Duplex](http://nodejs.org/api/stream.html#stream_class_st | [options.text.text] | string | | text to render as a UTF-8 string. It can contain Pango markup, for example `LeMonde`. | | [options.text.font] | string | | font name to render with. | | [options.text.fontfile] | string | | absolute filesystem path to a font file that can be used by `font`. | -| [options.text.width] | number | 0 | integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. | -| [options.text.height] | number | 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. | -| [options.text.align] | string | "'left'" | text alignment (`'left'`, `'centre'`, `'center'`, `'right'`). | +| [options.text.width] | number | 0 | Integral number of pixels to word-wrap at. Lines of text wider than this will be broken at word boundaries. | +| [options.text.height] | number | 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. | +| [options.text.align] | string | "'left'" | Alignment style for multi-line text (`'left'`, `'centre'`, `'center'`, `'right'`). | | [options.text.justify] | boolean | false | set this to true to apply justification to the text. | | [options.text.dpi] | number | 72 | the resolution (size) at which to render the text. Does not take effect if `height` is specified. | | [options.text.rgba] | boolean | false | set this to true to enable RGBA output. This is useful for colour emoji rendering, or support for pango markup features like `Red!`. | diff --git a/docs/api-operation.md b/docs/api-operation.md index 612e57853..b97f279d5 100644 --- a/docs/api-operation.md +++ b/docs/api-operation.md @@ -93,7 +93,7 @@ Perform an affine transform on an image. This operation will always occur after 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`. +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` @@ -127,7 +127,7 @@ where: 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 @@ -265,6 +265,31 @@ await sharp(rgbaInput) ``` +## 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 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`. diff --git a/docs/api-output.md b/docs/api-output.md index 8d997fdb0..447c7c240 100644 --- a/docs/api-output.md +++ b/docs/api-output.md @@ -6,7 +6,7 @@ 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. +See [withMetadata](#withmetadata) for control over this. The caller is responsible for ensuring directory structures and permissions exist. @@ -42,12 +42,12 @@ sharp(input) 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. +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. +See [withMetadata](#withmetadata) for control over this. `callback`, if present, gets three arguments `(err, data, info)` where: - `err` is an error, if any. @@ -140,12 +140,18 @@ sharp('input.jpg') ``` **Example** ```js -// Set "IFD0-Copyright" in output EXIF metadata +// Set output EXIF metadata const data = await sharp(input) .withMetadata({ exif: { IFD0: { - Copyright: 'Wernham Hogg' + Copyright: 'The National Gallery' + }, + IFD3: { + GPSLatitudeRef: 'N', + GPSLatitude: '51/1 30/1 3230/100', + GPSLongitudeRef: 'W', + GPSLongitude: '0/1 7/1 4366/100' } } }) @@ -368,12 +374,55 @@ await sharp('in.gif', { animated: true }) .gif({ interFrameMaxError: 8 }) .toFile('optim.gif'); ``` + + +## jp +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](https://sharp.pixelplumbing.com/install#custom-libvips). + + +**Throws**: + +- Error Invalid options + +**Since**: 0.29.1 + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [options] | Object | | output options | +| [options.quality] | number | 80 | quality, integer 1-100 | +| [options.lossless] | boolean | false | use lossless compression mode | +| [options.tileWidth] | number | 512 | horizontal tile size | +| [options.tileHeight] | number | 512 | vertical tile size | +| [options.chromaSubsampling] | string | "'4:4:4'" | 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 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. +The `density` can be set in pixels/inch via [withMetadata](#withmetadata) +instead of providing `xres` and `yres` in pixels/mm. **Throws**: diff --git a/docs/changelog.md b/docs/changelog.md index 73a351958..6b1c540da 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,6 +4,34 @@ Requires libvips v8.14.2 +### v0.32.1 - 27th April 2023 + +* 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) + ### v0.32.0 - 24th March 2023 * Default to using sequential rather than random access read where possible. diff --git a/docs/performance.md b/docs/performance.md index 82131bb53..92613316e 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -98,7 +98,7 @@ Note: jimp does not support premultiply/unpremultiply. | jimp | buffer | buffer | 7.62 | 19.1 | | imagemagick | file | file | 7.96 | 19.9 | | sharp | file | file | 12.97 | 32.4 | -| sharp | buffer | buffer | 13.12 | 45.0 | +| sharp | buffer | buffer | 13.12 | 32.8 | ## Running the benchmark test diff --git a/docs/search-index.json b/docs/search-index.json index 517bf95d8..d11b96f44 100644 --- a/docs/search-index.json +++ b/docs/search-index.json @@ -1 +1 @@ -[{"t":"Prerequisites","d":"Node.js 14.15.0","k":"prerequisites node","l":"/install#prerequisites"},{"t":"Prebuilt binaries","d":"Ready-compiled sharp and libvips binaries are provided for use on the most common platforms macOS x64 10.13 macOS ARM64 Linux x64 glibc 2.17, musl 1.1.24, CPU with SSE4.2 Linux ARM64 glibc 2.17, musl","k":"prebuilt binaries compiled libvips common platforms macos arm linux glibc musl cpu sse","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. See the cross-platform","k":"common problems architecture platform node npm install runtime cross","l":"/install#common-problems"},{"t":"Apple M1","d":"Prebuilt sharp and libvips binaries have been provided for macOS on ARM64 since sharp v0.29.0.","k":"apple prebuilt libvips binaries macos arm","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 and that it can be lo","k":"custom libvips globally installed version instead binaries 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, prebuilt sharp binarie","k":"building source module compiled npm install time globally installed libvips detected environment variable skip prebuilt binarie","l":"/install#building-from-source"},{"t":"Custom prebuilt binaries","d":"This is an advanced approach that most people will not require.","k":"custom prebuilt binaries advanced approach people require","l":"/install#custom-prebuilt-binaries"},{"t":"Prebuilt sharp binaries","d":"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 sharp binaries f","k":"prebuilt binaries install custom url npm config option environment variable","l":"/install#prebuilt-sharp-binaries"},{"t":"Prebuilt libvips binaries","d":"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. To install the prebuil","k":"prebuilt libvips binaries install custom url npm config option environment variable prebuil","l":"/install#prebuilt-libvips-binaries"},{"t":"Chinese mirror","d":"A mirror site based in China, provided by Alibaba, contains binaries for both sharp and libvips. To use this either set the following configuration sh npm config set sharp_binary_host https//npmmirror","k":"chinese mirror china alibaba binaries libvips configuration npm config https npmmirror","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 pkg pkgconf usr ports graphics 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 allocations. For this rea","k":"linux memory allocator glibc systems debian red hat long running multi threaded processes small allocations rea","l":"/install#linux-memory-allocator"},{"t":"AWS Lambda","d":"The node_modules directory of the deployment package must include binaries for the Linux x64 platform. When building your deployment package on machines other than Linux x64 glibc, run the following a","k":"aws lambda nodemodules directory deployment package binaries linux platform building machines glibc","l":"/install#aws-lambda"},{"t":"webpack","d":"Ensure sharp is excluded from bundling via the externals configuration. js externals sharp commonjs sharp","k":"webpack excluded bundling externals configuration commonjs","l":"/install#webpack"},{"t":"esbuild","d":"Ensure sharp is excluded from bundling via the external","k":"esbuild excluded bundling external","l":"/install#esbuild"},{"t":"TypeScript","d":"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 ensu","k":"typescript definitions published package types deprecated ensu","l":"/install#typescript"},{"t":"Fonts","d":"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 ma","k":"fonts creating text images rendering svg contain elements fontconfig find relevant windows macos systems system","l":"/install#fonts"},{"t":"Worker threads","d":"On some platforms, including glibc-based Linux, the main thread must call requiresharp _before_ worker threads are created. This is to ensure shared libraries remain loaded in memory until after all t","k":"worker threads platforms glibc linux main thread shared libraries remain loaded memory","l":"/install#worker-threads"},{"t":"Canvas and Windows","d":"The prebuilt binaries provided by canvas for Windows from v2.7.0 onwards depend on the Visual C Runtime MSVCRT. These conflict with the binaries provided by sharp, which depend on the more modern Univ","k":"canvas windows prebuilt binaries onwards depend visual runtime msvcrt conflict modern univ","l":"/install#canvas-and-windows"},{"t":"Sharp","d":"Emits codeSharpeventinfo/code, codeSharpeventwarning/code a namenew_Sharp_new/a","k":"emits code fail limit input pixels unlimited sequential read density ignore icc pages page subifd level animated raw create text","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 multiple processing pipe","k":"clone snapshot instance returning new cloned instances inherit input parent multiple output streams processing pipe","l":"/api-constructor#clone"},{"t":"metadata","d":"Fast access to uncached image metadata without decoding any compressed pixel data.","k":"metadata fast access uncached decoding compressed pixel data","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","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, GIF and raw pixel data output are supported.","k":"tobuffer write output buffer jpeg png webp avif tiff gif raw pixel data resolve object","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 provided.","k":"withmetadata metadata exif xmp iptc input output convert add web friendly srgb icc profile custom orientation density","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 quality progressive chroma subsampling optimise coding optimize mozjpeg trellis quantisation overshoot deringing scans table quantization force","l":"/api-output#jpeg"},{"t":"png","d":"Use these PNG options for output image.","k":"png output progressive compression level adaptive filtering palette quality effort colours colors dither force","l":"/api-output#png"},{"t":"webp","d":"Use these WebP options for output image.","k":"webp output quality alpha lossless near smart subsample effort loop delay min size mixed force","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for the output image.","k":"gif output reuse progressive colours colors effort dither inter frame max error palette loop delay force","l":"/api-output#gif"},{"t":"tiff","d":"Use these TIFF options for output image.","k":"tiff output quality force compression predictor pyramid tile width height xres yres resolution unit bitdepth","l":"/api-output#tiff"},{"t":"avif","d":"Use these AVIF options for output image.","k":"avif output quality lossless effort chroma subsampling","l":"/api-output#avif"},{"t":"heif","d":"Use these HEIF options for output image.","k":"heif output quality compression lossless effort chroma subsampling","l":"/api-output#heif"},{"t":"jxl","d":"Use these JPEG-XL JXL options for output image.","k":"jxl jpeg output distance quality decoding tier lossless effort depth","l":"/api-output#jxl"},{"t":"tile","d":"Use tile-based deep zoom image pyramid output.","k":"tile deep zoom pyramid output size overlap angle background depth skip blanks container layout centre center basename","l":"/api-output#tile"},{"t":"timeout","d":"Set a timeout for processing, in seconds. Use a value of zero to continue processing indefinitely, the default behaviour.","k":"timeout processing seconds zero continue indefinitely behaviour","l":"/api-output#timeout"},{"t":"resize","d":"Resize image to width, height or width x height.","k":"resize width height fit position background kernel enlargement reduction fast shrink load","l":"/api-resize#resize"},{"t":"extend","d":"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 an","k":"extend pad extrude edges background colour pixels derived operation resizing extraction","l":"/api-resize#extend"},{"t":"extract","d":"Extract/crop a region of the image.","k":"extract crop region left top width height","l":"/api-resize#extract"},{"t":"trim","d":"Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel.","k":"trim pixels edges contain similar background colour defaults top left pixel","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 background","l":"/api-operation#rotate"},{"t":"flip","d":"Flip the image about the vertical Y axis. This always occurs before rotation, if any. The use of flip implies the removal of the EXIF Orientation tag, if any.","k":"flip vertical axis rotation removal exif orientation tag","l":"/api-operation#flip"},{"t":"flop","d":"Flop the image about the horizontal X axis. This always occurs before rotation, if any. The use of flop implies the removal of the EXIF Orientation tag, if any.","k":"flop horizontal axis rotation 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 background idx idy odx ody interpolator","l":"/api-operation#affine"},{"t":"sharpen","d":"Sharpen the image.","k":"sharpen sigma","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.","k":"blur","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 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 the perceived bright","k":"gamma apply correction reducing encoding darken resize factor increasing brighten post improve perceived bright alpha","l":"/api-operation#gamma"},{"t":"normalise","d":"Enhance output image contrast by stretching its luminance to cover a full dynamic range.","k":"normalise enhance output contrast stretching luminance cover full dynamic range lower upper","l":"/api-operation#normalise"},{"t":"normalize","d":"Alternative spelling of normalise.","k":"normalize normalise lower upper","l":"/api-operation#normalize"},{"t":"clahe","d":"Perform contrast limiting adaptive histogram equalization CLAHE.","k":"clahe contrast limiting adaptive histogram equalization width height max slope","l":"/api-operation#clahe"},{"t":"convolve","d":"Convolve the image with the specified kernel.","k":"convolve 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 equal otherwise greyscale grayscale raw","l":"/api-operation#threshold"},{"t":"recomb","d":"Recomb the image with the specified matrix.","k":"recomb matrix","l":"/api-operation#recomb"},{"t":"modulate","d":"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","k":"modulate transforms brightness saturation hue rotation lightness operate luminance difference being multiplicative whereas","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 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 al","k":"ensurealpha output alpha transparency channel missing added level defaulting 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-friendly sRGB, with additio","k":"joinchannel join channels meaning added depends output colourspace tocolourspace web friendly srgb additio","l":"/api-channel#joinchannel"},{"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 results. By default th","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma results","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"pipelineColorspace","d":"Alternative spelling of pipelineColourspace.","k":"","l":"/api-colour#pipelinecolorspace"},{"t":"versions","d":"An Object containing the version numbers of sharp, libvips and its dependencies.","k":"versions object version numbers libvips dependencies","l":"/api-utility#versions"},{"t":"interpolators","d":"An Object containing the available interpolators and their proper values","k":"interpolators object proper","l":"/api-utility#interpolators"},{"t":"vendor","d":"An Object containing the platform and architecture of the current and installed vendored binaries.","k":"vendor object platform architecture installed vendored binaries","l":"/api-utility#vendor"},{"t":"queue","d":"An EventEmitter that emits a change event when a task is either - queued, waiting for _libuv_ to provide a worker thread - complete","k":"queue eventemitter emits change event queued waiting libuv worker thread complete","l":"/api-utility#queue"},{"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 returns cache statistics,","k":"cache limits libvips operation existing entries trimmed change method returns statistics memory files items","l":"/api-utility#cache"},{"t":"concurrency","d":"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 o","k":"concurrency maximum number threads libvips process thread pool managed glib helps avoid overhead","l":"/api-utility#concurrency"},{"t":"counters","d":"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 c","k":"counters provides access internal queue number tasks module queued waiting libuv worker thread pool process resize","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 libvips compiled liborc","l":"/api-utility#simd"}] \ No newline at end of file +[{"t":"Prerequisites","d":"Node.js 14.15.0","k":"prerequisites node","l":"/install#prerequisites"},{"t":"Prebuilt binaries","d":"Ready-compiled sharp and libvips binaries are provided for use on the most common platforms macOS x64 10.13 macOS ARM64 Linux x64 glibc 2.17, musl 1.1.24, CPU with SSE4.2 Linux ARM64 glibc 2.17, musl","k":"prebuilt binaries compiled libvips common platforms macos arm linux glibc musl cpu sse","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. See the cross-platform","k":"common problems architecture platform node npm install runtime cross","l":"/install#common-problems"},{"t":"Apple M1","d":"Prebuilt sharp and libvips binaries have been provided for macOS on ARM64 since sharp v0.29.0.","k":"apple prebuilt libvips binaries macos arm","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 and that it can be lo","k":"custom libvips globally installed version instead binaries 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, prebuilt sharp binarie","k":"building source module compiled npm install time globally installed libvips detected environment variable skip prebuilt binarie","l":"/install#building-from-source"},{"t":"Custom prebuilt binaries","d":"This is an advanced approach that most people will not require.","k":"custom prebuilt binaries advanced approach people require","l":"/install#custom-prebuilt-binaries"},{"t":"Prebuilt sharp binaries","d":"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 sharp binaries f","k":"prebuilt binaries install custom url npm config option environment variable","l":"/install#prebuilt-sharp-binaries"},{"t":"Prebuilt libvips binaries","d":"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. To install the prebuil","k":"prebuilt libvips binaries install custom url npm config option environment variable prebuil","l":"/install#prebuilt-libvips-binaries"},{"t":"Chinese mirror","d":"A mirror site based in China, provided by Alibaba, contains binaries for both sharp and libvips. To use this either set the following configuration sh npm config set sharp_binary_host https//npmmirror","k":"chinese mirror china alibaba binaries libvips configuration npm config https npmmirror","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 pkg pkgconf usr ports graphics 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 allocations. For this rea","k":"linux memory allocator glibc systems debian red hat long running multi threaded processes small allocations rea","l":"/install#linux-memory-allocator"},{"t":"AWS Lambda","d":"The node_modules directory of the deployment package must include binaries for the Linux x64 platform. When building your deployment package on machines other than Linux x64 glibc, run the following a","k":"aws lambda nodemodules directory deployment package binaries linux platform building machines glibc","l":"/install#aws-lambda"},{"t":"webpack","d":"Ensure sharp is excluded from bundling via the externals configuration. js externals sharp commonjs sharp","k":"webpack excluded bundling externals configuration commonjs","l":"/install#webpack"},{"t":"esbuild","d":"Ensure sharp is excluded from bundling via the external","k":"esbuild excluded bundling external","l":"/install#esbuild"},{"t":"TypeScript","d":"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 ensu","k":"typescript definitions published package types deprecated ensu","l":"/install#typescript"},{"t":"Fonts","d":"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 ma","k":"fonts creating text images rendering svg contain elements fontconfig find relevant windows macos systems system","l":"/install#fonts"},{"t":"Worker threads","d":"On some platforms, including glibc-based Linux, the main thread must call requiresharp _before_ worker threads are created. This is to ensure shared libraries remain loaded in memory until after all t","k":"worker threads platforms glibc linux main thread shared libraries remain loaded memory","l":"/install#worker-threads"},{"t":"Canvas and Windows","d":"The prebuilt binaries provided by canvas for Windows from v2.7.0 onwards depend on the Visual C Runtime MSVCRT. These conflict with the binaries provided by sharp, which depend on the more modern Univ","k":"canvas windows prebuilt binaries onwards depend visual runtime msvcrt conflict modern univ","l":"/install#canvas-and-windows"},{"t":"Sharp","d":"Emits codeSharpeventinfo/code, codeSharpeventwarning/code a namenew_Sharp_new/a","k":"emits code fail limit input pixels unlimited sequential read density ignore icc pages page subifd level animated raw create text","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 multiple processing pipe","k":"clone snapshot instance returning new cloned instances inherit input parent multiple output streams processing pipe","l":"/api-constructor#clone"},{"t":"metadata","d":"Fast access to uncached image metadata without decoding any compressed pixel data.","k":"metadata fast access uncached decoding compressed pixel data","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","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, GIF and raw pixel data output are supported.","k":"tobuffer write output buffer jpeg png webp avif tiff gif raw pixel data resolve object","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 provided.","k":"withmetadata metadata exif xmp iptc input output convert add web friendly srgb icc profile custom orientation density","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 quality progressive chroma subsampling optimise coding optimize mozjpeg trellis quantisation overshoot deringing scans table quantization force","l":"/api-output#jpeg"},{"t":"png","d":"Use these PNG options for output image.","k":"png output progressive compression level adaptive filtering palette quality effort colours colors dither force","l":"/api-output#png"},{"t":"webp","d":"Use these WebP options for output image.","k":"webp output quality alpha lossless near smart subsample effort loop delay min size mixed force","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for the output image.","k":"gif output reuse progressive colours colors effort dither inter frame max error palette loop delay force","l":"/api-output#gif"},{"t":"jp","d":"Use these JP2 options for output image.","k":"output quality lossless tile width height chroma subsampling","l":"/api-output#jp"},{"t":"tiff","d":"Use these TIFF options for output image.","k":"tiff output quality force compression predictor pyramid tile width height xres yres resolution unit bitdepth","l":"/api-output#tiff"},{"t":"avif","d":"Use these AVIF options for output image.","k":"avif output quality lossless effort chroma subsampling","l":"/api-output#avif"},{"t":"heif","d":"Use these HEIF options for output image.","k":"heif output quality compression lossless effort chroma subsampling","l":"/api-output#heif"},{"t":"jxl","d":"Use these JPEG-XL JXL options for output image.","k":"jxl jpeg output distance quality decoding tier lossless effort depth","l":"/api-output#jxl"},{"t":"tile","d":"Use tile-based deep zoom image pyramid output.","k":"tile deep zoom pyramid output size overlap angle background depth skip blanks container layout centre center basename","l":"/api-output#tile"},{"t":"timeout","d":"Set a timeout for processing, in seconds. Use a value of zero to continue processing indefinitely, the default behaviour.","k":"timeout processing seconds zero continue indefinitely behaviour","l":"/api-output#timeout"},{"t":"resize","d":"Resize image to width, height or width x height.","k":"resize width height fit position background kernel enlargement reduction fast shrink load","l":"/api-resize#resize"},{"t":"extend","d":"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 an","k":"extend pad extrude edges background colour pixels derived operation resizing extraction","l":"/api-resize#extend"},{"t":"extract","d":"Extract/crop a region of the image.","k":"extract crop region left top width height","l":"/api-resize#extract"},{"t":"trim","d":"Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel.","k":"trim pixels edges contain similar background colour defaults top left pixel","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 background","l":"/api-operation#rotate"},{"t":"flip","d":"Flip the image about the vertical Y axis. This always occurs before rotation, if any. The use of flip implies the removal of the EXIF Orientation tag, if any.","k":"flip vertical axis rotation removal exif orientation tag","l":"/api-operation#flip"},{"t":"flop","d":"Flop the image about the horizontal X axis. This always occurs before rotation, if any. The use of flop implies the removal of the EXIF Orientation tag, if any.","k":"flop horizontal axis rotation 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 background idx idy odx ody interpolator","l":"/api-operation#affine"},{"t":"sharpen","d":"Sharpen the image.","k":"sharpen sigma","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.","k":"blur","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 remove","l":"/api-operation#flatten"},{"t":"unflatten","d":"Ensure the image has an alpha channel with all white pixel values made fully transparent.","k":"unflatten alpha channel white pixel made fully transparent","l":"/api-operation#unflatten"},{"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 the perceived bright","k":"gamma apply correction reducing encoding darken resize factor increasing brighten post improve perceived bright alpha","l":"/api-operation#gamma"},{"t":"normalise","d":"Enhance output image contrast by stretching its luminance to cover a full dynamic range.","k":"normalise enhance output contrast stretching luminance cover full dynamic range lower upper","l":"/api-operation#normalise"},{"t":"normalize","d":"Alternative spelling of normalise.","k":"normalize normalise lower upper","l":"/api-operation#normalize"},{"t":"clahe","d":"Perform contrast limiting adaptive histogram equalization CLAHE.","k":"clahe contrast limiting adaptive histogram equalization width height max slope","l":"/api-operation#clahe"},{"t":"convolve","d":"Convolve the image with the specified kernel.","k":"convolve 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 equal otherwise greyscale grayscale raw","l":"/api-operation#threshold"},{"t":"recomb","d":"Recomb the image with the specified matrix.","k":"recomb matrix","l":"/api-operation#recomb"},{"t":"modulate","d":"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","k":"modulate transforms brightness saturation hue rotation lightness operate luminance difference being multiplicative whereas","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 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 al","k":"ensurealpha output alpha transparency channel missing added level defaulting 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-friendly sRGB, with additio","k":"joinchannel join channels meaning added depends output colourspace tocolourspace web friendly srgb additio","l":"/api-channel#joinchannel"},{"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 results. By default th","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma results","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"pipelineColorspace","d":"Alternative spelling of pipelineColourspace.","k":"","l":"/api-colour#pipelinecolorspace"},{"t":"versions","d":"An Object containing the version numbers of sharp, libvips and its dependencies.","k":"versions object version numbers libvips dependencies","l":"/api-utility#versions"},{"t":"interpolators","d":"An Object containing the available interpolators and their proper values","k":"interpolators object proper","l":"/api-utility#interpolators"},{"t":"vendor","d":"An Object containing the platform and architecture of the current and installed vendored binaries.","k":"vendor object platform architecture installed vendored binaries","l":"/api-utility#vendor"},{"t":"queue","d":"An EventEmitter that emits a change event when a task is either - queued, waiting for _libuv_ to provide a worker thread - complete","k":"queue eventemitter emits change event queued waiting libuv worker thread complete","l":"/api-utility#queue"},{"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 returns cache statistics,","k":"cache limits libvips operation existing entries trimmed change method returns statistics memory files items","l":"/api-utility#cache"},{"t":"concurrency","d":"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 o","k":"concurrency maximum number threads libvips process thread pool managed glib helps avoid overhead","l":"/api-utility#concurrency"},{"t":"counters","d":"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 c","k":"counters provides access internal queue number tasks module queued waiting libuv worker thread pool process resize","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 libvips compiled liborc","l":"/api-utility#simd"}] \ No newline at end of file diff --git a/install/libvips.js b/install/libvips.js index dfec78e2a..339f22284 100644 --- a/install/libvips.js +++ b/install/libvips.js @@ -11,6 +11,7 @@ const zlib = require('zlib'); const { createHash } = require('crypto'); const detectLibc = require('detect-libc'); +const semverCoerce = require('semver/functions/coerce'); const semverLessThan = require('semver/functions/lt'); const semverSatisfies = require('semver/functions/satisfies'); const simpleGet = require('simple-get'); @@ -77,7 +78,11 @@ const verifyIntegrity = function (platformAndArch) { flush: function (done) { const digest = `sha512-${hash.digest('base64')}`; if (expected !== digest) { - libvips.removeVendoredLibvips(); + try { + libvips.removeVendoredLibvips(); + } catch (err) { + libvips.log(err.message); + } libvips.log(`Integrity expected: ${expected}`); libvips.log(`Integrity received: ${digest}`); done(new Error(`Integrity check failed for ${platformAndArch}`)); @@ -135,17 +140,19 @@ try { throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`); } // Linux libc version check - const libcFamily = detectLibc.familySync(); - const libcVersion = detectLibc.versionSync(); - if (libcFamily === detectLibc.GLIBC && libcVersion && minimumGlibcVersionByArch[arch]) { - const libcVersionWithoutPatch = libcVersion.split('.').slice(0, 2).join('.'); - if (semverLessThan(`${libcVersionWithoutPatch}.0`, `${minimumGlibcVersionByArch[arch]}.0`)) { - handleError(new Error(`Use with glibc ${libcVersion} requires manual installation of libvips >= ${minimumLibvipsVersion}`)); + const libcVersionRaw = detectLibc.versionSync(); + if (libcVersionRaw) { + const libcFamily = detectLibc.familySync(); + const libcVersion = semverCoerce(libcVersionRaw).version; + if (libcFamily === detectLibc.GLIBC && minimumGlibcVersionByArch[arch]) { + if (semverLessThan(libcVersion, semverCoerce(minimumGlibcVersionByArch[arch]).version)) { + handleError(new Error(`Use with glibc ${libcVersionRaw} requires manual installation of libvips >= ${minimumLibvipsVersion}`)); + } } - } - if (libcFamily === detectLibc.MUSL && libcVersion) { - if (semverLessThan(libcVersion, '1.1.24')) { - handleError(new Error(`Use with musl ${libcVersion} requires manual installation of libvips >= ${minimumLibvipsVersion}`)); + if (libcFamily === detectLibc.MUSL) { + if (semverLessThan(libcVersion, '1.1.24')) { + handleError(new Error(`Use with musl ${libcVersionRaw} requires manual installation of libvips >= ${minimumLibvipsVersion}`)); + } } } // Node.js minimum version check diff --git a/lib/agent.js b/lib/agent.js index 4eb2b4f69..74b6f47e5 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -30,7 +30,7 @@ module.exports = function (log) { const proxyAuth = proxy.username && proxy.password ? `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}` : null; - log(`Via proxy ${proxy.protocol}://${proxy.hostname}:${proxy.port} ${proxyAuth ? 'with' : 'no'} credentials`); + log(`Via proxy ${proxy.protocol}//${proxy.hostname}:${proxy.port} ${proxyAuth ? 'with' : 'no'} credentials`); return tunnel({ proxy: { port: Number(proxy.port), diff --git a/lib/colour.js b/lib/colour.js index 09d2cda95..a7761d29d 100644 --- a/lib/colour.js +++ b/lib/colour.js @@ -70,7 +70,8 @@ function grayscale (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}. + * All operations will use this colourspace before converting to the output colourspace, + * as defined by {@link #tocolourspace|toColourspace}. * * This feature is experimental and has not yet been fully-tested with all operations. * diff --git a/lib/constructor.js b/lib/constructor.js index 62921f2ab..6b60cde00 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -154,9 +154,9 @@ const debuglog = util.debuglog('sharp'); * @param {string} [options.text.text] - text to render as a UTF-8 string. It can contain Pango markup, for example `LeMonde`. * @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] - 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'] - text alignment (`'left'`, `'centre'`, `'center'`, `'right'`). + * @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 `Red!`. @@ -217,6 +217,7 @@ const Sharp = function (input, options) { tintB: 128, flatten: false, flattenBackground: [0, 0, 0], + unflatten: false, negate: false, negateAlpha: true, medianSize: 0, diff --git a/lib/index.d.ts b/lib/index.d.ts index d5dbb0be6..0a815a16c 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -356,7 +356,7 @@ declare namespace 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 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`. + * 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 @@ -427,6 +427,13 @@ declare namespace sharp { */ 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. diff --git a/lib/libvips.js b/lib/libvips.js index 003c93176..ca001d953 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -88,8 +88,7 @@ const hasVendoredLibvips = function () { /* istanbul ignore next */ const removeVendoredLibvips = function () { - const rm = fs.rmSync ? fs.rmSync : fs.rmdirSync; - rm(vendorPath, { recursive: true, maxRetries: 3, force: true }); + fs.rmSync(vendorPath, { recursive: true, maxRetries: 3, force: true }); }; /* istanbul ignore next */ diff --git a/lib/operation.js b/lib/operation.js index b58583ad3..7e2b3aef1 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -114,7 +114,7 @@ function flop (flop) { * * 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`. + * 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` @@ -131,7 +131,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 @@ -405,6 +405,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`. @@ -875,6 +901,7 @@ module.exports = function (Sharp) { median, blur, flatten, + unflatten, gamma, negate, normalise, diff --git a/lib/output.js b/lib/output.js index d9263dfe2..2268ab3d3 100644 --- a/lib/output.js +++ b/lib/output.js @@ -43,7 +43,7 @@ const bitdepthFromColourCount = (colours) => 1 << 31 - Math.clz32(Math.ceil(Math * 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. * @@ -95,12 +95,12 @@ function toFile (fileOut, callback) { * Write output to a Buffer. * JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported. * - * Use {@link toFormat} or one of the format-specific functions such as {@link jpeg}, {@link png} etc. to set the output format. + * Use {@link #toformat|toFormat} or one of the format-specific functions such as {@link jpeg}, {@link 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. @@ -177,12 +177,18 @@ function toBuffer (options, callback) { * .then(info => { ... }); * * @example - * // Set "IFD0-Copyright" in output EXIF metadata + * // Set output EXIF metadata * const data = await sharp(input) * .withMetadata({ * exif: { * IFD0: { - * Copyright: 'Wernham Hogg' + * Copyright: 'The National Gallery' + * }, + * IFD3: { + * GPSLatitudeRef: 'N', + * GPSLatitude: '51/1 30/1 3230/100', + * GPSLongitudeRef: 'W', + * GPSLongitude: '0/1 7/1 4366/100' * } * } * }) @@ -626,6 +632,7 @@ function gif (options) { return this._updateFormatOut('gif', options); } +/* istanbul ignore next */ /** * Use these JP2 options for output image. * @@ -659,7 +666,6 @@ function gif (options) { * @returns {Sharp} * @throws {Error} Invalid options */ -/* istanbul ignore next */ function jp2 (options) { if (!this.constructor.format.jp2k.output.buffer) { throw errJp2Save(); @@ -740,7 +746,8 @@ function trySetAnimationOptions (source, target) { /** * Use these TIFF options for output image. * - * The `density` can be set in pixels/inch via {@link withMetadata} instead of providing `xres` and `yres` in pixels/mm. + * 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 diff --git a/package.json b/package.json index a9bdc8b41..373719e5e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sharp", "description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, GIF, AVIF and TIFF images", - "version": "0.32.0", + "version": "0.32.1", "author": "Lovell Fuller ", "homepage": "https://github.com/lovell/sharp", "contributors": [ @@ -134,9 +134,9 @@ "dependencies": { "color": "^4.2.3", "detect-libc": "^2.0.1", - "node-addon-api": "^6.0.0", + "node-addon-api": "^6.1.0", "prebuild-install": "^7.1.1", - "semver": "^7.3.8", + "semver": "^7.5.0", "simple-get": "^4.0.1", "tar-fs": "^2.1.1", "tunnel-agent": "^0.6.0" @@ -147,7 +147,7 @@ "cc": "^3.0.1", "exif-reader": "^1.2.0", "extract-zip": "^2.0.1", - "icc": "^2.0.0", + "icc": "^3.0.0", "jsdoc-to-markdown": "^8.0.0", "license-checker": "^25.0.1", "mocha": "^10.2.0", @@ -155,7 +155,7 @@ "nyc": "^15.1.0", "prebuild": "^11.0.4", "semistandard": "^16.0.1", - "tsd": "^0.28.0" + "tsd": "^0.28.1" }, "license": "Apache-2.0", "config": { diff --git a/src/common.cc b/src/common.cc index 4e04f786d..dec73e88e 100644 --- a/src/common.cc +++ b/src/common.cc @@ -65,16 +65,6 @@ namespace sharp { } return vector; } - Napi::Buffer NewOrCopyBuffer(Napi::Env env, char* data, size_t len) { - try { - return Napi::Buffer::New(env, data, len, FreeCallback); - } catch (Napi::Error const &err) { - static_cast(err); - } - Napi::Buffer buf = Napi::Buffer::Copy(env, data, len); - FreeCallback(nullptr, data); - return buf; - } // Create an InputDescriptor instance from a Napi::Object describing an input image InputDescriptor* CreateInputDescriptor(Napi::Object input) { diff --git a/src/common.h b/src/common.h index 3888f3f2b..35e7ac1ad 100644 --- a/src/common.h +++ b/src/common.h @@ -126,7 +126,6 @@ namespace sharp { return static_cast( vips_enum_from_nick(nullptr, type, AttrAsStr(obj, attr).data())); } - Napi::Buffer NewOrCopyBuffer(Napi::Env env, char* data, size_t len); // Create an InputDescriptor instance from a Napi::Object describing an input image InputDescriptor* CreateInputDescriptor(Napi::Object input); diff --git a/src/metadata.cc b/src/metadata.cc index 4cd2ed8d7..6bb861f42 100644 --- a/src/metadata.cc +++ b/src/metadata.cc @@ -230,20 +230,21 @@ class MetadataWorker : public Napi::AsyncWorker { info.Set("orientation", baton->orientation); } if (baton->exifLength > 0) { - info.Set("exif", sharp::NewOrCopyBuffer(env, baton->exif, baton->exifLength)); + info.Set("exif", Napi::Buffer::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback)); } if (baton->iccLength > 0) { - info.Set("icc", sharp::NewOrCopyBuffer(env, baton->icc, baton->iccLength)); + info.Set("icc", Napi::Buffer::NewOrCopy(env, baton->icc, baton->iccLength, sharp::FreeCallback)); } if (baton->iptcLength > 0) { - info.Set("iptc", sharp::NewOrCopyBuffer(env, baton->iptc, baton->iptcLength)); + info.Set("iptc", Napi::Buffer::NewOrCopy(env, baton->iptc, baton->iptcLength, sharp::FreeCallback)); } if (baton->xmpLength > 0) { - info.Set("xmp", sharp::NewOrCopyBuffer(env, baton->xmp, baton->xmpLength)); + info.Set("xmp", Napi::Buffer::NewOrCopy(env, baton->xmp, baton->xmpLength, sharp::FreeCallback)); } if (baton->tifftagPhotoshopLength > 0) { info.Set("tifftagPhotoshop", - sharp::NewOrCopyBuffer(env, baton->tifftagPhotoshop, baton->tifftagPhotoshopLength)); + Napi::Buffer::NewOrCopy(env, baton->tifftagPhotoshop, + baton->tifftagPhotoshopLength, sharp::FreeCallback)); } Callback().MakeCallback(Receiver().Value(), { env.Null(), info }); } else { diff --git a/src/operations.cc b/src/operations.cc index ea8652ec7..e59157ff9 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -186,6 +186,7 @@ namespace sharp { VImage Modulate(VImage image, double const brightness, double const saturation, int const hue, double const lightness) { + VipsInterpretation colourspaceBeforeModulate = image.interpretation(); if (HasAlpha(image)) { // Separate alpha channel VImage alpha = image[image.bands() - 1]; @@ -195,7 +196,7 @@ namespace sharp { { brightness, saturation, 1}, { lightness, 0.0, static_cast(hue) } ) - .colourspace(VIPS_INTERPRETATION_sRGB) + .colourspace(colourspaceBeforeModulate) .bandjoin(alpha); } else { return image @@ -204,7 +205,7 @@ namespace sharp { { brightness, saturation, 1 }, { lightness, 0.0, static_cast(hue) } ) - .colourspace(VIPS_INTERPRETATION_sRGB); + .colourspace(colourspaceBeforeModulate); } } @@ -268,30 +269,20 @@ namespace sharp { if (image.width() < 3 && image.height() < 3) { throw VError("Image to trim must be at least 3x3 pixels"); } - - // Scale up 8-bit values to match 16-bit input image - double multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0; - threshold *= multiplier; - - std::vector backgroundAlpha(1); 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); - multiplier = 1.0; - } - if (HasAlpha(image) && background.size() == 4) { - // Just discard the alpha because flattening the background colour with - // itself (effectively what find_trim() does) gives the same result - backgroundAlpha[0] = background[3] * multiplier; + } else if (sharp::Is16Bit(image.interpretation())) { + for (size_t i = 0; i < background.size(); i++) { + background[i] *= 256.0; + } + threshold *= 256.0; } - if (image.bands() > 2) { - background = { - background[0] * multiplier, - background[1] * multiplier, - background[2] * multiplier - }; + std::vector backgroundAlpha({ background.back() }); + if (HasAlpha(image)) { + background.pop_back(); } else { - background[0] = background[0] * multiplier; + background.resize(image.bands()); } int left, top, width, height; left = image.find_trim(&top, &width, &height, VImage::option() @@ -332,12 +323,26 @@ namespace sharp { if (a.size() > bands) { throw VError("Band expansion using linear is unsupported"); } + bool const uchar = !Is16Bit(image.interpretation()); if (HasAlpha(image) && 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", TRUE)).bandjoin(alpha); + 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 (HasAlpha(image)) { + VImage alpha = image[image.bands() - 1]; + VImage noAlpha = RemoveAlpha(image); + return noAlpha.bandjoin(alpha & (noAlpha.colourspace(VIPS_INTERPRETATION_B_W) < 255)); } else { - return image.linear(a, b, VImage::option()->set("uchar", TRUE)); + return image.bandjoin(image.colourspace(VIPS_INTERPRETATION_B_W) < 255); } } diff --git a/src/operations.h b/src/operations.h index df4d1eaf5..10a0c6ced 100644 --- a/src/operations.h +++ b/src/operations.h @@ -86,6 +86,11 @@ namespace sharp { */ VImage Linear(VImage image, std::vector const a, std::vector const b); + /* + * Unflatten + */ + VImage Unflatten(VImage image); + /* * Recomb with a Matrix of the given bands/channel size. * Eg. RGB will be a 3x3 matrix. diff --git a/src/pipeline.cc b/src/pipeline.cc index 78c01e4d8..663a202ba 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -322,7 +322,10 @@ class PipelineWorker : public Napi::AsyncWorker { } catch(...) { sharp::VipsWarningCallback(nullptr, G_LOG_LEVEL_WARNING, "Invalid embedded profile", nullptr); } - } else if (image.interpretation() == VIPS_INTERPRETATION_CMYK) { + } else if ( + image.interpretation() == VIPS_INTERPRETATION_CMYK && + baton->colourspaceInput != VIPS_INTERPRETATION_CMYK + ) { image = image.icc_transform(processingProfile, VImage::option() ->set("input_profile", "cmyk") ->set("intent", VIPS_INTENT_PERCEPTUAL)); @@ -470,6 +473,7 @@ class PipelineWorker : public Napi::AsyncWorker { image = image.smartcrop(baton->width, baton->height, VImage::option() ->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; @@ -550,7 +554,9 @@ class PipelineWorker : public Napi::AsyncWorker { 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); } @@ -560,6 +566,11 @@ class PipelineWorker : public Napi::AsyncWorker { image = sharp::Blur(image, baton->blurSigma); } + // Unflatten the image + if (baton->unflatten) { + image = sharp::Unflatten(image); + } + // Convolve if (shouldConv) { image = sharp::Convolve(image, @@ -1222,8 +1233,8 @@ class PipelineWorker : public Napi::AsyncWorker { // Add buffer size to info info.Set("size", static_cast(baton->bufferOutLength)); // Pass ownership of output data to Buffer instance - Napi::Buffer data = sharp::NewOrCopyBuffer(env, static_cast(baton->bufferOut), - baton->bufferOutLength); + Napi::Buffer data = Napi::Buffer::NewOrCopy(env, static_cast(baton->bufferOut), + baton->bufferOutLength, sharp::FreeCallback); Callback().MakeCallback(Receiver().Value(), { env.Null(), data, info }); } else { // Add file size to info @@ -1460,6 +1471,7 @@ 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"); @@ -1662,6 +1674,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { baton->rotationAngle != 0.0 || baton->tileAngle != 0 || baton->useExifOrientation || + baton->flip || baton->claheWidth != 0 || !baton->affineMatrix.empty() ) { diff --git a/src/pipeline.h b/src/pipeline.h index aa37090bf..19d9bde97 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -73,6 +73,7 @@ struct PipelineBaton { double tintB; bool flatten; std::vector flattenBackground; + bool unflatten; bool negate; bool negateAlpha; double blurSigma; @@ -239,6 +240,7 @@ struct PipelineBaton { tintB(128.0), flatten(false), flattenBackground{ 0.0, 0.0, 0.0 }, + unflatten(false), negate(false), negateAlpha(true), blurSigma(0.0), 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/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/unit/agent.js b/test/unit/agent.js index 99ba4825a..95d587311 100644 --- a/test/unit/agent.js +++ b/test/unit/agent.js @@ -21,7 +21,7 @@ describe('HTTP agent', function () { assert.strictEqual(123, proxy.options.proxy.port); assert.strictEqual('user:pass', proxy.options.proxy.proxyAuth); assert.strictEqual(443, proxy.defaultPort); - assert.strictEqual(logMsg, 'Via proxy https:://secure:123 with credentials'); + assert.strictEqual(logMsg, 'Via proxy https://secure:123 with credentials'); }); it('HTTPS proxy with auth from HTTPS_PROXY using credentials containing special characters', function () { @@ -34,7 +34,7 @@ describe('HTTP agent', function () { assert.strictEqual(789, proxy.options.proxy.port); assert.strictEqual('user,:pass=', proxy.options.proxy.proxyAuth); assert.strictEqual(443, proxy.defaultPort); - assert.strictEqual(logMsg, 'Via proxy https:://secure:789 with credentials'); + assert.strictEqual(logMsg, 'Via proxy https://secure:789 with credentials'); }); it('HTTP proxy without auth from npm_config_proxy', function () { @@ -47,6 +47,6 @@ describe('HTTP agent', function () { assert.strictEqual(456, proxy.options.proxy.port); assert.strictEqual(null, proxy.options.proxy.proxyAuth); assert.strictEqual(443, proxy.defaultPort); - assert.strictEqual(logMsg, 'Via proxy http:://plaintext:456 no credentials'); + assert.strictEqual(logMsg, 'Via proxy http://plaintext:456 no credentials'); }); }); diff --git a/test/unit/colourspace.js b/test/unit/colourspace.js index 532877cdd..71ce402d6 100644 --- a/test/unit/colourspace.js +++ b/test/unit/colourspace.js @@ -93,6 +93,19 @@ describe('Colour space conversion', 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('From sRGB with RGB16 pipeline, resize with gamma, to sRGB', function (done) { sharp(fixtures.inputPngGradients) .pipelineColourspace('rgb16') diff --git a/test/unit/linear.js b/test/unit/linear.js index 05bdad880..d668aabdf 100644 --- a/test/unit/linear.js +++ b/test/unit/linear.js @@ -51,6 +51,16 @@ describe('Linear adjustment', function () { }); }); + it('applies linear levels adjustment to 16-bit w alpha ch', function (done) { + sharp(fixtures.inputPngWithTransparency16bit) + .linear(a, b) + .png({ compressionLevel: 0 }) + .toBuffer(function (err, data) { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('linear-16bit.png'), data, done); + }); + }); + it('applies slope level adjustment w alpha ch', function (done) { sharp(fixtures.inputPngOverlayLayer1) .resize(240) diff --git a/test/unit/text.js b/test/unit/text.js index 2643e2145..1b5a80c6f 100644 --- a/test/unit/text.js +++ b/test/unit/text.js @@ -8,7 +8,9 @@ const assert = require('assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -describe('Text to image', () => { +describe('Text to image', function () { + this.retries(3); + it('text with default values', async () => { const output = fixtures.path('output.text-default.png'); const text = sharp({ diff --git a/test/unit/trim.js b/test/unit/trim.js index 1025e6971..8d2c76d44 100644 --- a/test/unit/trim.js +++ b/test/unit/trim.js @@ -153,6 +153,32 @@ describe('Trim borders', function () { 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: { diff --git a/test/unit/unflatten.js b/test/unit/unflatten.js new file mode 100644 index 000000000..68147b240 --- /dev/null +++ b/test/unit/unflatten.js @@ -0,0 +1,31 @@ +// Copyright 2013 Lovell Fuller and others. +// SPDX-License-Identifier: Apache-2.0 + +'use strict'; + +const sharp = require('../../'); +const fixtures = require('../fixtures'); + +describe('Unflatten', function () { + it('unflatten white background', function (done) { + sharp(fixtures.inputPng).unflatten() + .toBuffer(function (err, data) { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('unflatten-white-transparent.png'), data, { threshold: 0 }, done); + }); + }); + it('unflatten transparent image', function (done) { + sharp(fixtures.inputPngTrimSpecificColourIncludeAlpha).unflatten() + .toBuffer(function (err, data) { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('unflatten-flag-white-transparent.png'), data, { threshold: 0 }, done); + }); + }); + it('unflatten using threshold', function (done) { + sharp(fixtures.inputPngPalette).unflatten().threshold(128, { grayscale: false }) + .toBuffer(function (err, data) { + if (err) throw err; + fixtures.assertSimilar(fixtures.expected('unflatten-swiss.png'), data, { threshold: 1 }, done); + }); + }); +});