-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode-ancillary.js
More file actions
60 lines (54 loc) · 1.31 KB
/
encode-ancillary.js
File metadata and controls
60 lines (54 loc) · 1.31 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
53
54
55
56
57
58
59
60
import {
ChunkType,
ColorType,
colorTypeToChannels,
encode,
encode_iTXt,
encode_pHYs_PPI,
} from "../index.js";
import { deflate } from "pako";
import fs from "node:fs/promises";
import { dirname } from "node:path";
import getDocument from "canvas-dimensions";
const output = process.argv[2];
if (!output)
throw new Error(
"Must specify an output, example:\n node encode-ancillary.js myfile.png"
);
const pixelsPerInch = 300;
const { canvasWidth: width, canvasHeight: height } = getDocument({
dimensions: "A4",
pixelsPerInch,
units: "cm",
});
const colorType = ColorType.RGB;
const channels = colorTypeToChannels(colorType);
const data = new Uint8ClampedArray(width * height * channels);
// fill with pure black pixels
data.fill(0x00);
// encode an image
const buf = encode(
{
width,
height,
data,
colorType,
ancillary: [
{ type: ChunkType.pHYs, data: encode_pHYs_PPI(pixelsPerInch) },
{
// encode some JSON into the PNG as well for fun
type: ChunkType.iTXt,
data: encode_iTXt({
keyword: "metadata",
text: JSON.stringify({ seed: "some-random-seed" }),
}),
},
],
},
deflate
);
// mkdirp and write file
try {
await fs.mkdir(dirname(output), { recursive: true });
} catch (err) {}
await fs.writeFile(output, buf);