-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbun-encode.js
More file actions
52 lines (44 loc) · 1.25 KB
/
bun-encode.js
File metadata and controls
52 lines (44 loc) · 1.25 KB
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
import { ColorType, colorTypeToChannels, encode } from "../index.js";
import { deflate } from "pako";
const output = Bun.argv[2];
if (!output)
throw new Error(
"Must specify an output, example:\n bun run examples/bun-encode.js myfile.png"
);
const width = 4096;
const height = 4096;
const colorType = ColorType.RGB;
const depth = 8;
const channels = colorTypeToChannels(colorType);
const ArrType = depth === 16 ? Uint16Array : Uint8ClampedArray;
const maxValue = depth === 16 ? 0xffff : 0xff;
const data = new ArrType(width * height * channels).fill(maxValue);
// create the first scanline of a gradient
for (let x = 0; x < width; x++) {
const u = width <= 1 ? 1 : x / (width - 1);
const color = Math.round(u * maxValue);
for (let c = 0; c < channels; c++) {
data[x * channels + c] = color;
}
}
// now quickly repeat this across the rest of the height
for (let y = 1; y < height; y++) {
const x = 0;
const idx = x + y * width;
data.copyWithin(idx * channels, 0, width * channels);
}
// encode an image
console.time("encode");
const buf = encode(
{
width,
height,
data,
colorType,
depth,
},
deflate
);
console.timeEnd("encode");
// await Deno.mkdir(dirname(output), { recursive: true });
await Bun.write(output, buf);