* Detect image dimensions from PNG or JPEG bytes.
(
bytes: Buffer,
)
| 1909 | * Detect image dimensions from PNG or JPEG bytes. |
| 1910 | */ |
| 1911 | function detectImageDimensions( |
| 1912 | bytes: Buffer, |
| 1913 | ): { width: number; height: number } | null { |
| 1914 | // PNG: width at offset 16 (4 bytes BE), height at offset 20 (4 bytes BE) |
| 1915 | if ( |
| 1916 | bytes[0] === 0x89 && |
| 1917 | bytes[1] === 0x50 && |
| 1918 | bytes[2] === 0x4e && |
| 1919 | bytes[3] === 0x47 |
| 1920 | ) { |
| 1921 | if (bytes.length >= 24) { |
| 1922 | const width = bytes.readUInt32BE(16); |
| 1923 | const height = bytes.readUInt32BE(20); |
| 1924 | return { width, height }; |
| 1925 | } |
| 1926 | } |
| 1927 | // JPEG: scan for SOF0/SOF2 markers (0xFF 0xC0 / 0xFF 0xC2) |
| 1928 | if (bytes[0] === 0xff && bytes[1] === 0xd8) { |
| 1929 | let offset = 2; |
| 1930 | while (offset < bytes.length - 8) { |
| 1931 | if (bytes[offset] !== 0xff) break; |
| 1932 | const marker = bytes[offset + 1]; |
| 1933 | if (marker === 0xc0 || marker === 0xc2) { |
| 1934 | const height = bytes.readUInt16BE(offset + 5); |
| 1935 | const width = bytes.readUInt16BE(offset + 7); |
| 1936 | return { width, height }; |
| 1937 | } |
| 1938 | const segLen = bytes.readUInt16BE(offset + 2); |
| 1939 | offset += 2 + segLen; |
| 1940 | } |
| 1941 | } |
| 1942 | return null; |
| 1943 | } |
| 1944 | |
| 1945 | /** Process a single interact command. Returns content parts and an isError flag. */ |
| 1946 | async function processInteractCommand( |
no outgoing calls
no test coverage detected
searching dependent graphs…