From 0725378257e6752e0cfb0521a72f77dfb4b3f658 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Tue, 2 Oct 2018 20:16:00 +0100 Subject: [PATCH] Add trimOffsetLeft, trimOffsetTop to trim response #914 --- docs/api-resize.md | 1 + lib/resize.js | 1 + src/pipeline.cc | 8 ++++++++ src/pipeline.h | 4 ++++ test/unit/trim.js | 6 ++++++ 5 files changed, 20 insertions(+) diff --git a/docs/api-resize.md b/docs/api-resize.md index a33c93aeb..e6e60e39c 100644 --- a/docs/api-resize.md +++ b/docs/api-resize.md @@ -196,6 +196,7 @@ Returns **Sharp** ## trim Trim "boring" pixels from all edges that contain values similar to the top-left pixel. +The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties. ### Parameters diff --git a/lib/resize.js b/lib/resize.js index 06f190fb5..72e409bda 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -361,6 +361,7 @@ function extract (options) { /** * Trim "boring" pixels from all edges that contain values similar to the top-left pixel. + * The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties. * @param {Number} [threshold=10] the allowed difference from the top-left pixel, a number greater than zero. * @returns {Sharp} * @throws {Error} Invalid parameters diff --git a/src/pipeline.cc b/src/pipeline.cc index 6193c9cee..61bc79661 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -102,6 +102,8 @@ class PipelineWorker : public Nan::AsyncWorker { // Trim if (baton->trimThreshold > 0.0) { image = sharp::Trim(image, baton->trimThreshold); + baton->trimOffsetLeft = image.xoffset(); + baton->trimOffsetTop = image.yoffset(); } // Pre extraction @@ -966,6 +968,12 @@ class PipelineWorker : public Nan::AsyncWorker { Set(info, New("cropOffsetTop").ToLocalChecked(), New(static_cast(baton->cropOffsetTop))); } + if (baton->trimThreshold > 0.0) { + Set(info, New("trimOffsetLeft").ToLocalChecked(), + New(static_cast(baton->trimOffsetLeft))); + Set(info, New("trimOffsetTop").ToLocalChecked(), + New(static_cast(baton->trimOffsetTop))); + } if (baton->bufferOutLength > 0) { // Pass ownership of output data to Buffer instance diff --git a/src/pipeline.h b/src/pipeline.h index 8d1fa80ae..cd763a869 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -82,6 +82,8 @@ struct PipelineBaton { int threshold; bool thresholdGrayscale; double trimThreshold; + int trimOffsetLeft; + int trimOffsetTop; double linearA; double linearB; double gamma; @@ -177,6 +179,8 @@ struct PipelineBaton { threshold(0), thresholdGrayscale(true), trimThreshold(0.0), + trimOffsetLeft(0), + trimOffsetTop(0), linearA(1.0), linearB(0.0), gamma(0.0), diff --git a/test/unit/trim.js b/test/unit/trim.js index 39ba632bb..709b5f0a3 100644 --- a/test/unit/trim.js +++ b/test/unit/trim.js @@ -16,6 +16,8 @@ describe('Trim borders', function () { assert.strictEqual('png', info.format); assert.strictEqual(450, info.width); assert.strictEqual(322, info.height); + assert.strictEqual(-204, info.trimOffsetLeft); + assert.strictEqual(0, info.trimOffsetTop); fixtures.assertSimilar(expected, data, done); }); }); @@ -29,6 +31,8 @@ describe('Trim borders', function () { if (err) throw err; assert.strictEqual('jpeg', info.format); assert.strictEqual(300, info.width); + assert.strictEqual(-873, info.trimOffsetLeft); + assert.strictEqual(-554, info.trimOffsetTop); fixtures.assertSimilar(expected, data, done); }); }); @@ -44,6 +48,8 @@ describe('Trim borders', function () { assert.strictEqual(32, info.width); assert.strictEqual(32, info.height); assert.strictEqual(4, info.channels); + assert.strictEqual(-2, info.trimOffsetLeft); + assert.strictEqual(-2, info.trimOffsetTop); fixtures.assertSimilar(fixtures.expected('trim-16bit-rgba.png'), data, done); }); });