Skip to content

Commit a38126c

Browse files
committedMar 20, 2021
Ensure composite replicates correct tiles with centre gravity #2626
1 parent cb592ce commit a38126c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed
 

‎docs/changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Requires libvips v8.10.6
2020
[#2612](https://github.com/lovell/sharp/issues/2612)
2121
[@edsilv](https://github.com/edsilv)
2222

23+
* Ensure composite replicates the correct number of tiles for centred gravities.
24+
[#2626](https://github.com/lovell/sharp/issues/2626)
25+
2326
## v0.27 - *avif*
2427

2528
Requires libvips v8.10.5

‎src/pipeline.cc

+8
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,17 @@ class PipelineWorker : public Napi::AsyncWorker {
562562
// Use gravity in overlay
563563
if (compositeImage.width() <= baton->width) {
564564
across = static_cast<int>(ceil(static_cast<double>(image.width()) / compositeImage.width()));
565+
// Ensure odd number of tiles across when gravity is centre, north or south
566+
if (composite->gravity == 0 || composite->gravity == 1 || composite->gravity == 3) {
567+
across |= 1;
568+
}
565569
}
566570
if (compositeImage.height() <= baton->height) {
567571
down = static_cast<int>(ceil(static_cast<double>(image.height()) / compositeImage.height()));
572+
// Ensure odd number of tiles down when gravity is centre, east or west
573+
if (composite->gravity == 0 || composite->gravity == 2 || composite->gravity == 4) {
574+
down |= 1;
575+
}
568576
}
569577
if (across != 0 || down != 0) {
570578
int left;

‎test/unit/composite.js

+18
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ describe('composite', () => {
225225
});
226226
});
227227

228+
it('centre gravity should replicate correct number of tiles', async () => {
229+
const red = { r: 255, g: 0, b: 0 };
230+
const [r, g, b] = await sharp({
231+
create: {
232+
width: 40, height: 40, channels: 4, background: red
233+
}
234+
})
235+
.composite([{
236+
input: fixtures.inputPngWithTransparency16bit,
237+
gravity: 'centre',
238+
tile: true
239+
}])
240+
.raw()
241+
.toBuffer();
242+
243+
assert.deepStrictEqual({ r, g, b }, red);
244+
});
245+
228246
it('cutout via dest-in', done => {
229247
sharp(fixtures.inputJpg)
230248
.resize(300, 300)

0 commit comments

Comments
 (0)
Please sign in to comment.