Skip to content

Commit d963549

Browse files
committed
fisheye effect (explode/implode)
(cherry picked from commit b535bba)
1 parent 4369c80 commit d963549

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎ImageScript.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,36 @@ export class Image {
10711071
};
10721072
}
10731073

1074+
fisheye(radius = 2) {
1075+
const r = new Image(this.width, this.height);
1076+
1077+
const w = this.width;
1078+
const h = this.height;
1079+
const tu32 = this.__u32__;
1080+
const ru32 = r.__u32__;
1081+
const iw = 1 / w;
1082+
const ih = 1 / h;
1083+
1084+
for (const [x, y] of this) {
1085+
const xco = x * iw - .5;
1086+
const yco = y * ih - .5;
1087+
const dfc = Math.sqrt(xco ** 2 + yco ** 2);
1088+
const dis = 2 * dfc ** radius;
1089+
const nx = ((dis * xco / dfc + 0.5) * w) | 0;
1090+
const ny = ((dis * yco / dfc + 0.5) * h) | 0;
1091+
1092+
if (nx < 1 || nx > w || ny < 1 || ny > h || isNaN(nx) || isNaN(ny))
1093+
continue;
1094+
1095+
ru32[y * w + x] = tu32[w * ny + nx];
1096+
}
1097+
1098+
const cO = tu32.length * .5 + w / 2;
1099+
ru32[cO] = tu32[cO];
1100+
1101+
return this.__apply__(r);
1102+
}
1103+
10741104
/**
10751105
* Encodes the image into a PNG
10761106
* @param {number} compression The compression level to use (0-3)

‎tests/fisheye.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require('fs').promises;
2+
const {Image} = require('../ImageScript');
3+
4+
(async () => {
5+
const input = await fs.readFile('./tests/targets/external.png');
6+
7+
const image = await Image.decode(input);
8+
image.fisheye();
9+
10+
const output = await image.encode();
11+
const target = await fs.readFile('./tests/targets/fisheye.png');
12+
13+
if (!Buffer.from(target).equals(Buffer.from(output)))
14+
process.exit(1);
15+
})();

‎tests/targets/fisheye.png‎

755 KB
Loading

0 commit comments

Comments
 (0)