🤖 Merge PR #63711 [sharp] Add options for sharp v0.31.3 by @akwiatek

* [sharp] Add options for sharp v0.31.3

* [sharp] Add options for sharp from before v0.31.0
This commit is contained in:
Adam Kwiatek
2022-12-31 17:30:31 +01:00
committed by GitHub
parent 98c87a4104
commit ecf2e4811d
2 changed files with 155 additions and 25 deletions

112
types/sharp/index.d.ts vendored
View File

@@ -147,11 +147,11 @@ declare namespace sharp {
/**
* Extract a single channel from a multi-channel image.
* @param channel zero-indexed band number to extract, or red, green or blue as alternative to 0, 1 or 2 respectively.
* @param channel zero-indexed channel/band number to extract, or red, green, blue or alpha.
* @throws {Error} Invalid channel
* @returns A sharp instance that can be used to chain operations
*/
extractChannel(channel: number | string): Sharp;
extractChannel(channel: 0 | 1 | 2 | 3 | 'red' | 'green' | 'blue' | 'alpha'): Sharp;
/**
* Join one or more channels to the image. The meaning of the added channels depends on the output colourspace, set with toColourspace().
@@ -174,7 +174,7 @@ declare namespace sharp {
* @throws {Error} Invalid parameters
* @returns A sharp instance that can be used to chain operations
*/
bandbool(boolOp: string): Sharp;
bandbool(boolOp: keyof BoolEnum): Sharp;
//#endregion
@@ -361,7 +361,7 @@ declare namespace sharp {
* Sharpen the image.
* When used without parameters, performs a fast, mild sharpen of the output image.
* When a sigma is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
* Separate control over the level of sharpening in "flat" and "jagged" areas is available.
* Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available.
* @param options if present, is an Object with optional attributes
* @throws {Error} Invalid parameters
* @returns A sharp instance that can be used to chain operations
@@ -372,7 +372,7 @@ declare namespace sharp {
* Sharpen the image.
* When used without parameters, performs a fast, mild sharpen of the output image.
* When a sigma is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
* Separate control over the level of sharpening in "flat" and "jagged" areas is available.
* Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available.
* @param sigma the sigma of the Gaussian mask, where sigma = 1 + radius / 2.
* @param flat the level of sharpening to apply to "flat" areas. (optional, default 1.0)
* @param jagged the level of sharpening to apply to "jagged" areas. (optional, default 2.0)
@@ -413,11 +413,13 @@ declare namespace sharp {
* Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of 1/gamma then increasing the encoding (brighten) post-resize at a factor of gamma.
* This can improve the perceived brightness of a resized image in non-linear colour spaces.
* JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation when applying a gamma correction.
* Supply a second argument to use a different output gamma value, otherwise the first value is used in both cases.
* @param gamma value between 1.0 and 3.0. (optional, default 2.2)
* @param gammaOut value between 1.0 and 3.0. (optional, defaults to same as gamma)
* @throws {Error} Invalid parameters
* @returns A sharp instance that can be used to chain operations
*/
gamma(gamma?: number): Sharp;
gamma(gamma?: number, gammaOut?: number): Sharp;
/**
* Produce the "negative" of the image.
@@ -477,7 +479,7 @@ declare namespace sharp {
* @throws {Error} Invalid parameters
* @returns A sharp instance that can be used to chain operations
*/
boolean(operand: string | Buffer, operator: string, options?: { raw: Raw }): Sharp;
boolean(operand: string | Buffer, operator: keyof BoolEnum, options?: { raw: Raw }): Sharp;
/**
* Apply the linear formula a * input + b to the image (levels adjustment)
@@ -581,7 +583,19 @@ declare namespace sharp {
* @throws {Error} Invalid options
* @returns A sharp instance that can be used to chain operations
*/
jp2: (options?: Jp2Options) => Sharp;
jp2(options?: Jp2Options): Sharp;
/**
* Use these JPEG-XL (JXL) options for output image.
* This feature is experimental, please do not use in production systems.
* Requires libvips compiled with support for libjxl.
* The prebuilt binaries do not include this.
* Image metadata (EXIF, XMP) is unsupported.
* @param options Output options.
* @throws {Error} Invalid options
* @returns A sharp instance that can be used to chain operations
*/
jxl(options?: JxlOptions): Sharp;
/**
* Use these PNG options for output image.
@@ -660,7 +674,9 @@ declare namespace sharp {
| WebpOptions
| AvifOptions
| HeifOptions
| JxlOptions
| GifOptions
| Jp2Options
| TiffOptions,
): Sharp;
@@ -769,8 +785,8 @@ declare namespace sharp {
interface SharpOptions {
/**
* Level of sensitivity to invalid images, one of (in order of sensitivity):
* 'none' (least), 'truncated', 'error' or 'warning' (most), highers level imply lower levels. (optional, default 'warning')
* When to abort processing of invalid pixel data, one of (in order of sensitivity):
* 'none' (least), 'truncated', 'error' or 'warning' (most), highers level imply lower levels, invalid metadata will always abort. (optional, default 'warning')
*/
failOn?: FailOnOptions | undefined;
/**
@@ -791,7 +807,7 @@ declare namespace sharp {
unlimited?: boolean | undefined;
/** Set this to true to use sequential rather than random access where possible. This can reduce memory usage and might improve performance on some systems. (optional, default false) */
sequentialRead?: boolean | undefined;
/** Number representing the DPI for vector images. (optional, default 72) */
/** Number representing the DPI for vector images in the range 1 to 100000. (optional, default 72) */
density?: number | undefined;
/** Number of pages to extract for multi-page input (GIF, TIFF, PDF), use -1 for all pages */
pages?: number | undefined;
@@ -804,7 +820,7 @@ declare namespace sharp {
/** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default false) */
animated?: boolean | undefined;
/** Describes raw pixel input image data. See raw() for pixel ordering. */
raw?: Raw | undefined;
raw?: CreateRaw | undefined;
/** Describes a new image to be created. */
create?: Create | undefined;
/** Describes a new text image to be created. */
@@ -838,6 +854,11 @@ declare namespace sharp {
channels: 1 | 2 | 3 | 4;
}
interface CreateRaw extends Raw {
/** Specifies that the raw input has already been premultiplied, set to true to avoid sharp premultiplying the image. (optional, default false) */
premultiplied?: boolean | undefined;
}
interface Create {
/** Number of pixels wide. */
width: number;
@@ -866,7 +887,7 @@ declare namespace sharp {
*/
height?: number;
/** Text alignment ('left', 'centre', 'center', 'right'). (optional, default 'left') */
align?: string;
align?: TextAlign;
/** Set this to true to apply justification to the text. (optional, default `false`) */
justify?: boolean;
/** The resolution (size) at which to render the text. Does not take effect if `height` is specified. (optional, default `72`) */
@@ -942,6 +963,17 @@ declare namespace sharp {
compression?: 'av1' | 'hevc';
/** Default background colour, if present, for PNG (bKGD) and GIF images, either an RGB Object or a single greyscale value */
background?: { r: number; g: number; b: number } | number;
/** Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide */
levels?: LevelMetadata[] | undefined;
/** Number of Sub Image File Directories in an OME-TIFF image */
subifds?: number | undefined;
/** The unit of resolution (density) */
resolutionUnit?: 'inch' | 'cm' | undefined;
}
interface LevelMetadata {
width: number;
height: number;
}
interface Stats {
@@ -1025,6 +1057,19 @@ declare namespace sharp {
chromaSubsampling?: '4:4:4' | '4:2:0';
}
interface JxlOptions extends OutputOptions {
/** Maximum encoding error, between 0 (highest quality) and 15 (lowest quality) (optional, default 1.0) */
distance?: number;
/** Calculate distance based on JPEG-like quality, between 1 and 100, overrides distance if specified */
quality?: number;
/** Target decode speed tier, between 0 (highest quality) and 4 (lowest quality) (optional, default 0) */
decodingTier?: number;
/** Use lossless compression (optional, default false) */
lossless?: boolean;
/** CPU effort, between 3 (fastest) and 9 (slowest) (optional, default 7) */
effort?: number | undefined;
}
interface WebpOptions extends OutputOptions, AnimationOptions {
/** Quality, integer 1-100 (optional, default 80) */
quality?: number | undefined;
@@ -1064,6 +1109,8 @@ declare namespace sharp {
lossless?: boolean | undefined;
/** Level of CPU effort to reduce file size, between 0 (fastest) and 9 (slowest) (optional, default 4) */
effort?: number | undefined;
/** set to '4:2:0' to use chroma subsampling (optional, default '4:4:4') */
chromaSubsampling?: string | undefined;
}
interface GifOptions extends OutputOptions, AnimationOptions {
@@ -1079,6 +1126,10 @@ declare namespace sharp {
effort?: number | undefined;
/** Level of Floyd-Steinberg error diffusion, between 0 (least) and 1 (most) (optional, default 1.0) */
dither?: number | undefined;
/** Maximum inter-frame error for transparency, between 0 (lossless) and 32 (optional, default 0) */
interFrameMaxError?: number;
/** Maximum inter-palette error for palette reuse, between 0 and 256 (optional, default 3) */
interPaletteMaxError?: number;
}
interface TiffOptions extends OutputOptions {
@@ -1143,9 +1194,9 @@ declare namespace sharp {
}
interface ResizeOptions {
/** Alternative means of specifying width. If both are present this take priority. */
/** Alternative means of specifying width. If both are present this takes priority. */
width?: number | undefined;
/** Alternative means of specifying height. If both are present this take priority. */
/** Alternative means of specifying height. If both are present this takes priority. */
height?: number | undefined;
/** How the image should be resized to fit both provided dimensions, one of cover, contain, fill, inside or outside. (optional, default 'cover') */
fit?: keyof FitEnum | undefined;
@@ -1237,7 +1288,7 @@ declare namespace sharp {
width: number;
/** height of the region */
height: number;
/** max slope of the cumulative contrast. (optional, default 3) */
/** max slope of the cumulative contrast. A value of 0 disables contrast limiting. Valid values are integers in the range 0-100 (inclusive) (optional, default 3) */
maxSlope?: number | undefined;
}
@@ -1267,6 +1318,13 @@ declare namespace sharp {
raw?: Raw | undefined;
/** Set to true to avoid premultipling the image below. Equivalent to the --premultiplied vips option. */
premultiplied?: boolean | undefined;
/** Set to true to read all frames/pages of an animated image. (optional, default false). */
animated?: boolean | undefined;
/**
* When to abort processing of invalid pixel data, one of (in order of sensitivity):
* 'none' (least), 'truncated', 'error' or 'warning' (most), highers level imply lower levels, invalid metadata will always abort. (optional, default 'warning')
*/
failOn?: FailOnOptions | undefined;
/**
* Do not process input images where the number of pixels (width x height) exceeds this limit.
* Assumes image dimensions contained in the input metadata can be trusted.
@@ -1289,7 +1347,7 @@ declare namespace sharp {
/** Threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images */
skipBlanks?: number | undefined;
/** Tile container, with value fs (filesystem) or zip (compressed file). (optional, default 'fs') */
container?: string | undefined;
container?: TileContainer | undefined;
/** Filesystem layout, possible values are dz, iiif, iiif3, zoomify or google. (optional, default 'dz') */
layout?: TileLayout | undefined;
/** Centre image in tile. (optional, default false) */
@@ -1298,6 +1356,8 @@ declare namespace sharp {
center?: boolean | undefined;
/** When layout is iiif/iiif3, sets the @id/id attribute of info.json (optional, default 'https://example.com/iiif') */
id?: string | undefined;
/** The name of the directory within the zip file when container is `zip`. */
basename?: string | undefined;
}
interface AnimationOptions {
@@ -1308,17 +1368,17 @@ declare namespace sharp {
}
interface SharpenOptions {
/** The sigma of the Gaussian mask, where sigma = 1 + radius / 2. */
/** The sigma of the Gaussian mask, where sigma = 1 + radius / 2, between 0.000001 and 10000 */
sigma: number;
/** The level of sharpening to apply to "flat" areas. (optional, default 1.0) */
/** The level of sharpening to apply to "flat" areas, between 0 and 1000000 (optional, default 1.0) */
m1?: number | undefined;
/** The level of sharpening to apply to "jagged" areas. (optional, default 2.0) */
/** The level of sharpening to apply to "jagged" areas, between 0 and 1000000 (optional, default 2.0) */
m2?: number | undefined;
/** Threshold between "flat" and "jagged" (optional, default 2.0) */
/** Threshold between "flat" and "jagged", between 0 and 1000000 (optional, default 2.0) */
x1?: number | undefined;
/** Maximum amount of brightening. (optional, default 10.0) */
/** Maximum amount of brightening, between 0 and 1000000 (optional, default 10.0) */
y2?: number | undefined;
/** Maximum amount of darkening. (optional, default 20.0) */
/** Maximum amount of darkening, between 0 and 1000000 (optional, default 20.0) */
y3?: number | undefined;
}
@@ -1395,6 +1455,10 @@ declare namespace sharp {
type FailOnOptions = 'none' | 'truncated' | 'error' | 'warning';
type TextAlign = 'left' | 'centre' | 'center' | 'right';
type TileContainer = 'fs' | 'zip';
type TileLayout = 'dz' | 'iiif' | 'iiif3' | 'zoomify' | 'google';
type Blend =
@@ -1455,6 +1519,8 @@ declare namespace sharp {
input: AvailableFormatInfo;
jpeg: AvailableFormatInfo;
jpg: AvailableFormatInfo;
jp2: AvailableFormatInfo;
jxl: AvailableFormatInfo;
magick: AvailableFormatInfo;
openslide: AvailableFormatInfo;
pdf: AvailableFormatInfo;

View File

@@ -27,7 +27,7 @@ sharp('input.png')
.rotate(180)
.resize(300)
.flatten({ background: '#ff6600' })
.composite([{ input: 'overlay.png', gravity: sharp.gravity.southeast }])
.composite([{ input: 'overlay.png', gravity: sharp.gravity.southeast, animated: false, failOn: 'warning' }])
.sharpen()
.withMetadata()
.withMetadata({
@@ -342,7 +342,7 @@ sharp(input)
.avif({ quality: 50, lossless: false, effort: 5, chromaSubsampling: '4:2:0' })
.heif()
.heif({})
.heif({ quality: 50, compression: 'hevc', lossless: false, effort: 5 })
.heif({ quality: 50, compression: 'hevc', lossless: false, effort: 5, chromaSubsampling: '4:2:0' })
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
console.log(data);
@@ -404,6 +404,21 @@ sharp({
.toBuffer()
.then(largeImage => sharp(input).composite([{ input: largeImage, limitInputPixels: false }]));
// From https://github.com/lovell/sharp/pull/2685
// Accept premultiplied raw inputs
sharp('input.png', {
raw: {
width: 25000,
height: 25000,
channels: 4,
premultiplied: true,
},
})
.raw()
.toBuffer(err => {
if (err) throw err;
});
// Taken from API documentation at
// https://sharp.pixelplumbing.com/api-operation#clahe
// introducted
@@ -448,6 +463,15 @@ sharp(input)
.toColourspace('srgb') // this is the default, but included here for clarity
.toBuffer();
// From https://github.com/lovell/sharp/pull/1439
// Second parameter to gamma operation for different output gamma
sharp(input)
.resize(129, 111)
.gamma(2.2, 3.0)
.toBuffer(err => {
if (err) throw err;
});
// Support for raw depth specification
sharp('16bpc.png')
.toColourspace('rgb16')
@@ -490,6 +514,12 @@ sharp('input.gif').gif({ colours: 16 }).toFile('out.gif');
sharp('input.gif').gif({ dither: 0.5 }).toFile('out.gif');
sharp('input.gif').png({ dither: 0.5 }).toFile('out.png');
// Support for `interFrameMaxError` for gif output
sharp('input.gif').gif({ interFrameMaxError: 0 }).toFile('out.gif');
// Support for `interPaletteMaxError` for gif output
sharp('input.gif').gif({ interPaletteMaxError: 0 }).toFile('out.gif');
// Support for `resolutionUnit` for tiff output
sharp('input.tiff').tiff({ resolutionUnit: 'cm' }).toFile('out.tiff');
@@ -500,6 +530,14 @@ sharp('input.tiff').jp2({ lossless: true }).toFile('out.jp2');
sharp('input.tiff').jp2({ tileWidth: 128, tileHeight: 128 }).toFile('out.jp2');
sharp('input.tiff').jp2({ chromaSubsampling: '4:2:0' }).toFile('out.jp2');
// Support for `jxl` output with different options
sharp('input.tiff').jxl().toFile('out.jxl');
sharp('input.tiff').jxl({ distance: 15.0 }).toFile('out.jxl');
sharp('input.tiff').jxl({ quality: 50 }).toFile('out.jxl');
sharp('input.tiff').jxl({ decodingTier: 4 }).toFile('out.jxl');
sharp('input.tiff').jxl({ lossless: true }).toFile('out.jxl');
sharp('input.tiff').jxl({ effort: 7 }).toFile('out.jxl');
// Support `minSize` and `mixed` webp options
sharp('input.tiff').webp({ minSize: 1000, mixed: true }).toFile('out.gif');
@@ -587,3 +625,29 @@ sharp('input.png').composite([
},
},
]);
// From https://github.com/lovell/sharp/pull/1835
sharp('input.png').composite([
{
input: {
text: {
text: 'Okay then',
font: 'Comic Sans',
},
},
blend: 'color-burn',
top: 0,
left: 0,
premultiplied: true,
},
]);
// https://github.com/lovell/sharp/pull/402
(['fs', 'zip'] as const).forEach(container => {
sharp().tile({ container });
});
// From https://github.com/lovell/sharp/issues/2238
sharp('input.png').tile({
basename: 'output.dz.tiles',
});