Skip to content
Source
Sign inSign up
Open History to see the last commit

image-bitmap.ts

20 lines947 Bytes

1import {
2 IMAGE_DIMENSIONS_HEADER_BYTES,
3 readImageDimensions,
4} from "@t3tools/shared/imageDimensions";
5​
6export async function officeImageBitmap(blob: Blob) {
7 const bytes = new Uint8Array(await blob.slice(0, IMAGE_DIMENSIONS_HEADER_BYTES).arrayBuffer());
8 let size = readImageDimensions(bytes);
9 if (!size && bytes.length >= 26 && bytes[0] === 0x42 && bytes[1] === 0x4d) {
10 const view = new DataView(bytes.buffer);
11 size =
12 view.getUint32(14, true) === 12
13 ? { width: view.getUint16(18, true), height: view.getUint16(20, true) }
14 : { width: view.getInt32(18, true), height: Math.abs(view.getInt32(22, true)) };
15 }
16 // Unknown dimensions cannot be decoded safely: compressed bytes do not bound pixel memory.
17 if (!size || size.width <= 0 || size.height <= 0 || size.width > 16_777_216 / size.height)
18 throw new Error("This embedded image exceeds the preview pixel limit.");
19 return createImageBitmap(blob);