This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmirage-tank.ts
139 lines (127 loc) · 4.76 KB
/
mirage-tank.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import Jimp from "jimp/browser/lib/jimp.js";
import ab2b from "arraybuffer-to-buffer";
import * as mjs from "mathjs";
class MirageTankImage {
readonly width: number;
readonly height: number;
private channel: number;
private data: mjs.Matrix;
constructor(image: Jimp | mjs.Matrix) {
if (image instanceof Jimp) {
image = image as Jimp;
this.width = image.bitmap.width;
this.height = image.bitmap.height;
const matrix = mjs.zeros([this.height, this.width, 3], "dense") as mjs.Matrix;
image.scan(0, 0, image.bitmap.width, image.bitmap.height,
function (x: number, y: number, index: number) {
matrix.set([y, x, 0], this.bitmap.data[index]);
matrix.set([y, x, 1], this.bitmap.data[index] + 1);
matrix.set([y, x, 2], this.bitmap.data[index] + 2);
}
);
this.data = matrix;
this.channel = 3;
} else {
image = image as mjs.Matrix;
const size = image.size();
this.width = size[1];
this.height = size[0];
this.channel = size[2];
this.data = image;
}
}
getData(): mjs.Matrix {
return this.data;
}
desaturate() {
this.data = mjs.divide(mjs.add(mjs.max(this.data, 2), mjs.min(this.data, 2)), 2) as unknown as mjs.Matrix;
this.channel = 1;
}
invert() {
this.data = mjs.subtract(255, this.data) as mjs.Matrix;
}
adjustLightness(ratio: number) {
if (ratio > 0) {
this.data = mjs.add(mjs.multiply(this.data, 1 - ratio), 255 * ratio) as mjs.Matrix;
} else {
this.data = mjs.multiply(this.data, 1 + ratio);
}
}
linearDodgeBlend(image: MirageTankImage) {
this.data = mjs.add(this.data, image.getData()) as mjs.Matrix;
}
divideBlend(image: MirageTankImage) {
const result = mjs.zeros([this.height, this.width], "dense") as mjs.Matrix;
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
this.data.forEach((value: number, index: number[]) => {
const i = mjs.index(...index);
// @ts-ignore
const mix = this.data.subset(i) as number;
// @ts-ignore
const base = image.getData().subset(i) as number;
let color: number;
switch (mix) {
case 0:
color = (base === 0) ? 0 : 255;
break;
case 255:
color = base;
break;
case base:
color = 255;
break;
default:
color = mjs.round((base / mix) * 255);
}
result.set([...index], color);
});
/* eslint-enable @typescript-eslint/ban-ts-comment */
this.data = result;
}
clone(): MirageTankImage {
return new MirageTankImage(this.data.clone());
}
toRGBA(data?: mjs.Matrix) {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
this.data = this.data.map((value: number, index: number[]) => {
value = mjs.round(value);
if (value > 255) { value = 255; }
if (value < 0) { value = 0; }
// @ts-ignore
const alpha = (data) ? data.subset(mjs.index(...index)) as number : 255;
return [value, value, value, alpha];
});
/* eslint-enable @typescript-eslint/ban-ts-comment */
this.channel = 4;
}
toUint8Array(): Uint8Array {
return new Uint8Array(this.data.toArray().flat(2));
}
}
export async function buildImage(top: ArrayBuffer, bottom: ArrayBuffer): Promise<string> {
const topImage: Jimp = await Jimp.read(ab2b(top));
const bottomImage: Jimp = await Jimp.read(ab2b(bottom));
return new Promise<string>((resolve, reject) => {
if (topImage.bitmap.width !== bottomImage.bitmap.width ||
topImage.bitmap.height !== bottomImage.bitmap.height) reject(new Error("not same size"));
const top = new MirageTankImage(topImage);
const bottom = new MirageTankImage(bottomImage);
top.desaturate();
bottom.desaturate();
top.adjustLightness(0.5);
bottom.adjustLightness(-0.5);
top.invert();
top.linearDodgeBlend(bottom);
const linearDodged = top.clone();
top.divideBlend(bottom);
top.toRGBA(linearDodged.getData());
const result: Jimp = new Jimp({
data: top.toUint8Array(),
width: topImage.bitmap.width,
height: topImage.bitmap.height
});
resolve(result.getBase64Async(Jimp.MIME_PNG));
});
}