-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_display.ts
310 lines (251 loc) · 9.59 KB
/
run_display.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import fs from 'fs';
import {loadImage, createCanvas} from 'canvas';
import { BitmapClient } from './client';
const startIndex = 1440;
const page = 1;
const shuffleEnabled = false;
async function main() {
// Function to convert image to black and white and map it to a binary array
async function convertToBlackAndWhite(imagePath: string, targetWidth: number, startIndex=0, threshold = 128, invert = false) {
const image = await loadImage(imagePath);
const targetHeight = Math.floor(image.height * (targetWidth / image.width));
const canvas = createCanvas(targetWidth, targetHeight);
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
const imageData = ctx.getImageData(0, 0, targetWidth, targetHeight);
const binaryPixels = new Array(targetWidth * targetHeight).fill(0);
const gridWidth = 60;
const startRow = Math.floor(startIndex / gridWidth);
const startCol = startIndex % gridWidth;
for (let row = 0; row < targetHeight; row++) {
for (let col = 0; col < targetWidth; col++) {
const pixelIndex = row * targetWidth + col;
const gridRow = startRow + Math.floor(pixelIndex / gridWidth);
const gridCol = (startCol + col) % gridWidth;
const originalIndex = (row * targetWidth + col) * 4;
const grayscale = (imageData.data[originalIndex] + imageData.data[originalIndex + 1] + imageData.data[originalIndex + 2]) / 3;
let dark = (invert) ? 0 : 1;
let light = (invert) ? 1 : 0;
binaryPixels[gridRow * gridWidth + gridCol] = grayscale < threshold ? dark : light; // Apply threshold
}
}
let trimmedBinaryPixels = binaryPixels.slice(startIndex);
return { binaryPixels: trimmedBinaryPixels, width: targetWidth, height: targetHeight };
}
function createBlackAndWhiteImage(binaryPixels, width, height, outputPath) {
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(width, height);
for (let i = 0; i < binaryPixels.length; i++) {
const colorValue = binaryPixels[i] === 1 ? 0 : 255; // 0 for black, 255 for white
const index = i * 4;
imageData.data[index] = colorValue; // R
imageData.data[index + 1] = colorValue; // G
imageData.data[index + 2] = colorValue; // B
imageData.data[index + 3] = 255; // A (full opacity)
}
ctx.putImageData(imageData, 0, 0);
// Save the canvas as an image file
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync(outputPath, buffer);
console.log(`Black-and-white image saved to ${outputPath}`);
}
let images = [
{
name: [
//'./images/image34.jpg',
//'./images/image35.jpg',
//'./images/image31.jpg',
//'./images/image32.jpg',
//'./images/image29.jpg',
'./images/image26.jpg'
/*
'./images/image36.jpg',
'./images/image37.jpg',
'./images/image38.jpg',
'./images/image39.jpg',
'./images/image40.jpg',
'./images/image41.jpg',
'./images/image42.jpg',
'./images/image43.jpg',
'./images/image44.jpg',
'./images/image45.jpg',
'./images/image46.jpg',
'./images/image47.jpg',
'./images/image48.jpg',
*/
],
threshold: 170,
// invert: "random"
},
{
name: './images/image20.jpg',
threshold: 200,
invert: true
},
{
name: './images/image15.jpg',
threshold: 128
},
{
name: './images/image12.jpg',
threshold: 120,
invert: true
},
{
name: './images/image4.jpg',
threshold: 200
},
{
name: './images/image3.jpg',
threshold: 160,
invert: true
},
{
name: './images/image2.jpg',
threshold: 128,
invert: true
},
{
name: './images/image10.jpg',
threshold: 200
},
{
name: './images/image11.jpg',
threshold: 180
},
];
/**
*
{
name: './image.jpg',
threshold: 128
}
*/
let binaryPixelList: Array<any> = [];
function shuffleArray(array) {
let currentIndex = array.length;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
}
async function shuffle() {
for ( let index = 0; index < images.length; index++) {
if (shuffleEnabled) shuffleArray(images);
let image = structuredClone(images[index]);
if (image.name instanceof Array) image.name = image.name[Math.floor(Math.random()*image.name.length)];
// @ts-expect-error
if (image.invert instanceof String ) image.invert = (() => Math.random() < 0.5)();
convertToBlackAndWhite(image.name, 60, 0, image.threshold ).then(({ binaryPixels, width, height, }) => {
createBlackAndWhiteImage(binaryPixels, width, height, `./renders/render_${index}.jpeg`);
});
let { binaryPixels } = await convertToBlackAndWhite( image.name, 60, startIndex, image.threshold, image.invert );
binaryPixelList.push(binaryPixels)
}
}
await shuffle();
console.log(`Start: ${startIndex}\n End: ${binaryPixelList[0].length + startIndex}`);
async function navigateAndWait(client: BitmapClient, page: number, retries: number = 15000) {
// Wait for the websocket to open
let currentRetry = 0;
if (!client.websocketOpen) {
while (true) {
if (currentRetry >= retries) break;
if (client.websocketOpen) break;
currentRetry += 100;
await new Promise(r => setTimeout(r, 100));
}
}
// Wait for the chunk index to change
client.setChunkIndex(page - 1);
currentRetry = 0;
if (!client.chunkLoaded) {
while (true) {
if (currentRetry >= retries) break;
if (client.chunkLoaded) break;
currentRetry += 100;
await new Promise(r => setTimeout(r, 100));
}
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
let client = new BitmapClient();
await navigateAndWait(client, page);
let currentBinaryPixel = 0;
let imageHasChanged = true;
let combatMode = true;
setInterval( async () => {
if (shuffleEnabled) await shuffle();
}, 60000);
setInterval( () => {
currentBinaryPixel = (currentBinaryPixel + 1) % binaryPixelList.length;
imageHasChanged = true;
},
// getRandomInt(4000, 6000)
5000
);
async function drawImage() {
let chosenBinaryPixel = binaryPixelList[currentBinaryPixel]
await navigateAndWait(client, page);
let image = structuredClone(images[currentBinaryPixel]);
let isChanged = true;
if (image.name instanceof Array) {
image.name = image.name[Math.floor(Math.random()*image.name.length)];
isChanged = true;
}
if (typeof image.invert === 'string') {
// @ts-expect-error
image.invert = (() => Math.random() < 0.5)();
isChanged = true;
}
if (isChanged) {
let { binaryPixels } = await convertToBlackAndWhite( image.name, 60, startIndex, image.threshold, image.invert );
chosenBinaryPixel = binaryPixels;
}
return new Promise( (resolve, reject) => {
for (let index = 0; index < chosenBinaryPixel.length; index++) {
let pixel = chosenBinaryPixel[index];
let pixelIndex = startIndex + index;
let isChecked = client.isChecked(pixelIndex);
if (
pixel === 1 && !isChecked ||
pixel === 0 && isChecked
) {
try { client.toggle(pixelIndex); }
catch (err) { console.log(err); }
}
}
resolve("finished")
})
}
while (true) {
try {
if (combatMode) {
await drawImage()
}
else if (imageHasChanged) {
await drawImage()
imageHasChanged = false;
}
else {
await new Promise(r => setTimeout(r, 100));
}
await new Promise(r => setImmediate(r));;
}
catch (error) {
console.log(error);
process.exit(1)
}
}
}
main();