Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: convolute not defaulting to 0, 0 as starting point #1228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
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.