Skip to content

Commit

Permalink
Ensure precision of trim threshold, update docs #914
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Oct 2, 2018
1 parent 21fbe54 commit 60438eb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/api-resize.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ Returns **Sharp**

## trim

Trim "boring" pixels from all edges that contain values within a percentage similarity of the top-left pixel.
Trim "boring" pixels from all edges that contain values similar to the top-left pixel.

### Parameters

- `tolerance` **[Number][7]** value between 1 and 99 representing the percentage similarity. (optional, default `10`)
- `threshold` **[Number][7]** the allowed difference from the top-left pixel, a number greater than zero. (optional, default `10`)


- Throws **[Error][8]** Invalid parameters
Expand Down
7 changes: 4 additions & 3 deletions src/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,16 @@ namespace sharp {
/*
Trim an image
*/
VImage Trim(VImage image, int const threshold) {
VImage Trim(VImage image, double const threshold) {
// Top-left pixel provides the background colour
VImage background = image.extract_area(0, 0, 1, 1);
if (HasAlpha(background)) {
background = background.flatten();
}
int top, width, height;
int const left = image.find_trim(&top, &width, &height,
VImage::option()->set("background", background(0, 0)));
int const left = image.find_trim(&top, &width, &height, VImage::option()
->set("background", background(0, 0))
->set("threshold", threshold));
if (width == 0 || height == 0) {
throw VError("Unexpected error while trimming. Try to lower the tolerance");
}
Expand Down
2 changes: 1 addition & 1 deletion src/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace sharp {
/*
Trim an image
*/
VImage Trim(VImage image, int const threshold);
VImage Trim(VImage image, double const threshold);

/*
* Linear adjustment (a * in + b)
Expand Down
1 change: 0 additions & 1 deletion test/unit/trim.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('Trim borders', function () {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(300, info.width);
assert.strictEqual(300, info.height);
fixtures.assertSimilar(expected, data, done);
});
});
Expand Down

0 comments on commit 60438eb

Please sign in to comment.