@khoohaoyit/image-size
Version:
A stream based image size extractor in Node
67 lines (66 loc) • 3.39 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractJp2Size = void 0;
exports.extractJp2Size = function (stream) {
return __asyncGenerator(this, arguments, function* () {
const signatureLength = yield __await(stream.readUInt32BE(0));
const signature = yield __await(stream.readString(4, 4));
if (signature !== BoxTypes.jp__ || signatureLength < 1)
return yield __await(void 0);
const ftypeBoxStart = signatureLength + 4;
const ftypBoxLength = yield __await(stream.readUInt32BE(signatureLength));
if ((yield __await(stream.readString(ftypeBoxStart, 4))) !== BoxTypes.ftyp)
return yield __await(void 0);
let ihdrOffset = signatureLength + 4 + ftypBoxLength;
switch (yield __await(stream.readString(ihdrOffset, 4))) {
default:
return yield __await(void 0);
case BoxTypes.rreq:
{
ihdrOffset += 4;
const unit = yield __await(stream.readUint8(ihdrOffset));
let offset = 1 + 2 * unit;
const numStdFlags = yield __await(stream.readUInt16BE(ihdrOffset + offset));
const flagsLength = numStdFlags * (2 + unit);
offset += 2 + flagsLength;
const numVendorFeatures = yield __await(stream.readUInt16BE(ihdrOffset + offset));
const featuresLength = numVendorFeatures * (16 + unit);
const rreqLength = offset + 2 + featuresLength;
ihdrOffset += 4 + rreqLength;
ihdrOffset += 8;
}
break;
case BoxTypes.jp2h:
{
ihdrOffset += 8;
}
break;
}
return yield __await(yield yield __await({
height: yield __await(stream.readUint32BE(ihdrOffset + 4)),
width: yield __await(stream.readUint32BE(ihdrOffset + 8)),
}));
});
};
var BoxTypes;
(function (BoxTypes) {
BoxTypes["ftyp"] = "ftyp";
BoxTypes["ihdr"] = "ihdr";
BoxTypes["jp2h"] = "jp2h";
BoxTypes["jp__"] = "jP ";
BoxTypes["rreq"] = "rreq";
BoxTypes["xml_"] = "xml ";
})(BoxTypes || (BoxTypes = {}));
;