mirror of
https://github.com/lovell/sharp.git
synced 2025-07-15 13:10:13 +02:00
Add alpha channel, if required, before extend operation (#439)
This commit is contained in:
parent
331926dc3c
commit
e699e36270
@ -598,12 +598,19 @@ class PipelineWorker : public AsyncWorker {
|
|||||||
baton->background[2] * multiplier
|
baton->background[2] * multiplier
|
||||||
};
|
};
|
||||||
// Add alpha channel to background colour
|
// Add alpha channel to background colour
|
||||||
if (HasAlpha(image)) {
|
if (baton->background[3] < 255.0 || HasAlpha(image)) {
|
||||||
background.push_back(baton->background[3] * multiplier);
|
background.push_back(baton->background[3] * multiplier);
|
||||||
}
|
}
|
||||||
|
// Add non-transparent alpha channel, if required
|
||||||
|
if (baton->background[3] < 255.0 && !HasAlpha(image)) {
|
||||||
|
image = image.bandjoin(
|
||||||
|
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
|
||||||
|
);
|
||||||
|
}
|
||||||
// Embed
|
// Embed
|
||||||
baton->width = image.width() + baton->extendLeft + baton->extendRight;
|
baton->width = image.width() + baton->extendLeft + baton->extendRight;
|
||||||
baton->height = image.height() + baton->extendTop + baton->extendBottom;
|
baton->height = image.height() + baton->extendTop + baton->extendBottom;
|
||||||
|
|
||||||
image = image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height,
|
image = image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height,
|
||||||
VImage::option()->set("extend", VIPS_EXTEND_BACKGROUND)->set("background", background));
|
VImage::option()->set("extend", VIPS_EXTEND_BACKGROUND)->set("background", background));
|
||||||
}
|
}
|
||||||
|
BIN
test/fixtures/expected/addAlphaChanelBeforeExtend.png
vendored
Normal file
BIN
test/fixtures/expected/addAlphaChanelBeforeExtend.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 644 KiB |
@ -49,4 +49,16 @@ describe('Extend', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should add alpha channel before extending with a transparent Background', function( done ){
|
||||||
|
sharp(fixtures.inputJpgWithLandscapeExif1)
|
||||||
|
.background({r: 0, g: 0, b: 0, a: 0})
|
||||||
|
.toFormat( sharp.format.png )
|
||||||
|
.extend({top: 0, bottom: 10, left: 0, right: 10})
|
||||||
|
.toBuffer( function(err, data, info){
|
||||||
|
assert.strictEqual(610, info.width);
|
||||||
|
assert.strictEqual(460, info.height);
|
||||||
|
fixtures.assertSimilar(fixtures.expected('addAlphaChanelBeforeExtend.png'), data, done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user