Skip to content

Commit

Permalink
blit alpha bug (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie authored and edi9999 committed Sep 2, 2018
1 parent b5e5577 commit c09d130
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
14 changes: 13 additions & 1 deletion packages/jimp/test/blit.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Jimp, mkJGD } from './test-helper';
import { Jimp, mkJGD, getTestDir } from './test-helper';

describe('Blit over image', () => {
const targetJGD = mkJGD(
Expand Down Expand Up @@ -148,4 +148,16 @@ describe('Blit over image', () => {
)
);
});

it('blit alpha', async () => {
const expectedImg = await Jimp.read(
getTestDir() + '/samples/blit-alpha.png'
);
const dice = await Jimp.read(getTestDir() + '/samples/dice.png');
const image = await Jimp.read(getTestDir() + '/samples/cops.jpg');

image
.blit(dice, 0, 0)
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
});
});
Binary file added packages/jimp/test/samples/blit-alpha.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 23 additions & 4 deletions packages/plugin-blit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,29 @@ export default () => ({
maxh - y - sy > 0
) {
const dstIdx = baseImage.getPixelIndex(x + sx - srcx, y + sy - srcy);
baseImage.bitmap.data[dstIdx] = this.bitmap.data[idx];
baseImage.bitmap.data[dstIdx + 1] = this.bitmap.data[idx + 1];
baseImage.bitmap.data[dstIdx + 2] = this.bitmap.data[idx + 2];
baseImage.bitmap.data[dstIdx + 3] = this.bitmap.data[idx + 3];
const src = {
r: this.bitmap.data[idx],
g: this.bitmap.data[idx + 1],
b: this.bitmap.data[idx + 2],
a: this.bitmap.data[idx + 3]
};

const dst = {
r: baseImage.bitmap.data[dstIdx],
g: baseImage.bitmap.data[dstIdx + 1],
b: baseImage.bitmap.data[dstIdx + 2],
a: baseImage.bitmap.data[dstIdx + 3]
};

baseImage.bitmap.data[dstIdx] =
((src.a * (src.r - dst.r) - dst.r + 255) >> 8) + dst.r;
baseImage.bitmap.data[dstIdx + 1] =
((src.a * (src.g - dst.g) - dst.g + 255) >> 8) + dst.g;
baseImage.bitmap.data[dstIdx + 2] =
((src.a * (src.b - dst.b) - dst.b + 255) >> 8) + dst.b;
baseImage.bitmap.data[dstIdx + 3] = this.constructor.limit255(
dst.a + src.a
);
}
});

Expand Down

0 comments on commit c09d130

Please sign in to comment.