Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added resolution unit option in tiff method #3023

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ const Sharp = function (input, options) {
tiffTileWidth: 256,
tiffXres: 1.0,
tiffYres: 1.0,
tiffResolutionUnit: 'inch',
heifQuality: 50,
heifLossless: false,
heifCompression: 'av1',
Expand Down
9 changes: 9 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,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 {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit
* @param {string} [options.resolutionUnit='inch'] - resolutionUnit options: inch, cm
* @returns {Sharp}
* @throws {Error} Invalid options
*/
Expand Down Expand Up @@ -733,6 +734,14 @@ function tiff (options) {
throw is.invalidParameterError('predictor', 'one of: none, horizontal, float', options.predictor);
}
}
// resolutionUnit
if (is.defined(options.resolutionUnit)) {
if (is.string(options.resolutionUnit) && is.inArray(options.resolutionUnit, ['inch', 'cm'])) {
this.options.tiffResolutionUnit = options.resolutionUnit;
} else {
throw is.invalidParameterError('resolutionUnit', 'one of: inch, cm', options.resolutionUnit);
}
}
}
return this._updateFormatOut('tiff', options);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"Michael Nutt <[email protected]>",
"Brad Parham <[email protected]>",
"Taneli Vatanen <[email protected]>",
"Joris Dugué <[email protected]>"
"Joris Dugué <[email protected]>",
"Ompal Singh <[email protected]>"
],
"scripts": {
"install": "(node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)",
Expand Down
12 changes: 10 additions & 2 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("tile_height", baton->tiffTileHeight)
->set("tile_width", baton->tiffTileWidth)
->set("xres", baton->tiffXres)
->set("yres", baton->tiffYres)));
->set("yres", baton->tiffYres)
->set("resunit", baton->tiffResolutionUnit)));
baton->bufferOut = static_cast<char*>(area->data);
baton->bufferOutLength = area->length;
area->free_fn = nullptr;
Expand Down Expand Up @@ -1046,7 +1047,8 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("tile_height", baton->tiffTileHeight)
->set("tile_width", baton->tiffTileWidth)
->set("xres", baton->tiffXres)
->set("yres", baton->tiffYres));
->set("yres", baton->tiffYres)
->set("resunit", baton->tiffResolutionUnit));
baton->formatOut = "tiff";
} else if (baton->formatOut == "heif" || (mightMatchInput && isHeif) ||
(willMatchInput && inputImageType == sharp::ImageType::HEIF)) {
Expand Down Expand Up @@ -1506,6 +1508,11 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->tiffPredictor = static_cast<VipsForeignTiffPredictor>(
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_PREDICTOR,
sharp::AttrAsStr(options, "tiffPredictor").data()));
// tiff resolution unit options
baton->tiffResolutionUnit = static_cast<VipsForeignTiffResunit>(
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_RESUNIT,
sharp::AttrAsStr(options, "tiffResolutionUnit").data()));

baton->heifQuality = sharp::AttrAsUint32(options, "heifQuality");
baton->heifLossless = sharp::AttrAsBool(options, "heifLossless");
baton->heifCompression = static_cast<VipsForeignHeifCompression>(
Expand All @@ -1514,6 +1521,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->heifSpeed = sharp::AttrAsUint32(options, "heifSpeed");
baton->heifChromaSubsampling = sharp::AttrAsStr(options, "heifChromaSubsampling");


// Raw output
baton->rawDepth = static_cast<VipsBandFormat>(
vips_enum_from_nick(nullptr, VIPS_TYPE_BAND_FORMAT,
Expand Down
2 changes: 2 additions & 0 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ struct PipelineBaton {
VipsForeignDzDepth tileDepth;
std::string tileId;
std::unique_ptr<double[]> recombMatrix;
VipsForeignTiffResunit tiffResolutionUnit;

PipelineBaton():
input(nullptr),
Expand Down Expand Up @@ -307,6 +308,7 @@ struct PipelineBaton {
tiffTileWidth(256),
tiffXres(1.0),
tiffYres(1.0),
tiffResolutionUnit(VIPS_FOREIGN_TIFF_RESUNIT_INCH),
heifQuality(50),
heifCompression(VIPS_FOREIGN_HEIF_COMPRESSION_AV1),
heifSpeed(5),
Expand Down
37 changes: 37 additions & 0 deletions test/unit/tiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,37 @@ describe('TIFF', function () {
});
});

it('TTIFF ccittfax4 compression with inch resolutionUnit test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiff).size;
sharp(fixtures.inputTiff)
.toColourspace('b-w')
.tiff({
bitdepth: 1,
compression: 'ccittfax4',
resolutionUnit: 'inch'
})
.toFile(outputTiff, (err, info) => {
if (err) throw err;
console.log('info', info);
assert.strictEqual('tiff', info.format);
assert(info.size < startSize);
rimraf(outputTiff, done);
});
});

it('TTIFF deflate compression without resolutionUnit test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
.tiff({
compression: 'deflate'
}).toFile(outputTiff, (err, info) => {
if (err) throw err;
assert.strictEqual('tiff', info.format);
assert(info.size < startSize);
rimraf(outputTiff, done);
});
});

it('TIFF deflate compression with horizontal predictor shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
Expand Down Expand Up @@ -383,6 +414,12 @@ describe('TIFF', function () {
});
});

it('TIFF invalid resolutionUnit option throws', function () {
assert.throws(function () {
sharp().tiff({ resolutionUnit: 'none' });
});
});

it('TIFF horizontal predictor does not throw error', function () {
assert.doesNotThrow(function () {
sharp().tiff({ predictor: 'horizontal' });
Expand Down