UNPKG

image-size

Version:
42 lines (38 loc) 1.18 kB
'use strict'; // lib/types/utils.ts var decoder = new TextDecoder(); var toUTF8String = (input, start = 0, end = input.length) => decoder.decode(input.slice(start, end)); var getView = (input, offset) => new DataView(input.buffer, input.byteOffset + offset); var readUInt32BE = (input, offset = 0) => getView(input, offset).getUint32(0, false); // lib/types/png.ts var pngSignature = "PNG\r\n\n"; var pngImageHeaderChunkName = "IHDR"; var pngFriedChunkName = "CgBI"; var PNG = { validate(input) { if (pngSignature === toUTF8String(input, 1, 8)) { let chunkName = toUTF8String(input, 12, 16); if (chunkName === pngFriedChunkName) { chunkName = toUTF8String(input, 28, 32); } if (chunkName !== pngImageHeaderChunkName) { throw new TypeError("Invalid PNG"); } return true; } return false; }, calculate(input) { if (toUTF8String(input, 12, 16) === pngFriedChunkName) { return { height: readUInt32BE(input, 36), width: readUInt32BE(input, 32) }; } return { height: readUInt32BE(input, 20), width: readUInt32BE(input, 16) }; } }; exports.PNG = PNG;