image-dimensions.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. export interface ImageDimensions {
  2. width: number;
  3. height: number;
  4. }
  5. export const IMAGE_DIMENSION_HEADER_BYTES = 96 * 1024;
  6. const MAX_MEDIA_DIMENSION = 1_000_000;
  7. function normalizeDimensions(width: number, height: number): ImageDimensions | undefined {
  8. if (
  9. Number.isFinite(width) &&
  10. Number.isFinite(height) &&
  11. width > 0 &&
  12. height > 0 &&
  13. width <= MAX_MEDIA_DIMENSION &&
  14. height <= MAX_MEDIA_DIMENSION
  15. ) {
  16. return { width, height };
  17. }
  18. return undefined;
  19. }
  20. function readAscii(bytes: Uint8Array, offset: number, length: number): string | undefined {
  21. if (offset < 0 || length < 0 || bytes.byteLength < offset + length) return undefined;
  22. let value = '';
  23. for (let index = offset; index < offset + length; index += 1) {
  24. value += String.fromCharCode(bytes[index] as number);
  25. }
  26. return value;
  27. }
  28. function readUint16(bytes: Uint8Array, offset: number, littleEndian: boolean): number | undefined {
  29. if (offset < 0 || bytes.byteLength < offset + 2) return undefined;
  30. return new DataView(bytes.buffer, bytes.byteOffset + offset, 2).getUint16(0, littleEndian);
  31. }
  32. function readUint24LE(bytes: Uint8Array, offset: number): number | undefined {
  33. if (offset < 0 || bytes.byteLength < offset + 3) return undefined;
  34. return (
  35. (bytes[offset] as number) |
  36. ((bytes[offset + 1] as number) << 8) |
  37. ((bytes[offset + 2] as number) << 16)
  38. );
  39. }
  40. function readUint32BE(bytes: Uint8Array, offset: number): number | undefined {
  41. if (offset < 0 || bytes.byteLength < offset + 4) return undefined;
  42. return new DataView(bytes.buffer, bytes.byteOffset + offset, 4).getUint32(0);
  43. }
  44. function parsePng(bytes: Uint8Array): ImageDimensions | undefined {
  45. if (
  46. bytes.byteLength < 24 ||
  47. readAscii(bytes, 0, 8) !== '\u0089PNG\r\n\u001a\n' ||
  48. readAscii(bytes, 12, 4) !== 'IHDR'
  49. ) {
  50. return undefined;
  51. }
  52. const width = readUint32BE(bytes, 16);
  53. const height = readUint32BE(bytes, 20);
  54. return width !== undefined && height !== undefined
  55. ? normalizeDimensions(width, height)
  56. : undefined;
  57. }
  58. function parseGif(bytes: Uint8Array): ImageDimensions | undefined {
  59. const signature = readAscii(bytes, 0, 6);
  60. if (signature !== 'GIF87a' && signature !== 'GIF89a') return undefined;
  61. const width = readUint16(bytes, 6, true);
  62. const height = readUint16(bytes, 8, true);
  63. return width !== undefined && height !== undefined
  64. ? normalizeDimensions(width, height)
  65. : undefined;
  66. }
  67. function parseWebp(bytes: Uint8Array): ImageDimensions | undefined {
  68. if (
  69. bytes.byteLength < 30 ||
  70. readAscii(bytes, 0, 4) !== 'RIFF' ||
  71. readAscii(bytes, 8, 4) !== 'WEBP'
  72. ) {
  73. return undefined;
  74. }
  75. const format = readAscii(bytes, 12, 4);
  76. if (format === 'VP8 ') {
  77. if (bytes[23] !== 0x9d || bytes[24] !== 0x01 || bytes[25] !== 0x2a) return undefined;
  78. const encodedWidth = readUint16(bytes, 26, true);
  79. const encodedHeight = readUint16(bytes, 28, true);
  80. return encodedWidth !== undefined && encodedHeight !== undefined
  81. ? normalizeDimensions(encodedWidth & 0x3fff, encodedHeight & 0x3fff)
  82. : undefined;
  83. }
  84. if (format === 'VP8L') {
  85. if (bytes[20] !== 0x2f) return undefined;
  86. const b0 = bytes[21] as number;
  87. const b1 = bytes[22] as number;
  88. const b2 = bytes[23] as number;
  89. const b3 = bytes[24] as number;
  90. return normalizeDimensions(
  91. 1 + (((b1 & 0x3f) << 8) | b0),
  92. 1 + (((b3 & 0x0f) << 10) | (b2 << 2) | ((b1 & 0xc0) >> 6)),
  93. );
  94. }
  95. if (format === 'VP8X') {
  96. const encodedWidth = readUint24LE(bytes, 24);
  97. const encodedHeight = readUint24LE(bytes, 27);
  98. return encodedWidth !== undefined && encodedHeight !== undefined
  99. ? normalizeDimensions(1 + encodedWidth, 1 + encodedHeight)
  100. : undefined;
  101. }
  102. return undefined;
  103. }
  104. const JPEG_STANDALONE_MARKERS = new Set([
  105. 0x01, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
  106. ]);
  107. function isJpegSofMarker(marker: number): boolean {
  108. return marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc;
  109. }
  110. function parseJpeg(bytes: Uint8Array): ImageDimensions | undefined {
  111. if (bytes.byteLength < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return undefined;
  112. let offset = 2;
  113. for (
  114. let iterations = 0;
  115. offset + 4 <= bytes.byteLength && iterations < bytes.byteLength;
  116. iterations += 1
  117. ) {
  118. if (bytes[offset] !== 0xff) {
  119. offset += 1;
  120. continue;
  121. }
  122. const marker = bytes[offset + 1] as number;
  123. if (marker === 0xff) {
  124. offset += 1;
  125. continue;
  126. }
  127. if (JPEG_STANDALONE_MARKERS.has(marker)) {
  128. offset += 2;
  129. continue;
  130. }
  131. const length = readUint16(bytes, offset + 2, false);
  132. if (length === undefined || length < 2) return undefined;
  133. if (isJpegSofMarker(marker)) {
  134. const height = readUint16(bytes, offset + 5, false);
  135. const width = readUint16(bytes, offset + 7, false);
  136. return width !== undefined && height !== undefined
  137. ? normalizeDimensions(width, height)
  138. : undefined;
  139. }
  140. offset += 2 + length;
  141. }
  142. return undefined;
  143. }
  144. /** Parse PNG, JPEG, GIF, or WebP dimensions from a bounded file prefix. */
  145. export function parseImageDimensionsFromBytes(bytes: Uint8Array): ImageDimensions | undefined {
  146. return parsePng(bytes) ?? parseJpeg(bytes) ?? parseGif(bytes) ?? parseWebp(bytes);
  147. }