Skip to content

Commit dd67871

Browse files
Fix blur with transparent pixels
(cherry picked from commit 3cd49fd)
1 parent 068cd03 commit dd67871

File tree

7 files changed

+57
-42
lines changed

7 files changed

+57
-42
lines changed

packages/jimp/src/__snapshots__/callbacks.test.ts.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ FF-00-00⁷ᶠ 00-FF-00⁷ᶠ 00-00-FF⁷ᶠ
1717
exports[`Callbacks > with blur 1`] = `
1818
Visualization:
1919
20-
7▩▵
21-
F0F
22-
2B4
20+
▩▴4
21+
C◆▴
22+
1 ◆
2323
2424
Data:
2525
26-
AA-94-3Fᶠ¹ 7E-7E-7Eᶠ¹ 54-69-BFᶠ¹
27-
9B-C2-52ᵈ⁴ 85-A7-85ᵈ⁴ 6E-8B-B6ᵈ
28-
88-00-6Cᵇ⁷ 8C-DC-8Cᵇ⁷ 92-B7-ACᵇ
26+
97-83-38ᶠ¹ 71-71-71ᶠ¹ 4B-5E-A9ᶠ¹
27+
97-83-38ᵈ⁴ 71-71-71ᵈ⁴ 4B-5E-A9ᵈ
28+
97-83-38ᵇ⁷ 71-71-71ᵇ⁷ 4B-5E-A9ᵇ
2929
`;
3030

3131
exports[`Callbacks > with brightness 1`] = `

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;

plugins/plugin-shadow/src/__snapshots__/index.test.ts.snap

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ exports[`Shadow > creates a shadow 1`] = `
44
Visualization:
55
66
7-
▪▫▦▾8F▥
8-
88F▵▿E▾▦F
9-
6▵02▪7▪▫▹E
10-
C0▰▥8665◇▹
11-
817◇▴8◆15C
12-
▱3▾▱▿4C▩7■
7+
0▱▥1▪◇▫
8+
▪ED▥93F5▫
9+
▰C7▸◇▥▵□A1
10+
F◇▦▾2A651▫
11+
◆■B■■3F6▰▰
12+
E▱▥!▸0■D6▫
1313
1414
Data:
1515
1616
00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰ 00-00-00⁰⁰
17-
00-00-00⁰⁰ A4-A4-3F⁰² 03-03-2F⁰ᵇ 35-35-34¹⁷ E5-E5-06ᶠᶠ F1-F1-06ᶠᶠ 5B-5B-3A¹² 2C-2C-37⁰⁶ 00-00-00⁰⁰ 00-00-00⁰⁰
18-
8F-8F-55⁰² C2-C2-55⁰ᵃ 3A-3A-59¹ᶜ D2-D2-10ᶠᶠ 76-76-72ᶠᶠ 80-80-74ᶠᶠ E6-E6-0Eᶠᶠ A8-A8-5B¹³ D2-D2-3F⁰⁵ 00-00-00⁰⁰
19-
5F-5F-3F⁰⁸ 81-81-5C¹ᵃ ED-ED-15ᶠᶠ 83-83-73ᶠᶠ 5E-5E-6Eᶠᶠ 67-67-6Fᶠᶠ 9C-9C-74ᶠᶠ DC-DC-0Eᶠᶠ ED-ED-3F¹⁰ 0E-0E-00⁰⁴
20-
1E-1E-4C⁰ᵉ DE-DE-59²ᵃ BC-BC-5F⁵⁴ 86-86-2Eᶠᶠ AF-AF-6Cᶠᶠ B3-B3-6Cᶠᶠ A6-A6-27ᶠᶠ 4D-4D-4F⁴³ E7-E7-3E¹ᶜ E9-E9-00⁰⁷
21-
AE-AE-4A¹⁰ 5E-5E-56²ᶠ 8A-8A-5E⁵ᶜ EF-EF-5E⁸⁷ D5-D5-3Bᶠᶠ DD-DD-38ᶠᶠ 16-16-5A⁷ᵃ 07-07-4E⁴ᵃ 16-16-3B²⁰ EE-EE-00⁰⁹
22-
77-77-3E⁰ᵇ F7-F7-53²⁴ FE-FE-5C⁴ᵃ 2A-2A-5C⁷³ E9-E9-5A⁸ᵇ F5-F5-5A⁸⁶ 62-62-57⁶⁶ AE-AE-4E³ᵃ 5B-5B-34¹⁷ B0-B0-00⁰⁶
17+
00-00-00⁰⁰ 04-04-00⁰² 0F-0F-00⁰ᵇ 20-20-02¹⁷ E4-E4-00ᶠᶠ E5-E5-00ᶠᶠ 18-18-01¹² 09-09-00⁰⁶ 00-00-00⁰⁰ 00-00-00⁰⁰
18+
03-03-00⁰² 0D-0D-01⁰ᵃ 22-22-05¹ᶜ D8-D8-02ᶠᶠ 73-73-63ᶠᶠ 72-72-64ᶠᶠ DD-DD-01ᶠᶠ 18-18-03¹³ 07-07-00⁰⁵ 00-00-00⁰⁰
19+
0B-0B-00⁰⁸ 20-20-05¹ᵃ D4-D4-03ᶠᶠ 75-75-5Dᶠᶠ 7C-7C-59ᶠᶠ 7A-7A-5Aᶠᶠ 73-73-60ᶠᶠ DB-DB-01ᶠᶠ 16-16-02¹⁰ 06-06-00⁰⁴
20+
13-13-01⁰ᵉ 33-33-08²ᵃ 5D-5D-1A⁵⁴ C0-C0-19ᶠᶠ 89-89-5Dᶠᶠ 87-87-5Cᶠᶠ C2-C2-12ᶠᶠ 4E-4E-11⁴³ 25-25-04¹ᶜ 0B-0B-00⁰⁷
21+
16-16-01¹⁰ 3A-3A-0A²ᶠ 65-65-1D⁵ᶜ 87-87-3A⁸⁷ BE-BE-30ᶠᶠ BE-BE-2Bᶠᶠ 7F-7F-2F⁷ᵃ 56-56-14⁴ᵃ 2A-2A-04²⁰ 0D-0D-00⁰⁹
22+
0F-0F-01⁰ᵇ 2C-2C-07²⁴ 54-54-16⁴ᵃ 78-78-2D⁷³ 8B-8B-3C⁸ᵇ 87-87-38⁸⁶ 6E-6E-24⁶⁶ 45-45-0F³ᵃ 1E-1E-03¹⁷ 09-09-00⁰⁶
2323
`;

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)