MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / extractPngMetadataFromBuffer

Function extractPngMetadataFromBuffer

packages/engine/src/utils/ffprobe.ts:140–211  ·  view source on GitHub ↗
(buf: Buffer)

Source from the content-addressed store, hash-verified

138}
139
140export function extractPngMetadataFromBuffer(buf: Buffer): StillImageMetadata | null {
141 if (
142 buf.length < 8 ||
143 buf[0] !== 137 ||
144 buf[1] !== 80 ||
145 buf[2] !== 78 ||
146 buf[3] !== 71 ||
147 buf[4] !== 13 ||
148 buf[5] !== 10 ||
149 buf[6] !== 26 ||
150 buf[7] !== 10
151 ) {
152 return null;
153 }
154
155 let width = 0;
156 let height = 0;
157 let seenIdat = false;
158 let pos = 8;
159 while (pos + 12 <= buf.length) {
160 const chunkLen = buf.readUInt32BE(pos);
161 const chunkType = buf.toString("ascii", pos + 4, pos + 8);
162 if (pos + 12 + chunkLen > buf.length) return null;
163 const chunkData = buf.subarray(pos + 8, pos + 8 + chunkLen);
164 const chunkCrc = buf.readUInt32BE(pos + 8 + chunkLen);
165 const chunkBytes = Buffer.concat([Buffer.from(chunkType, "ascii"), chunkData]);
166 if (crc32(chunkBytes) !== chunkCrc) return null;
167
168 if (chunkType === "IHDR" && chunkLen >= 8) {
169 width = buf.readUInt32BE(pos + 8);
170 height = buf.readUInt32BE(pos + 12);
171 }
172
173 if (chunkType === "IDAT") {
174 seenIdat = true;
175 }
176
177 if (chunkType === "cICP" && chunkLen === 4 && !seenIdat) {
178 const primariesCode = chunkData[0] ?? 0;
179 const transferCode = chunkData[1] ?? 0;
180 const matrixCode = chunkData[2] ?? 0;
181
182 return {
183 width,
184 height,
185 colorSpace: {
186 colorPrimaries:
187 primariesCode === 9
188 ? "bt2020"
189 : primariesCode === 1
190 ? "bt709"
191 : `unknown-${primariesCode}`,
192 colorTransfer:
193 transferCode === 16
194 ? "smpte2084"
195 : transferCode === 18
196 ? "arib-std-b67"
197 : transferCode === 1

Callers 2

ffprobe.test.tsFile · 0.85

Calls 2

fromMethod · 0.80
crc32Function · 0.70

Tested by

no test coverage detected