Skip to content

Commit

Permalink
Merge 721242d into 361ed98
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesBos committed May 30, 2017
2 parents 361ed98 + 721242d commit e97e08d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/constructor.js
Expand Up @@ -180,6 +180,8 @@ const Sharp = function (input, options) {
tiffCompression: 'jpeg',
tiffPredictor: 'none',
tiffSquash: false,
tiffXres: 10.0,
tiffYres: 10.0,
tileSize: 256,
tileOverlap: 0,
// Function to notify of libvips warnings
Expand Down
17 changes: 17 additions & 0 deletions lib/output.js
Expand Up @@ -214,6 +214,8 @@ function webp (options) {
* @param {Boolean} [options.force=true] - force TIFF output, otherwise attempt to use input format
* @param {Boolean} [options.compression='jpeg'] - compression options: lzw, deflate, jpeg
* @param {Boolean} [options.predictor='none'] - compression predictor options: none, horizontal, float
* @param {Number} [options.xres=10.0] - horizontal resolution in pixels/mm
* @param {Number} [options.yres=10.0] - vertical resolution in pixels/mm
* @param {Boolean} [options.squash=false] - squash 8-bit images down to 1 bit
* @returns {Sharp}
* @throws {Error} Invalid options
Expand All @@ -233,6 +235,21 @@ function tiff (options) {
throw new Error('Invalid Value for squash ' + options.squash + ' Only Boolean Values allowed for options.squash.');
}
}
// resolution
if (is.object(options) && is.defined(options.xres)) {
if (is.number(options.xres)) {
this.options.tiffXres = options.xres;
} else {
throw new Error('Invalid Value for xres ' + options.xres + ' Only numeric values allowed for options.xres');
}
}
if (is.object(options) && is.defined(options.yres)) {
if (is.number(options.yres)) {
this.options.tiffYres = options.yres;
} else {
throw new Error('Invalid Value for yres ' + options.yres + ' Only numeric values allowed for options.yres');
}
}
// compression
if (is.defined(options) && is.defined(options.compression)) {
if (is.string(options.compression) && is.inArray(options.compression, ['lzw', 'deflate', 'jpeg', 'none'])) {
Expand Down
6 changes: 5 additions & 1 deletion src/pipeline.cc
Expand Up @@ -904,7 +904,9 @@ class PipelineWorker : public Nan::AsyncWorker {
->set("Q", baton->tiffQuality)
->set("squash", baton->tiffSquash)
->set("compression", baton->tiffCompression)
->set("predictor", baton->tiffPredictor));
->set("predictor", baton->tiffPredictor)
->set("xres", baton->tiffXres)
->set("yres", baton->tiffYres));
baton->formatOut = "tiff";
baton->channels = std::min(baton->channels, 3);
} else if (baton->formatOut == "dz" || isDz || isDzZip) {
Expand Down Expand Up @@ -1277,6 +1279,8 @@ NAN_METHOD(pipeline) {
baton->webpNearLossless = AttrTo<bool>(options, "webpNearLossless");
baton->tiffQuality = AttrTo<uint32_t>(options, "tiffQuality");
baton->tiffSquash = AttrTo<bool>(options, "tiffSquash");
baton->tiffXres = AttrTo<double>(options, "tiffXres");
baton->tiffYres = AttrTo<double>(options, "tiffYres");
// tiff compression options
baton->tiffCompression = static_cast<VipsForeignTiffCompression>(
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_COMPRESSION,
Expand Down
4 changes: 4 additions & 0 deletions src/pipeline.h
Expand Up @@ -109,6 +109,8 @@ struct PipelineBaton {
VipsForeignTiffCompression tiffCompression;
VipsForeignTiffPredictor tiffPredictor;
bool tiffSquash;
double tiffXres;
double tiffYres;
std::string err;
bool withMetadata;
int withMetadataOrientation;
Expand Down Expand Up @@ -182,6 +184,8 @@ struct PipelineBaton {
tiffCompression(VIPS_FOREIGN_TIFF_COMPRESSION_JPEG),
tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_NONE),
tiffSquash(false),
tiffXres(10.0),
tiffYres(10.0),
withMetadata(false),
withMetadataOrientation(-1),
convKernelWidth(0),
Expand Down
40 changes: 40 additions & 0 deletions test/unit/io.js
Expand Up @@ -933,6 +933,46 @@ describe('Input/output', function () {
});
});

it('TIFF setting xres and yres', function (done) {
const res = 300.0;
const image = sharp(fixtures.inputTiff8BitDepth)
.tiff({
xres: (res),
yres: (res)
})
.toFile(fixtures.outputTiff, (err, info) => {
if (err) throw err;
assert.strictEqual('tiff', info.format);
assert.strictEqual(image.options.tiffXres, res);
assert.strictEqual(image.options.tiffYres, res);
fs.unlink(fixtures.outputTiff, done);
});
});

it('TIFF not setting xres and yres, default value should be retained', function (done) {
const image = sharp(fixtures.inputTiff8BitDepth)
.tiff()
.toFile(fixtures.outputTiff, (err, info) => {
if (err) throw err;
assert.strictEqual('tiff', info.format);
assert.strictEqual(image.options.tiffXres, 10.0); // 10.0 is the default value
assert.strictEqual(image.options.tiffYres, 10.0); // 10.0 is the default value
fs.unlink(fixtures.outputTiff, done);
});
});

it('TIFF invalid xres value should throw an error', function () {
assert.throws(function () {
sharp().tiff({ xres: '300.0' });
});
});

it('TIFF invalid yres value should throw an error', function () {
assert.throws(function () {
sharp().tiff({ yres: '300.0' });
});
});

it('TIFF lzw compression with horizontal predictor shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed)
Expand Down

0 comments on commit e97e08d

Please sign in to comment.