Skip to content

Commit

Permalink
Fix: convolute not defaulting to 0, 0 as starting point (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerd108 committed May 11, 2023
1 parent bc22100 commit 16ee71c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/plugin-color/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export default () => ({
* @param {number} w (optional) the width of the region to apply convolution to
* @param {number} h (optional) the height of the region to apply convolution to
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
* @returns {Jimp }this for chaining of methods
* @returns {Jimp} this for chaining of methods
*/
convolute(kernel, x, y, w, h, cb) {

Check warning on line 588 in packages/plugin-color/src/index.js

View workflow job for this annotation

GitHub Actions / lint

Method 'convolute' has too many parameters (6). Maximum allowed is 4
if (!Array.isArray(kernel))
Expand Down Expand Up @@ -613,10 +613,8 @@ export default () => ({
}
}

const ksize = (kernel.length - 1) / 2;

x = isDef(x) ? x : ksize;
y = isDef(y) ? y : ksize;
x = isDef(x) ? x : 0;
y = isDef(y) ? y : 0;
w = isDef(w) ? w : this.bitmap.width - x;
h = isDef(h) ? h : this.bitmap.height - y;

Expand Down
16 changes: 16 additions & 0 deletions packages/plugin-color/test/convolution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ describe("Convolution", function () {
[0, 1, 1],
];

const blurM = [
[1 / 9, 1 / 9, 1 / 9],
[1 / 9, 1 / 9, 1 / 9],
[1 / 9, 1 / 9, 1 / 9],
];

it("3x3 sharp matrix on EDGE_EXTEND", (done) => {
expectToBeJGD(
imgMid.clone().convolution(sharpM).getJGDSync(),
Expand Down Expand Up @@ -150,6 +156,16 @@ describe("Convolution", function () {
done();
});

it("3x3 box blur matrix using convolute", async () => {
const expectedImg = await jimp.read(
getTestDir(__dirname) + "/images/tiles-blurred.png"
);

const image = await jimp.read(getTestDir(__dirname) + "/images/tiles.jpg");

expect(image.convolute(blurM).bitmap.data).toEqual(expectedImg.bitmap.data);
});

it("new pixel value is greater than 255", async () => {
const expectedImg = await jimp.read(
getTestDir(__dirname) + "/images/qr-convoluted.png"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/plugin-color/test/images/tiles.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 16ee71c

Please sign in to comment.