-
Notifications
You must be signed in to change notification settings - Fork 1
/
glitch.js
59 lines (48 loc) · 1.38 KB
/
glitch.js
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
const {arrayBufferToBuffer, findIndexEx} = require('./utils');
module.exports = function(jimpImg, {
startPredicate,
endPredicate,
sortBy,
consequentRows,
minimalSequence,
columns
}) {
const comparator = (a, b) => sortBy(a) < sortBy(b);
glitchRows(jimpImg, consequentRows);
if (columns) {
jimpImg.rotate(90);
glitchRows(jimpImg, consequentRows);
jimpImg.rotate(-90);
}
function glitchRows(jimpImg, consequentRows) {
const {data, width, height} = jimpImg.bitmap;
const bytes = new Uint8Array(data);
const pixels = new Uint32Array(bytes.buffer);
if (consequentRows) {
glitchRow(pixels);
return;
}
for (let y = 0; y < height; y++) {
const row = new Uint32Array(pixels.buffer, y*width*4, width);
glitchRow(row);
}
arrayBufferToBuffer(bytes.buffer).copy(data);
}
function glitchRow(row) {
let start = 0, end = 0;
while (end < row.length - 1) {
start = findIndexEx(row, startPredicate, end);
if (start === -1) {
return;
}
end = findIndexEx(row, endPredicate, start);
if (end === -1) {
end = row.length - 1;
}
if (end - start > minimalSequence) {
const arr = new Uint32Array(row.buffer, row.byteOffset + start*4, end - start);
arr.sort(comparator);
}
}
}
};