UNPKG

image-size

Version:
81 lines (76 loc) 2.62 kB
'use strict'; // lib/utils/bit-reader.ts var BitReader = class { constructor(input, endianness) { this.input = input; this.endianness = endianness; // Skip the first 16 bits (2 bytes) of signature this.byteOffset = 2; this.bitOffset = 0; } /** Reads a specified number of bits, and move the offset */ getBits(length = 1) { let result = 0; let bitsRead = 0; while (bitsRead < length) { if (this.byteOffset >= this.input.length) { throw new Error("Reached end of input"); } const currentByte = this.input[this.byteOffset]; const bitsLeft = 8 - this.bitOffset; const bitsToRead = Math.min(length - bitsRead, bitsLeft); if (this.endianness === "little-endian") { const mask = (1 << bitsToRead) - 1; const bits = currentByte >> this.bitOffset & mask; result |= bits << bitsRead; } else { const mask = (1 << bitsToRead) - 1 << 8 - this.bitOffset - bitsToRead; const bits = (currentByte & mask) >> 8 - this.bitOffset - bitsToRead; result = result << bitsToRead | bits; } bitsRead += bitsToRead; this.bitOffset += bitsToRead; if (this.bitOffset === 8) { this.byteOffset++; this.bitOffset = 0; } } return result; } }; // lib/types/utils.ts new TextDecoder(); var toHexString = (input, start = 0, end = input.length) => input.slice(start, end).reduce((memo, i) => memo + `0${i.toString(16)}`.slice(-2), ""); // lib/types/jxl-stream.ts function calculateImageDimension(reader, isSmallImage) { if (isSmallImage) { return 8 * (1 + reader.getBits(5)); } const sizeClass = reader.getBits(2); const extraBits = [9, 13, 18, 30][sizeClass]; return 1 + reader.getBits(extraBits); } function calculateImageWidth(reader, isSmallImage, widthMode, height) { if (isSmallImage && widthMode === 0) { return 8 * (1 + reader.getBits(5)); } if (widthMode === 0) { return calculateImageDimension(reader, false); } const aspectRatios = [1, 1.2, 4 / 3, 1.5, 16 / 9, 5 / 4, 2]; return Math.floor(height * aspectRatios[widthMode - 1]); } var JXLStream = { validate: (input) => { return toHexString(input, 0, 2) === "ff0a"; }, calculate(input) { const reader = new BitReader(input, "little-endian"); const isSmallImage = reader.getBits(1) === 1; const height = calculateImageDimension(reader, isSmallImage); const widthMode = reader.getBits(3); const width = calculateImageWidth(reader, isSmallImage, widthMode, height); return { width, height }; } }; exports.JXLStream = JXLStream;