mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 15:25:07 +01:00
Add experimental support for JPEG-XL, requires libvips with libjxl
The prebuilt binaries do not include support for this format.
This commit is contained in:
@@ -308,6 +308,10 @@ const Sharp = function (input, options) {
|
||||
heifCompression: 'av1',
|
||||
heifEffort: 4,
|
||||
heifChromaSubsampling: '4:4:4',
|
||||
jxlDistance: 1,
|
||||
jxlDecodingTier: 0,
|
||||
jxlEffort: 7,
|
||||
jxlLossless: false,
|
||||
rawDepth: 'uchar',
|
||||
tileSize: 256,
|
||||
tileOverlap: 0,
|
||||
|
||||
@@ -22,7 +22,8 @@ const formats = new Map([
|
||||
['jp2', 'jp2'],
|
||||
['jpx', 'jp2'],
|
||||
['j2k', 'jp2'],
|
||||
['j2c', 'jp2']
|
||||
['j2c', 'jp2'],
|
||||
['jxl', 'jxl']
|
||||
]);
|
||||
|
||||
const jp2Regex = /\.jp[2x]|j2[kc]$/i;
|
||||
@@ -938,6 +939,71 @@ function heif (options) {
|
||||
return this._updateFormatOut('heif', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these JPEG-XL (JXL) options for output image.
|
||||
*
|
||||
* This feature is experimental, please do not use in production systems.
|
||||
*
|
||||
* Requires libvips compiled with support for libjxl.
|
||||
* The prebuilt binaries do not include this - see
|
||||
* {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
|
||||
*
|
||||
* Image metadata (EXIF, XMP) is unsupported.
|
||||
*
|
||||
* @since 0.31.3
|
||||
*
|
||||
* @param {Object} [options] - output options
|
||||
* @param {number} [options.distance=1.0] - maximum encoding error, between 0 (highest quality) and 15 (lowest quality)
|
||||
* @param {number} [options.quality] - calculate `distance` based on JPEG-like quality, between 1 and 100, overrides distance if specified
|
||||
* @param {number} [options.decodingTier=0] - target decode speed tier, between 0 (highest quality) and 4 (lowest quality)
|
||||
* @param {boolean} [options.lossless=false] - use lossless compression
|
||||
* @param {number} [options.effort=7] - CPU effort, between 3 (fastest) and 9 (slowest)
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
function jxl (options) {
|
||||
if (is.object(options)) {
|
||||
if (is.defined(options.quality)) {
|
||||
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
|
||||
// https://github.com/libjxl/libjxl/blob/0aeea7f180bafd6893c1db8072dcb67d2aa5b03d/tools/cjxl_main.cc#L640-L644
|
||||
this.options.jxlDistance = options.quality >= 30
|
||||
? 0.1 + (100 - options.quality) * 0.09
|
||||
: 53 / 3000 * options.quality * options.quality - 23 / 20 * options.quality + 25;
|
||||
} else {
|
||||
throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
|
||||
}
|
||||
} else if (is.defined(options.distance)) {
|
||||
if (is.number(options.distance) && is.inRange(options.distance, 0, 15)) {
|
||||
this.options.jxlDistance = options.distance;
|
||||
} else {
|
||||
throw is.invalidParameterError('distance', 'number between 0.0 and 15.0', options.distance);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.decodingTier)) {
|
||||
if (is.integer(options.decodingTier) && is.inRange(options.decodingTier, 0, 4)) {
|
||||
this.options.jxlDecodingTier = options.decodingTier;
|
||||
} else {
|
||||
throw is.invalidParameterError('decodingTier', 'integer between 0 and 4', options.decodingTier);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.lossless)) {
|
||||
if (is.bool(options.lossless)) {
|
||||
this.options.jxlLossless = options.lossless;
|
||||
} else {
|
||||
throw is.invalidParameterError('lossless', 'boolean', options.lossless);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.effort)) {
|
||||
if (is.integer(options.effort) && is.inRange(options.effort, 3, 9)) {
|
||||
this.options.jxlEffort = options.effort;
|
||||
} else {
|
||||
throw is.invalidParameterError('effort', 'integer between 3 and 9', options.effort);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._updateFormatOut('jxl', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force output to be raw, uncompressed pixel data.
|
||||
* Pixel ordering is left-to-right, top-to-bottom, without padding.
|
||||
@@ -1308,6 +1374,7 @@ module.exports = function (Sharp) {
|
||||
tiff,
|
||||
avif,
|
||||
heif,
|
||||
jxl,
|
||||
gif,
|
||||
raw,
|
||||
tile,
|
||||
|
||||
Reference in New Issue
Block a user