-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread-ihdr.js
More file actions
35 lines (30 loc) · 897 Bytes
/
read-ihdr.js
File metadata and controls
35 lines (30 loc) · 897 Bytes
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
import { colorTypeToChannels, colorTypeToString, readIHDR } from "../index.js";
import fs from "node:fs";
const input = process.argv[2];
if (!input)
throw new Error(
"Must specify an input, example:\n node read-ihdr.js myfile.png"
);
// The first 33 bytes of a PNG file are the header + IHDR
const byteCount = 33;
const bytes = await readBytes(input, byteCount);
// read IHDR chunk data
const data = readIHDR(bytes);
console.log(`Size: ${data.width} x ${data.height} px`);
console.log(
`Format: ${colorTypeToString(data.colorType)} (${colorTypeToChannels(
data.colorType
)} channels)`
);
console.log(`Depth: ${data.depth} bpp`);
async function readBytes(path, nBytes) {
const chunks = [];
const stream = fs.createReadStream(path, {
start: 0,
end: nBytes - 1,
});
for await (let chunk of stream) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
}