mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Ensure trim op supports image-in-alpha #1597
This commit is contained in:
parent
833aaead56
commit
c41b87303d
@ -196,6 +196,8 @@ Returns **Sharp**
|
|||||||
## trim
|
## trim
|
||||||
|
|
||||||
Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
||||||
|
Images consisting entirely of a single colour will calculate "boring" using the alpha channel, if any.
|
||||||
|
|
||||||
The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
||||||
|
|
||||||
### Parameters
|
### Parameters
|
||||||
|
@ -6,6 +6,9 @@ Requires libvips v8.8.1.
|
|||||||
|
|
||||||
#### v0.23.3 - TBD
|
#### v0.23.3 - TBD
|
||||||
|
|
||||||
|
* Ensure `trim` operation supports images contained in the alpha channel.
|
||||||
|
[#1597](https://github.com/lovell/sharp/issues/1597)
|
||||||
|
|
||||||
* Ensure tile `overlap` option works as expected.
|
* Ensure tile `overlap` option works as expected.
|
||||||
[#1921](https://github.com/lovell/sharp/pull/1921)
|
[#1921](https://github.com/lovell/sharp/pull/1921)
|
||||||
[@rustyguts](https://github.com/rustyguts)
|
[@rustyguts](https://github.com/rustyguts)
|
||||||
|
@ -367,7 +367,10 @@ function extract (options) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
* Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
||||||
|
* Images consisting entirely of a single colour will calculate "boring" using the alpha channel, if any.
|
||||||
|
*
|
||||||
* The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
* 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.
|
* @param {Number} [threshold=10] the allowed difference from the top-left pixel, a number greater than zero.
|
||||||
* @returns {Sharp}
|
* @returns {Sharp}
|
||||||
* @throws {Error} Invalid parameters
|
* @throws {Error} Invalid parameters
|
||||||
|
@ -266,13 +266,23 @@ namespace sharp {
|
|||||||
if (HasAlpha(background)) {
|
if (HasAlpha(background)) {
|
||||||
background = background.flatten();
|
background = background.flatten();
|
||||||
}
|
}
|
||||||
int top, width, height;
|
int left, top, width, height;
|
||||||
int const left = image.find_trim(&top, &width, &height, VImage::option()
|
left = image.find_trim(&top, &width, &height, VImage::option()
|
||||||
->set("background", background(0, 0))
|
->set("background", background(0, 0))
|
||||||
->set("threshold", threshold));
|
->set("threshold", threshold));
|
||||||
|
if (width == 0 || height == 0) {
|
||||||
|
if (HasAlpha(image)) {
|
||||||
|
// Search alpha channel
|
||||||
|
VImage alpha = image[image.bands() - 1];
|
||||||
|
VImage backgroundAlpha = alpha.extract_area(0, 0, 1, 1);
|
||||||
|
left = alpha.find_trim(&top, &width, &height, VImage::option()
|
||||||
|
->set("background", backgroundAlpha(0, 0))
|
||||||
|
->set("threshold", threshold));
|
||||||
|
}
|
||||||
if (width == 0 || height == 0) {
|
if (width == 0 || height == 0) {
|
||||||
throw VError("Unexpected error while trimming. Try to lower the tolerance");
|
throw VError("Unexpected error while trimming. Try to lower the tolerance");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return image.extract_area(left, top, width, height);
|
return image.extract_area(left, top, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
test/fixtures/image-in-alpha.png
vendored
Normal file
BIN
test/fixtures/image-in-alpha.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 70 KiB |
1
test/fixtures/index.js
vendored
1
test/fixtures/index.js
vendored
@ -87,6 +87,7 @@ module.exports = {
|
|||||||
inputPngTruncated: getPath('truncated.png'), // gm convert 2569067123_aca715a2ee_o.jpg -resize 320x240 saw.png ; head -c 10000 saw.png > truncated.png
|
inputPngTruncated: getPath('truncated.png'), // gm convert 2569067123_aca715a2ee_o.jpg -resize 320x240 saw.png ; head -c 10000 saw.png > truncated.png
|
||||||
inputPngEmbed: getPath('embedgravitybird.png'), // Released to sharp under a CC BY 4.0
|
inputPngEmbed: getPath('embedgravitybird.png'), // Released to sharp under a CC BY 4.0
|
||||||
inputPngRGBWithAlpha: getPath('2569067123_aca715a2ee_o.png'), // http://www.flickr.com/photos/grizdave/2569067123/ (same as inputJpg)
|
inputPngRGBWithAlpha: getPath('2569067123_aca715a2ee_o.png'), // http://www.flickr.com/photos/grizdave/2569067123/ (same as inputJpg)
|
||||||
|
inputPngImageInAlpha: getPath('image-in-alpha.png'), // https://github.com/lovell/sharp/issues/1597
|
||||||
|
|
||||||
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp
|
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp
|
||||||
inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
||||||
|
@ -41,6 +41,21 @@ describe('Trim borders', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('single colour PNG where alpha channel provides the image', () =>
|
||||||
|
sharp(fixtures.inputPngImageInAlpha)
|
||||||
|
.trim()
|
||||||
|
.toBuffer({ resolveWithObject: true })
|
||||||
|
.then(({ data, info }) => {
|
||||||
|
assert.strictEqual(true, data.length > 0);
|
||||||
|
assert.strictEqual('png', info.format);
|
||||||
|
assert.strictEqual(916, info.width);
|
||||||
|
assert.strictEqual(137, info.height);
|
||||||
|
assert.strictEqual(4, info.channels);
|
||||||
|
assert.strictEqual(-6, info.trimOffsetLeft);
|
||||||
|
assert.strictEqual(-20, info.trimOffsetTop);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
it('16-bit PNG with alpha channel', function (done) {
|
it('16-bit PNG with alpha channel', function (done) {
|
||||||
sharp(fixtures.inputPngWithTransparency16bit)
|
sharp(fixtures.inputPngWithTransparency16bit)
|
||||||
.resize(32, 32)
|
.resize(32, 32)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user