Skip to content

Commit ce93e64

Browse files
Fix blur with transparent pixels
1 parent a425b4c commit ce93e64

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

plugins/plugin-blur/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
"author": "Andrew Lisowski <[email protected]>",
1414
"license": "MIT",
1515
"dependencies": {
16-
"@jimp/types": "workspace:*"
16+
"@jimp/core": "workspace:*",
17+
"@jimp/utils": "workspace:*"
1718
},
1819
"devDependencies": {
1920
"@jimp/config-eslint": "workspace:*",
2021
"@jimp/config-typescript": "workspace:*",
2122
"@jimp/config-vitest": "workspace:*",
23+
"@jimp/js-png": "workspace:*",
24+
"@jimp/test-utils": "workspace:*",
25+
"@jimp/types": "workspace:*",
2226
"@vitest/browser": "^1.4.0",
2327
"eslint": "^8.57.0",
2428
"tshy": "^3.0.2",

plugins/plugin-blur/src/index.test.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
import { expect, test } from "vitest";
1+
import { describe, expect, test } from "vitest";
2+
import { createJimp } from "@jimp/core";
3+
import { getTestImagePath } from "@jimp/test-utils";
4+
import "@jimp/test-utils/image-snapshot";
5+
import png from "@jimp/js-png";
26

3-
test("adds 1 + 2 to equal 3", () => {
4-
expect(1 + 2).toBe(3);
7+
import * as blur from "./index.js";
8+
9+
const Jimp = createJimp({ formats: [png], plugins: [blur.methods] });
10+
11+
describe("hasAlpha", () => {
12+
test("image with alpha", async () => {
13+
const image = await Jimp.read(getTestImagePath("dice.png"));
14+
const output = await image.blur(16).getBuffer("image/png");
15+
expect(output).toMatchImageSnapshot();
16+
});
517
});

plugins/plugin-blur/src/index.ts

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mulTable, shgTable } from "./blur-tables.js";
22
import { JimpClass } from "@jimp/types";
3+
import { limit255 } from "@jimp/utils";
34

45
/*
56
Superfast Blur (0.5)
@@ -145,24 +146,10 @@ export const methods = {
145146
yi = x << 2;
146147

147148
for (y = 0; y < image.bitmap.height; y++) {
148-
pa = (asum * mulSum) >>> shgSum;
149-
image.bitmap.data[yi + 3] = pa;
150-
151-
// normalize alpha
152-
if (pa > 255) {
153-
image.bitmap.data[yi + 3] = 255;
154-
}
155-
156-
if (pa > 0) {
157-
pa = 255 / pa;
158-
image.bitmap.data[yi] = ((rsum * mulSum) >>> shgSum) * pa;
159-
image.bitmap.data[yi + 1] = ((gsum * mulSum) >>> shgSum) * pa;
160-
image.bitmap.data[yi + 2] = ((bsum * mulSum) >>> shgSum) * pa;
161-
} else {
162-
image.bitmap.data[yi + 2] = 0;
163-
image.bitmap.data[yi + 1] = 0;
164-
image.bitmap.data[yi] = 0;
165-
}
149+
image.bitmap.data[yi] = limit255((rsum * mulSum) >>> shgSum);
150+
image.bitmap.data[yi + 1] = limit255((gsum * mulSum) >>> shgSum);
151+
image.bitmap.data[yi + 2] = limit255((bsum * mulSum) >>> shgSum);
152+
image.bitmap.data[yi + 3] = limit255((asum * mulSum) >>> shgSum);
166153

167154
if (x === 0) {
168155
vmin[y] = ((p = y + rad1) < hm ? p : hm) * image.bitmap.width;

pnpm-lock.yaml

+14-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)