mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Ensure greyscale images can be trimmed #3386
This commit is contained in:
parent
70e6bb0162
commit
d1004eed02
@ -19,6 +19,9 @@ Requires libvips v8.13.1
|
|||||||
* Ensure AVIF output is always 8-bit.
|
* Ensure AVIF output is always 8-bit.
|
||||||
[#3358](https://github.com/lovell/sharp/issues/3358)
|
[#3358](https://github.com/lovell/sharp/issues/3358)
|
||||||
|
|
||||||
|
* Ensure greyscale images can be trimmed (regression in 0.31.0).
|
||||||
|
[#3386](https://github.com/lovell/sharp/issues/3386)
|
||||||
|
|
||||||
### v0.31.0 - 5th September 2022
|
### v0.31.0 - 5th September 2022
|
||||||
|
|
||||||
* Drop support for Node.js 12, now requires Node.js >= 14.15.0.
|
* Drop support for Node.js 12, now requires Node.js >= 14.15.0.
|
||||||
|
@ -289,17 +289,20 @@ namespace sharp {
|
|||||||
background = image.extract_area(0, 0, 1, 1)(0, 0);
|
background = image.extract_area(0, 0, 1, 1)(0, 0);
|
||||||
multiplier = 1.0;
|
multiplier = 1.0;
|
||||||
}
|
}
|
||||||
if (background.size() == 4) {
|
if (HasAlpha(image) && background.size() == 4) {
|
||||||
// Just discard the alpha because flattening the background colour with
|
// Just discard the alpha because flattening the background colour with
|
||||||
// itself (effectively what find_trim() does) gives the same result
|
// itself (effectively what find_trim() does) gives the same result
|
||||||
backgroundAlpha[0] = background[3] * multiplier;
|
backgroundAlpha[0] = background[3] * multiplier;
|
||||||
}
|
}
|
||||||
background = {
|
if (image.bands() > 2) {
|
||||||
background[0] * multiplier,
|
background = {
|
||||||
background[1] * multiplier,
|
background[0] * multiplier,
|
||||||
background[2] * multiplier
|
background[1] * multiplier,
|
||||||
};
|
background[2] * multiplier
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
background[0] = background[0] * multiplier;
|
||||||
|
}
|
||||||
int left, top, width, height;
|
int left, top, width, height;
|
||||||
left = image.find_trim(&top, &width, &height, VImage::option()
|
left = image.find_trim(&top, &width, &height, VImage::option()
|
||||||
->set("background", background)
|
->set("background", background)
|
||||||
|
@ -124,6 +124,32 @@ describe('Trim borders', function () {
|
|||||||
assert.strictEqual(trimOffsetLeft, -13);
|
assert.strictEqual(trimOffsetLeft, -13);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Ensure greyscale image can be trimmed', async () => {
|
||||||
|
const greyscale = await sharp({
|
||||||
|
create: {
|
||||||
|
width: 16,
|
||||||
|
height: 8,
|
||||||
|
channels: 3,
|
||||||
|
background: 'silver'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.extend({ left: 12, right: 24, background: 'gray' })
|
||||||
|
.toColourspace('b-w')
|
||||||
|
.png({ compressionLevel: 0 })
|
||||||
|
.toBuffer();
|
||||||
|
|
||||||
|
const { info } = await sharp(greyscale)
|
||||||
|
.trim()
|
||||||
|
.raw()
|
||||||
|
.toBuffer({ resolveWithObject: true });
|
||||||
|
|
||||||
|
const { width, height, trimOffsetTop, trimOffsetLeft } = info;
|
||||||
|
assert.strictEqual(width, 16);
|
||||||
|
assert.strictEqual(height, 8);
|
||||||
|
assert.strictEqual(trimOffsetTop, 0);
|
||||||
|
assert.strictEqual(trimOffsetLeft, -12);
|
||||||
|
});
|
||||||
|
|
||||||
it('Ensure trim of image with all pixels same is no-op', async () => {
|
it('Ensure trim of image with all pixels same is no-op', async () => {
|
||||||
const { info } = await sharp({
|
const { info } = await sharp({
|
||||||
create: {
|
create: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user