From ed6b7384d071192a0b87debd381539d7e98c24f0 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Fri, 23 Jan 2026 21:29:40 +0000 Subject: [PATCH] Ensure TIFF output bitdepth option is limited to 1, 2 or 4 --- docs/src/content/docs/api-output.md | 2 +- docs/src/content/docs/changelog/v0.35.0.md | 2 ++ lib/constructor.js | 2 +- lib/index.d.ts | 4 ++-- lib/output.js | 6 +++--- src/pipeline.h | 2 +- test/unit/tiff.js | 3 +-- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/src/content/docs/api-output.md b/docs/src/content/docs/api-output.md index b6729c63..25ad70c6 100644 --- a/docs/src/content/docs/api-output.md +++ b/docs/src/content/docs/api-output.md @@ -717,7 +717,7 @@ instead of providing `xres` and `yres` in pixels/mm. | [options.xres] | number | 1.0 | horizontal resolution in pixels/mm | | [options.yres] | number | 1.0 | vertical resolution in pixels/mm | | [options.resolutionUnit] | string | "'inch'" | resolution unit options: inch, cm | -| [options.bitdepth] | number | 8 | reduce bitdepth to 1, 2 or 4 bit | +| [options.bitdepth] | number | 0 | reduce bitdepth to 1, 2 or 4 bit | | [options.miniswhite] | boolean | false | write 1-bit images as miniswhite | **Example** diff --git a/docs/src/content/docs/changelog/v0.35.0.md b/docs/src/content/docs/changelog/v0.35.0.md index 9f97d0e3..f047aab4 100644 --- a/docs/src/content/docs/changelog/v0.35.0.md +++ b/docs/src/content/docs/changelog/v0.35.0.md @@ -20,6 +20,8 @@ slug: changelog/v0.35.0 * Upgrade to libvips v8.18.0 for upstream bug fixes. +* Ensure TIFF output `bitdepth` option is limited to 1, 2 or 4. + * Deprecate Windows 32-bit (win32-ia32) prebuilt binaries. * Add AVIF/HEIF `tune` option for control over quality metrics. diff --git a/lib/constructor.js b/lib/constructor.js index 1a2e55be..149d0b34 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -366,7 +366,7 @@ const Sharp = function (input, options) { tiffPredictor: 'horizontal', tiffPyramid: false, tiffMiniswhite: false, - tiffBitdepth: 8, + tiffBitdepth: 0, tiffTile: false, tiffTileHeight: 256, tiffTileWidth: 256, diff --git a/lib/index.d.ts b/lib/index.d.ts index fd36ecf9..b924ecf8 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1479,8 +1479,8 @@ declare namespace sharp { xres?: number | undefined; /** Vertical resolution in pixels/mm (optional, default 1.0) */ yres?: number | undefined; - /** Reduce bitdepth to 1, 2 or 4 bit (optional, default 8) */ - bitdepth?: 1 | 2 | 4 | 8 | undefined; + /** Reduce bitdepth to 1, 2 or 4 bit (optional) */ + bitdepth?: 1 | 2 | 4 | undefined; /** Write 1-bit images as miniswhite (optional, default false) */ miniswhite?: boolean | undefined; /** Resolution unit options: inch, cm (optional, default 'inch') */ diff --git a/lib/output.js b/lib/output.js index d0e1d3d5..b0213f5e 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1055,7 +1055,7 @@ function trySetAnimationOptions (source, target) { * @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm * @param {number} [options.yres=1.0] - vertical resolution in pixels/mm * @param {string} [options.resolutionUnit='inch'] - resolution unit options: inch, cm - * @param {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit + * @param {number} [options.bitdepth=0] - reduce bitdepth to 1, 2 or 4 bit * @param {boolean} [options.miniswhite=false] - write 1-bit images as miniswhite * @returns {Sharp} * @throws {Error} Invalid options @@ -1070,10 +1070,10 @@ function tiff (options) { } } if (is.defined(options.bitdepth)) { - if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [1, 2, 4, 8])) { + if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [1, 2, 4])) { this.options.tiffBitdepth = options.bitdepth; } else { - throw is.invalidParameterError('bitdepth', '1, 2, 4 or 8', options.bitdepth); + throw is.invalidParameterError('bitdepth', '1, 2 or 4', options.bitdepth); } } // tiling diff --git a/src/pipeline.h b/src/pipeline.h index a007f7a8..dfd50393 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -365,7 +365,7 @@ struct PipelineBaton { tiffBigtiff(false), tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL), tiffPyramid(false), - tiffBitdepth(8), + tiffBitdepth(0), tiffMiniswhite(false), tiffTile(false), tiffTileHeight(256), diff --git a/test/unit/tiff.js b/test/unit/tiff.js index be5e5535..21c46ace 100644 --- a/test/unit/tiff.js +++ b/test/unit/tiff.js @@ -122,7 +122,6 @@ describe('TIFF', () => { sharp(fixtures.inputTiff8BitDepth) .toColourspace('b-w') // can only squash 1 band uchar images .tiff({ - bitdepth: 8, compression: 'none', predictor: 'none' }) @@ -154,7 +153,7 @@ describe('TIFF', () => { it('Invalid TIFF bitdepth value throws error', () => { assert.throws(() => { sharp().tiff({ bitdepth: 3 }); - }, /Error: Expected 1, 2, 4 or 8 for bitdepth but received 3 of type number/); + }, /Error: Expected 1, 2 or 4 for bitdepth but received 3 of type number/); }); it('TIFF setting xres and yres on file', () =>