Skip to content

Commit

Permalink
Impute TIFF xres/yres from withMetadata({density})
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein authored and lovell committed Nov 8, 2021
1 parent b33231d commit 342de36
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/api-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ Returns **Sharp**

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

* `options` **[Object][6]?** output options
Expand Down
2 changes: 1 addition & 1 deletion docs/search-index.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ function trySetAnimationOptions (source, target) {
/**
* 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
* // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output
* sharp('input.svg')
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,9 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->tiffTileHeight = sharp::AttrAsUint32(options, "tiffTileHeight");
baton->tiffXres = sharp::AttrAsDouble(options, "tiffXres");
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
baton->tiffCompression = static_cast<VipsForeignTiffCompression>(
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_COMPRESSION,
Expand Down
20 changes: 20 additions & 0 deletions test/unit/tiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
assert.throws(function () {
sharp().tiff({ xres: '1000.0' });
Expand Down

0 comments on commit 342de36

Please sign in to comment.