Open History to see the last commit
1export interface ImageDimensions {
2 readonly width: number;
3 readonly height: number;
4}
5
6/**
7 * Enough for every supported header. A JPEG's frame header can sit behind
8 * several 64 KiB metadata segments (EXIF, an ICC profile, XMP), so allow a
9 * few of them before giving up.
10 */
11export const IMAGE_DIMENSIONS_HEADER_BYTES = 256 * 1024;
12
13export function readImageDimensions(bytes: Uint8Array): ImageDimensions | null {
14 const dimensions = readPng(bytes) ?? readGif(bytes) ?? readWebp(bytes) ?? readJpeg(bytes);
15 return dimensions && dimensions.width > 0 && dimensions.height > 0 ? dimensions : null;
16}
17
18const view = (bytes: Uint8Array) => new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
19
20function readPng(bytes: Uint8Array): ImageDimensions | null {
21 if (bytes.length < 24) return null;
22 if (
23 bytes[0] !== 0x89 ||
24 bytes[1] !== 0x50 ||
25 bytes[2] !== 0x4e ||
26 bytes[3] !== 0x47 ||
27 bytes[12] !== 0x49 ||
28 bytes[13] !== 0x48 ||
29 bytes[14] !== 0x44 ||
30 bytes[15] !== 0x52
31 ) {
32 return null;
33 }
34 const data = view(bytes);
35 return { width: data.getUint32(16), height: data.getUint32(20) };
36}
37
38function readGif(bytes: Uint8Array): ImageDimensions | null {
39 if (bytes.length < 10) return null;
40 if (bytes[0] !== 0x47 || bytes[1] !== 0x49 || bytes[2] !== 0x46 || bytes[3] !== 0x38) return null;
41 const data = view(bytes);
42 return { width: data.getUint16(6, true), height: data.getUint16(8, true) };
43}
44
45function readWebp(bytes: Uint8Array): ImageDimensions | null {
46 if (bytes.length < 16) return null;
47 if (
48 bytes[0] !== 0x52 ||
49 bytes[1] !== 0x49 ||
50 bytes[2] !== 0x46 ||
51 bytes[3] !== 0x46 ||
52 bytes[8] !== 0x57 ||
53 bytes[9] !== 0x45 ||
54 bytes[10] !== 0x42 ||
55 bytes[11] !== 0x50
56 ) {
57 return null;
58 }
59 const data = view(bytes);
60 const chunk = String.fromCharCode(bytes[12]!, bytes[13]!, bytes[14]!, bytes[15]!);
61 switch (chunk) {
62 case "VP8 ":
63 if (bytes.length < 30) return null;
64 return {
65 width: data.getUint16(26, true) & 0x3fff,
66 height: data.getUint16(28, true) & 0x3fff,
67 };
68 case "VP8L": {
69 if (bytes.length < 25) return null;
70 const packed = data.getUint32(21, true);
71 return { width: (packed & 0x3fff) + 1, height: ((packed >>> 14) & 0x3fff) + 1 };
72 }
73 case "VP8X":
74 if (bytes.length < 30) return null;
75 return {
76 width: (bytes[24]! | (bytes[25]! << 8) | (bytes[26]! << 16)) + 1,
77 height: (bytes[27]! | (bytes[28]! << 8) | (bytes[29]! << 16)) + 1,
78 };
79 default:
80 return null;
81 }
82}
83
84function readJpeg(bytes: Uint8Array): ImageDimensions | null {
85 if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return null;
86 const data = view(bytes);
87 let offset = 2;
88 let rotated = false;
89 while (offset + 9 <= bytes.length) {
90 if (bytes[offset] !== 0xff) return null;
91 const marker = bytes[offset + 1]!;
92 if (marker === 0xff) {
93 offset += 1;
94 continue;
95 }
96 // Start-of-frame markers carry the dimensions; skip the arithmetic-coding
97 // and Huffman-table markers that share the range.
98 if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
99 const height = data.getUint16(offset + 5);
100 const width = data.getUint16(offset + 7);
101 return rotated ? { width: height, height: width } : { width, height };
102 }
103 if (marker === 0xd9 || marker === 0xda) return null;
104 // TEM and the restart markers stand alone, with no length field.
105 if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
106 offset += 2;
107 continue;
108 }
109 const length = data.getUint16(offset + 2);
110 // Viewers apply the EXIF orientation before display, so a phone photo
111 // stored on its side takes the swapped size on screen.
112 if (marker === 0xe1 && !rotated) {
113 rotated = exifOrientationSwapsAxes(bytes, offset + 4, offset + 2 + length);
114 }
115 offset += 2 + length;
116 }
117 return null;
118}
119
120function exifOrientationSwapsAxes(bytes: Uint8Array, start: number, end: number): boolean {
121 end = Math.min(end, bytes.length);
122 if (end - start < 14 || String.fromCharCode(...bytes.subarray(start, start + 4)) !== "Exif") {
123 return false;
124 }
125 const tiff = start + 6;
126 const data = view(bytes);
127 const littleEndian = bytes[tiff] === 0x49 && bytes[tiff + 1] === 0x49;
128 if (!littleEndian && !(bytes[tiff] === 0x4d && bytes[tiff + 1] === 0x4d)) return false;
129 const ifd = tiff + data.getUint32(tiff + 4, littleEndian);
130 if (ifd + 2 > end) return false;
131 const entries = data.getUint16(ifd, littleEndian);
132 for (let i = 0; i < entries; i += 1) {
133 const entry = ifd + 2 + i * 12;
134 if (entry + 12 > end) return false;
135 if (data.getUint16(entry, littleEndian) === 0x0112) {
136 const orientation = data.getUint16(entry + 8, littleEndian);
137 return orientation >= 5 && orientation <= 8;
138 }
139 }
140 return false;
141}