mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
feat: support on-demand screenshots for canvas visual refs
This commit is contained in:
1 parent
a6de73aea3
commit
31ddcb139c
11 files changed
+922
-61
No files matched your search
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Parse a PNG's IHDR chunk and return `(width, height)`. Returns
|
||||
* `null` on any malformed input so callers fall back to `0/0` instead
|
||||
* of throwing.
|
||||
*
|
||||
* PNG layout: 8-byte signature, then a 4-byte length, 4-byte type
|
||||
* ("IHDR"), then the chunk data — width is bytes 16-19 BE, height is
|
||||
* 20-23 BE.
|
||||
*/
|
||||
export function parsePngDimensions(base64: string): { width: number; height: number } | null {
|
||||
try {
|
||||
// atob is available in MV3 service workers.
|
||||
const head = base64.length > 64 ? base64.slice(0, 64) : base64;
|
||||
const bin = atob(head);
|
||||
if (bin.length < 33) return null;
|
||||
if (bin.slice(0, 8) !== "\x89PNG\r\n\x1a\n" || bin.slice(8, 16) !== "\x00\x00\x00\x0dIHDR") {
|
||||
return null;
|
||||
}
|
||||
const u32 = (off: number) =>
|
||||
(bin.charCodeAt(off) << 24) |
|
||||
(bin.charCodeAt(off + 1) << 16) |
|
||||
(bin.charCodeAt(off + 2) << 8) |
|
||||
bin.charCodeAt(off + 3);
|
||||
const width = u32(16) >>> 0;
|
||||
const height = u32(20) >>> 0;
|
||||
if (width === 0 || height === 0) return null;
|
||||
return { width, height };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user