mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Ensure trim op works with CMYK input #3636
This commit is contained in:
parent
a39f959dcc
commit
8408e99aa3
@ -26,6 +26,9 @@ Requires libvips v8.14.2
|
||||
* Add support for `modulate` operation when using non-sRGB pipeline colourspace.
|
||||
[#3620](https://github.com/lovell/sharp/issues/3620)
|
||||
|
||||
* Ensure `trim` operation works with CMYK images (regression in 0.31.0).
|
||||
[#3636](https://github.com/lovell/sharp/issues/3636)
|
||||
|
||||
### v0.32.0 - 24th March 2023
|
||||
|
||||
* Default to using sequential rather than random access read where possible.
|
||||
|
@ -269,30 +269,20 @@ namespace sharp {
|
||||
if (image.width() < 3 && image.height() < 3) {
|
||||
throw VError("Image to trim must be at least 3x3 pixels");
|
||||
}
|
||||
|
||||
// Scale up 8-bit values to match 16-bit input image
|
||||
double multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
threshold *= multiplier;
|
||||
|
||||
std::vector<double> backgroundAlpha(1);
|
||||
if (background.size() == 0) {
|
||||
// Top-left pixel provides the default background colour if none is given
|
||||
background = image.extract_area(0, 0, 1, 1)(0, 0);
|
||||
multiplier = 1.0;
|
||||
} else if (sharp::Is16Bit(image.interpretation())) {
|
||||
for (size_t i = 0; i < background.size(); i++) {
|
||||
background[i] *= 256.0;
|
||||
}
|
||||
threshold *= 256.0;
|
||||
}
|
||||
if (HasAlpha(image) && background.size() == 4) {
|
||||
// Just discard the alpha because flattening the background colour with
|
||||
// itself (effectively what find_trim() does) gives the same result
|
||||
backgroundAlpha[0] = background[3] * multiplier;
|
||||
}
|
||||
if (image.bands() > 2) {
|
||||
background = {
|
||||
background[0] * multiplier,
|
||||
background[1] * multiplier,
|
||||
background[2] * multiplier
|
||||
};
|
||||
std::vector<double> backgroundAlpha({ background.back() });
|
||||
if (HasAlpha(image)) {
|
||||
background.pop_back();
|
||||
} else {
|
||||
background[0] = background[0] * multiplier;
|
||||
background.resize(image.bands());
|
||||
}
|
||||
int left, top, width, height;
|
||||
left = image.find_trim(&top, &width, &height, VImage::option()
|
||||
|
@ -153,6 +153,32 @@ describe('Trim borders', function () {
|
||||
assert.strictEqual(trimOffsetLeft, -12);
|
||||
});
|
||||
|
||||
it('Ensure CMYK image can be trimmed', async () => {
|
||||
const cmyk = await sharp({
|
||||
create: {
|
||||
width: 16,
|
||||
height: 8,
|
||||
channels: 3,
|
||||
background: 'red'
|
||||
}
|
||||
})
|
||||
.extend({ left: 12, right: 24, background: 'blue' })
|
||||
.toColourspace('cmyk')
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
|
||||
const { info } = await sharp(cmyk)
|
||||
.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 () => {
|
||||
const { info } = await sharp({
|
||||
create: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user