-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
43 lines (34 loc) · 1.15 KB
/
main.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
import imageJS from "image-js";
import path from "path";
async function main() {
if (process.argv.length !== 4) {
printUsage();
process.exit(1);
}
const inputPath: string = path.resolve(process.argv[2]);
console.log("Input image:", inputPath);
const inputFile = Bun.file(inputPath);
if (!(await inputFile.exists())) {
console.error(`ERROR: input file not found: ${inputPath}`);
process.exit(1);
}
const outputPath: string = path.resolve(process.argv[3]);
const image = await imageJS.load(inputPath);
let rgbaBuffer = "";
for (let y = 0; y < image.height; y++) {
for (let x = 0; x < image.width; x++) {
const [r, g, b] = image.getPixelXY(x, y);
rgbaBuffer += `${r},${g},${b},255,`;
}
}
if (rgbaBuffer[rgbaBuffer.length - 1] === ",") {
rgbaBuffer = rgbaBuffer.substring(0, rgbaBuffer.length - 1);
}
await Bun.write(outputPath, rgbaBuffer);
console.log("Output image:", outputPath);
}
function printUsage(): void {
console.log("Usage:");
console.log("bun run main.ts path/to/input.png path/to/output.rgba");
}
await main();