From c3a852eecf04ecca53f4fded26c379bc9525122d Mon Sep 17 00:00:00 2001 From: Mart Date: Tue, 23 Aug 2022 13:28:02 +0200 Subject: [PATCH] Add trim option to provide a specific background colour (#3332) Co-authored-by: Mart Jansink --- docs/api-resize.md | 48 ++++++- docs/search-index.json | 2 +- lib/constructor.js | 1 + lib/resize.js | 69 ++++++++-- package.json | 3 +- src/operations.cc | 32 +++-- src/operations.h | 2 +- src/pipeline.cc | 3 +- src/pipeline.h | 2 + .../Flag_of_the_Netherlands-16bit.png | Bin 0 -> 4857 bytes .../Flag_of_the_Netherlands-alpha.png | Bin 0 -> 812 bytes test/fixtures/Flag_of_the_Netherlands.png | Bin 0 -> 794 bytes test/fixtures/index.js | 3 + test/unit/trim.js | 120 ++++++++++++++---- 14 files changed, 240 insertions(+), 45 deletions(-) create mode 100644 test/fixtures/Flag_of_the_Netherlands-16bit.png create mode 100644 test/fixtures/Flag_of_the_Netherlands-alpha.png create mode 100644 test/fixtures/Flag_of_the_Netherlands.png diff --git a/docs/api-resize.md b/docs/api-resize.md index 0eaef4b1..b2f61e6c 100644 --- a/docs/api-resize.md +++ b/docs/api-resize.md @@ -238,7 +238,7 @@ Returns **Sharp** ## trim -Trim "boring" pixels from all edges that contain values similar to the top-left pixel. +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. @@ -249,9 +249,51 @@ 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`) +* `trim` **([string][10] | [number][8] | [Object][9])** the specific background colour to trim, the threshold for doing so or an Object with both. - + * `trim.background` **([string][10] | [Object][9])** background colour, parsed by the [color][11] module, defaults to that of the top-left pixel. (optional, default `'top-left pixel'`) + * `trim.threshold` **[number][8]** the allowed difference from the above colour, a positive number. (optional, default `10`) + +### Examples + +```javascript +// Trim pixels with a colour similar to that of the top-left pixel. +sharp(input) + .trim() + .toFile(output, function(err, info) { + ... + }); +``` + +```javascript +// Trim pixels with the exact same colour as that of the top-left pixel. +sharp(input) + .trim(0) + .toFile(output, function(err, info) { + ... + }); +``` + +```javascript +// Trim only pixels with a similar colour to red. +sharp(input) + .trim("#FF0000") + .toFile(output, function(err, info) { + ... + }); +``` + +```javascript +// Trim all "yellow-ish" pixels, being more lenient with the higher threshold. +sharp(input) + .trim({ + background: "yellow", + threshold: 42, + }) + .toFile(output, function(err, info) { + ... + }); +``` * Throws **[Error][13]** Invalid parameters diff --git a/docs/search-index.json b/docs/search-index.json index cb1014ce..0325acd3 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 sharp 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 sharp 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 sharp 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 sharp 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 sharp 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 run 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":"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":"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 your machines glibc run","l":"/install#aws-lambda"},{"t":"webpack","d":"Ensure sharp is excluded from bundling via the externals configuration. js externals sharp commonjs sharp","k":"webpack sharp excluded bundling via externals configuration commonjs","l":"/install#webpack"},{"t":"esbuild","d":"Ensure sharp is excluded from bundling via the external","k":"esbuild sharp excluded bundling via external","l":"/install#esbuild"},{"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 available","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 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","k":"canvas windows prebuilt binaries depend gtk updated conflict modern sharp modules","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 multiple processing pipe","k":"clone snapshot sharp 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","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","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 optimisecoding optimizecoding mozjpeg optimisescans optimizescans force","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 quality alphaquality lossless nearlossless smartsubsample effort loop delay minsize mixed force","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for the 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 pixel data. Pixel ordering is left-to-right, top-to-bottom, without padding. Channel ordering will be RGB or RGBA for non-greyscale colourspaces.","k":"raw force output uncompressed pixel data ordering left right top bottom padding channel rgb rgba greyscale colourspaces depth size overlap angle background skipblanks container layout centre center basename","l":"/api-output#raw"},{"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","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.","k":"trim boring pixels edges contain similar 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","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","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 channel in the LAB colou","k":"sharpen parameters fast mild output sigma slower accurate channel lab colou","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 pre resize factor increasing brighten post improve perceived bright","l":"/api-operation#gamma"},{"t":"negate","d":"Produce the negative of the image.","k":"negate negative alpha","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":"clahe","d":"Perform contrast limiting adaptive histogram equalization CLAHE10.","k":"clahe contrast limiting adaptive histogram equalization","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","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 to adjust image levels.","k":"linear apply formula input adjust levels","l":"/api-operation#linear"},{"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 one channels meaning added depends output colourspace tocolourspace web friendly srgb additio","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 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 results. By default th","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma best results","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"pipelineColourspace","d":"Set the pipeline colourspace.","k":"pipeline colourspace","l":"/api-colour#pipelinecolourspace"},{"t":"pipelineColorspace","d":"Alternative spelling of pipelineColourspace.","k":"","l":"/api-colour#pipelinecolorspace"},{"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":"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":"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, u","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 of cr","k":"concurrency maximum number threads libvips process thread pool managed glib helps avoid overhead","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","l":"/api-utility#queue"},{"t":"counters","d":"Provides access to internal task counters.","k":"counters provides access internal","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 sharp 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 sharp 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 sharp 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 sharp 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 sharp 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 run 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":"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":"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 your machines glibc run","l":"/install#aws-lambda"},{"t":"webpack","d":"Ensure sharp is excluded from bundling via the externals configuration. js externals sharp commonjs sharp","k":"webpack sharp excluded bundling via externals configuration commonjs","l":"/install#webpack"},{"t":"esbuild","d":"Ensure sharp is excluded from bundling via the external","k":"esbuild sharp excluded bundling via external","l":"/install#esbuild"},{"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 available","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 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","k":"canvas windows prebuilt binaries depend gtk updated conflict modern sharp modules","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 multiple processing pipe","k":"clone snapshot sharp 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","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","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 optimisecoding optimizecoding mozjpeg optimisescans optimizescans force","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 quality alphaquality lossless nearlossless smartsubsample effort loop delay minsize mixed force","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for the 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 pixel data. Pixel ordering is left-to-right, top-to-bottom, without padding. Channel ordering will be RGB or RGBA for non-greyscale colourspaces.","k":"raw force output uncompressed pixel data ordering left right top bottom padding channel rgb rgba greyscale colourspaces depth size overlap angle background skipblanks container layout centre center basename","l":"/api-output#raw"},{"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","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 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","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","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 channel in the LAB colou","k":"sharpen parameters fast mild output sigma slower accurate channel lab colou","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 pre resize factor increasing brighten post improve perceived bright","l":"/api-operation#gamma"},{"t":"negate","d":"Produce the negative of the image.","k":"negate negative alpha","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":"clahe","d":"Perform contrast limiting adaptive histogram equalization CLAHE10.","k":"clahe contrast limiting adaptive histogram equalization","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","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 to adjust image levels.","k":"linear apply formula input adjust levels","l":"/api-operation#linear"},{"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 one channels meaning added depends output colourspace tocolourspace web friendly srgb additio","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 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 results. By default th","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma best results","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"pipelineColourspace","d":"Set the pipeline colourspace.","k":"pipeline colourspace","l":"/api-colour#pipelinecolourspace"},{"t":"pipelineColorspace","d":"Alternative spelling of pipelineColourspace.","k":"","l":"/api-colour#pipelinecolorspace"},{"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":"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":"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, u","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 of cr","k":"concurrency maximum number threads libvips process thread pool managed glib helps avoid overhead","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","l":"/api-utility#queue"},{"t":"counters","d":"Provides access to internal task counters.","k":"counters provides access internal","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/lib/constructor.js b/lib/constructor.js index 0350807e..25a4e02b 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -224,6 +224,7 @@ const Sharp = function (input, options) { sharpenY3: 20, threshold: 0, thresholdGrayscale: true, + trimBackground: [], trimThreshold: 0, gamma: 0, gammaOut: 0, diff --git a/lib/resize.js b/lib/resize.js index a05c2a66..161c9f2c 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -450,7 +450,7 @@ function extract (options) { } /** - * Trim "boring" pixels from all edges that contain values similar to the top-left pixel. + * 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. * @@ -459,19 +459,72 @@ function extract (options) { * The `info` response Object, obtained from callback of `.toFile()` or `.toBuffer()`, * will contain `trimOffsetLeft` and `trimOffsetTop` properties. * - * @param {number} [threshold=10] the allowed difference from the top-left pixel, a number greater than zero. + * @example + * // Trim pixels with a colour similar to that of the top-left pixel. + * sharp(input) + * .trim() + * .toFile(output, function(err, info) { + * ... + * }); + * @example + * // Trim pixels with the exact same colour as that of the top-left pixel. + * sharp(input) + * .trim(0) + * .toFile(output, function(err, info) { + * ... + * }); + * @example + * // Trim only pixels with a similar colour to red. + * sharp(input) + * .trim("#FF0000") + * .toFile(output, function(err, info) { + * ... + * }); + * @example + * // Trim all "yellow-ish" pixels, being more lenient with the higher threshold. + * sharp(input) + * .trim({ + * background: "yellow", + * threshold: 42, + * }) + * .toFile(output, function(err, info) { + * ... + * }); + * + * @param {string|number|Object} trim - the specific background colour to trim, the threshold for doing so or an Object with both. + * @param {string|Object} [trim.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} [trim.threshold=10] - the allowed difference from the above colour, a positive number. * @returns {Sharp} * @throws {Error} Invalid parameters */ -function trim (threshold) { - if (!is.defined(threshold)) { +function trim (trim) { + if (!is.defined(trim)) { this.options.trimThreshold = 10; - } else if (is.number(threshold) && threshold > 0) { - this.options.trimThreshold = threshold; + } else if (is.string(trim)) { + this._setBackgroundColourOption('trimBackground', trim); + this.options.trimThreshold = 10; + } else if (is.number(trim)) { + if (trim >= 0) { + this.options.trimThreshold = trim; + } else { + throw is.invalidParameterError('threshold', 'positive number', trim); + } + } else if (is.object(trim)) { + this._setBackgroundColourOption('trimBackground', trim.background); + + if (!is.defined(trim.threshold)) { + this.options.trimThreshold = 10; + } else if (is.number(trim.threshold)) { + if (trim.threshold >= 0) { + this.options.trimThreshold = trim.threshold; + } else { + throw is.invalidParameterError('threshold', 'positive number', trim); + } + } } else { - throw is.invalidParameterError('threshold', 'number greater than zero', threshold); + throw is.invalidParameterError('trim', 'string, number or object', trim); } - if (this.options.trimThreshold && isRotationExpected(this.options)) { + if (isRotationExpected(this.options)) { this.options.rotateBeforePreExtract = true; } return this; diff --git a/package.json b/package.json index baf05db8..586a953b 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,8 @@ "Ompal Singh ", "Brodan ", - "Brahim Ait elhaj " + "Brahim Ait elhaj ", + "Mart Jansink " ], "scripts": { "install": "(node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)", diff --git a/src/operations.cc b/src/operations.cc index 8c3dc81a..b288d201 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -262,26 +262,42 @@ namespace sharp { /* Trim an image */ - VImage Trim(VImage image, double const threshold) { + VImage Trim(VImage image, std::vector background, double threshold) { 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(); + + // 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 (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; + } + background = { + background[0] * multiplier, + background[1] * multiplier, + background[2] * multiplier + }; + int left, top, width, height; left = image.find_trim(&top, &width, &height, VImage::option() - ->set("background", background(0, 0)) + ->set("background", background) ->set("threshold", threshold)); if (HasAlpha(image)) { // Search alpha channel (A) int leftA, topA, widthA, heightA; VImage alpha = image[image.bands() - 1]; - VImage backgroundAlpha = alpha.extract_area(0, 0, 1, 1); leftA = alpha.find_trim(&topA, &widthA, &heightA, VImage::option() - ->set("background", backgroundAlpha(0, 0)) + ->set("background", backgroundAlpha) ->set("threshold", threshold)); if (widthA > 0 && heightA > 0) { if (width > 0 && height > 0) { diff --git a/src/operations.h b/src/operations.h index b0545397..c9f2f8c2 100644 --- a/src/operations.h +++ b/src/operations.h @@ -85,7 +85,7 @@ namespace sharp { /* Trim an image */ - VImage Trim(VImage image, double const threshold); + VImage Trim(VImage image, std::vector background, double const threshold); /* * Linear adjustment (a * in + b) diff --git a/src/pipeline.cc b/src/pipeline.cc index f8ea3678..9281c0fe 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -118,7 +118,7 @@ class PipelineWorker : public Napi::AsyncWorker { // Trim if (baton->trimThreshold > 0.0) { MultiPageUnsupported(nPages, "Trim"); - image = sharp::Trim(image, baton->trimThreshold); + image = sharp::Trim(image, baton->trimBackground, baton->trimThreshold); baton->trimOffsetLeft = image.xoffset(); baton->trimOffsetTop = image.yoffset(); } @@ -1451,6 +1451,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { 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->gamma = sharp::AttrAsDouble(options, "gamma"); baton->gammaOut = sharp::AttrAsDouble(options, "gammaOut"); diff --git a/src/pipeline.h b/src/pipeline.h index 8577d353..98b36986 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -97,6 +97,7 @@ struct PipelineBaton { double sharpenY3; int threshold; bool thresholdGrayscale; + std::vector trimBackground; double trimThreshold; int trimOffsetLeft; int trimOffsetTop; @@ -248,6 +249,7 @@ struct PipelineBaton { sharpenY3(20.0), threshold(0), thresholdGrayscale(true), + trimBackground{}, trimThreshold(0.0), trimOffsetLeft(0), trimOffsetTop(0), 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 0000000000000000000000000000000000000000..61ee36d530021eefb4f842e0c46f6109d9ad43cf GIT binary patch literal 4857 zcmeAS@N?(olHy`uVBq!ia0y~yU~XYxV2Ti60*X8}Pd*Q%SkfJR9T^xl_H+M9WCijS zl0AZa85pY67#JFWihlux8eT9klo~KFyh>nTu$sZZAYL$MSD+10f-TA0-R1vk5IkJb z8wM0%FY)wsWq-!O#mU3+o!1j6B6!l%#WAGf*4tZ#qAdmtE(cG)T(HoY^Vx@{qYsoc z|MfJj{eS!8zT<_l_CUo94L$$gF@hKjt2q~gD2F}+knTM|k>ue^VLb2S^N zWItdfH*~A2(WWLS3P-zLpimg?gpT&XNAp4rdlX+m{Pps9V2)5Nag8WRNi0dVN-jzT zQVd20Mn<{@7Pux%nxXX_Z(s7(+CisAGu*YS4h& sP?DLOT3nKtTL9N%Xb@sxX=P|+Wnco)W21ENB2W*5r>mdKI;Vst07v9dHUIzs literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..35a21b4ac7e85e817d0670d3bd031e4bc35d6c00 GIT binary patch literal 812 zcmeAS@N?(olHy`uVBq!ia0y~yU~XYxV2WU328y_^KHveQSkfJR9T^xl_H+M9WCijS zl0AZa85pY67#JFWihlux8eT9klo~KFyh>nTu$sZZAYL$MSD+10f+xTy#C4sF#{d8S z72Uc)Dii%vffRE|kY8{^L&MpMBc?zuW0JSK3$q}P&nF; zzL#-J$jd{(NK`FxjVMV;EJ?LWE=mPb3`PbnTu$sZZAYL$MSD+10f-}G;#C4sF#{d8S z72UdX6~uFZf{aPt?k~IqW5#zOL-gI5RdP`(kYX@0Ff!6Lu+TNI3^6jaGBB_*GS&u?1_nEo_dG<=kei>9nO2Eg!-+bU kSfB{JRR910 literal 0 HcmV?d00001 diff --git a/test/fixtures/index.js b/test/fixtures/index.js index aede886e..6bb712db 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -95,6 +95,9 @@ module.exports = { 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 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 diff --git a/test/unit/trim.js b/test/unit/trim.js index 0531323b..022968a7 100644 --- a/test/unit/trim.js +++ b/test/unit/trim.js @@ -7,22 +7,6 @@ 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) { const expected = fixtures.expected('alpha-layer-2-trim-resize.jpg'); sharp(fixtures.inputJpgOverlayLayer2) @@ -41,7 +25,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 }) @@ -94,7 +78,7 @@ describe('Trim borders', function () { .catch(done); }); - it('should rotate before trim', () => + it('Should rotate before trim', () => sharp({ create: { width: 20, @@ -159,13 +143,105 @@ describe('Trim borders', function () { assert.strictEqual(trimOffsetLeft, 0); }); - describe('Invalid thresholds', function () { - [-1, 'fail', {}].forEach(function (threshold) { - it(JSON.stringify(threshold), function () { + describe('Valid parameters', function () { + const expected = fixtures.expected('alpha-layer-1-fill-trim-resize.png'); + Object.entries({ + 'Background and threshold default': undefined, + 'Background string': '#00000000', + 'Background option': { + background: '#00000000' + }, + 'Threshold number': 10, + 'Threshold option': { + threshold: 10 + } + }).forEach(function ([description, parameter]) { + it(description, function (done) { + sharp(fixtures.inputPngOverlayLayer1) + .resize(450, 322) + .trim(parameter) + .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); + }); + }); + }); + }); + + describe('Invalid parameters', function () { + Object.entries({ + 'Invalid background string': 'fail', + 'Invalid background option': { + background: 'fail' + }, + + 'Negative threshold number': -1, + 'Negative threshold option': { + threshold: -1 + }, + + Boolean: false + }).forEach(function ([description, parameter]) { + it(description, function () { assert.throws(function () { - sharp().trim(threshold); + sharp().trim(parameter); }); }); }); }); + + describe('Specific background colour', function () { + it('Doesn\'t trim at all', async () => { + const { info } = await sharp(fixtures.inputPngTrimSpecificColour) + .trim('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('#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('#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('#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); + }); + }); });