Impute TIFF xres/yres from withMetadata({density})

This commit is contained in:
Michael B. Klein 2021-10-28 14:46:45 -05:00 committed by Lovell Fuller
parent b33231d4bd
commit 342de36973
5 changed files with 28 additions and 1 deletions

View File

@ -379,6 +379,8 @@ Returns **Sharp**
Use these TIFF options for output image. Use these TIFF options for output image.
The `density` can be set in pixels/inch via [withMetadata][1] instead of providing `xres` and `yres` in pixels/mm.
### Parameters ### Parameters
* `options` **[Object][6]?** output options * `options` **[Object][6]?** output options

File diff suppressed because one or more lines are too long

View File

@ -637,6 +637,8 @@ function trySetAnimationOptions (source, target) {
/** /**
* Use these TIFF options for output image. * 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.
*
* @example * @example
* // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output * // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output
* sharp('input.svg') * sharp('input.svg')

View File

@ -1496,6 +1496,9 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->tiffTileHeight = sharp::AttrAsUint32(options, "tiffTileHeight"); baton->tiffTileHeight = sharp::AttrAsUint32(options, "tiffTileHeight");
baton->tiffXres = sharp::AttrAsDouble(options, "tiffXres"); baton->tiffXres = sharp::AttrAsDouble(options, "tiffXres");
baton->tiffYres = sharp::AttrAsDouble(options, "tiffYres"); baton->tiffYres = sharp::AttrAsDouble(options, "tiffYres");
if (baton->tiffXres == 1.0 && baton->tiffYres == 1.0 && baton->withMetadataDensity > 0) {
baton->tiffXres = baton->tiffYres = baton->withMetadataDensity / 25.4;
}
// tiff compression options // tiff compression options
baton->tiffCompression = static_cast<VipsForeignTiffCompression>( baton->tiffCompression = static_cast<VipsForeignTiffCompression>(
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_COMPRESSION, vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_COMPRESSION,

View File

@ -188,6 +188,26 @@ describe('TIFF', function () {
) )
); );
it('TIFF imputes xres and yres from withMetadataDensity if not explicitly provided', async () => {
const data = await sharp(fixtures.inputTiff)
.resize(8, 8)
.tiff()
.withMetadata({ density: 600 })
.toBuffer();
const { density } = await sharp(data).metadata();
assert.strictEqual(600, density);
});
it('TIFF uses xres and yres over withMetadataDensity if explicitly provided', async () => {
const data = await sharp(fixtures.inputTiff)
.resize(8, 8)
.tiff({ xres: 1000, yres: 1000 })
.withMetadata({ density: 600 })
.toBuffer();
const { density } = await sharp(data).metadata();
assert.strictEqual(25400, density);
});
it('TIFF invalid xres value should throw an error', function () { it('TIFF invalid xres value should throw an error', function () {
assert.throws(function () { assert.throws(function () {
sharp().tiff({ xres: '1000.0' }); sharp().tiff({ xres: '1000.0' });