Skip to content

Commit

Permalink
Add trimOffsetLeft, trimOffsetTop to trim response #914
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Oct 2, 2018
1 parent c431909 commit 0725378
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api-resize.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -966,6 +968,12 @@ class PipelineWorker : public Nan::AsyncWorker {
Set(info, New("cropOffsetTop").ToLocalChecked(),
New<v8::Int32>(static_cast<int32_t>(baton->cropOffsetTop)));
}
if (baton->trimThreshold > 0.0) {
Set(info, New("trimOffsetLeft").ToLocalChecked(),
New<v8::Int32>(static_cast<int32_t>(baton->trimOffsetLeft)));
Set(info, New("trimOffsetTop").ToLocalChecked(),
New<v8::Int32>(static_cast<int32_t>(baton->trimOffsetTop)));
}

if (baton->bufferOutLength > 0) {
// Pass ownership of output data to Buffer instance
Expand Down
4 changes: 4 additions & 0 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ struct PipelineBaton {
int threshold;
bool thresholdGrayscale;
double trimThreshold;
int trimOffsetLeft;
int trimOffsetTop;
double linearA;
double linearB;
double gamma;
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions test/unit/trim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down

0 comments on commit 0725378

Please sign in to comment.