UNPKG

yuv-rx

Version:

Reading videos as Rx Observables of decoded frames

135 lines (134 loc) 5.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InterleavedPlane = exports.Gray = exports.parseRational = void 0; function _parseRational(str, separator) { const parts = str.split(separator); if (parts.length == 2) { return { numerator: parseInt(parts[0]), denominator: parseInt(parts[1]) }; } else { return null; } } function parseRational(str) { var _a, _b; return (_b = (_a = _parseRational(str, ':')) !== null && _a !== void 0 ? _a : _parseRational(str, ':')) !== null && _b !== void 0 ? _b : (() => { throw `${str} is not a valid rational number`; })(); } exports.parseRational = parseRational; class Gray { constructor(width, height, data) { var _a; this.data = (_a = data === null || data === void 0 ? void 0 : data.subarray()) !== null && _a !== void 0 ? _a : Buffer.allocUnsafe(width * height); if (width * height > this.data.length) { throw new Error(`Invalid buffer size ${this.data.length} for dimensions ${width}x${height}`); } this.width = width; this.height = height; } getPixel(x, y) { return this.data.readUInt8(y * this.width + x); } putPixel(x, y, pix) { this.data.writeUint8(pix, y * this.width + x); } getWidth() { return this.width; } getHeight() { return this.height; } getBounds() { return { x: 0, y: 0, width: this.width, height: this.height }; } getDimension() { return { width: this.width, height: this.height }; } getSubimage(r, dest) { if (r == this.getBounds()) { return this; } else { dest = dest !== null && dest !== void 0 ? dest : Buffer.allocUnsafe(r.width * r.height); if (dest.length < r.width * r.height) { throw new Error(`buffer capacity (${dest.length}) < requested image size (${r.width * r.height})`); } if (r.x + r.width > this.width || r.y + r.height > this.height) { throw new Error(`image: ${this.width}x${this.height} subimage: ${r.x}:${r.y} ${r.width}x${r.height}`); } let offset = r.y * this.width + r.x; for (let i = 0; i < r.height; i++) { dest.fill(this.data.subarray(offset, offset + r.width), i * r.width, (i + 1) * r.width); offset += this.width; } return new Gray(r.width, r.height, dest); } } } exports.Gray = Gray; class InterleavedPlane { constructor(width, height, colorComponents, data) { var _a; this.data = (_a = data === null || data === void 0 ? void 0 : data.subarray()) !== null && _a !== void 0 ? _a : Buffer.allocUnsafe(width * height * colorComponents.length); if (width * height * colorComponents.length > this.data.length) { throw new Error(`Invalid buffer size ${this.data.length} for dimensions ${width}x${height} and ${colorComponents.length} color components`); } this.width = width; this.height = height; this.colorComponents = colorComponents; this.stride = width * colorComponents.length; } colorComponentIdx(colorComponent) { const idx = this.colorComponents.indexOf(colorComponent); if (idx == -1) { throw `Color component ${colorComponent} not found. Existing components: ${this.colorComponents}`; } else { return idx; } } getPixel(x, y, colorComponent) { const componentIdx = this.colorComponentIdx(colorComponent); return this.data.readUInt8(y * this.stride + x * this.colorComponents.length + componentIdx); } putPixel(x, y, colorComponent, pix) { const componentIdx = this.colorComponentIdx(colorComponent); this.data.writeUint8(pix, y * this.stride + x * this.colorComponents.length + componentIdx); } getWidth() { return this.width; } getHeight() { return this.height; } getBounds() { return { x: 0, y: 0, width: this.width, height: this.height }; } getDimension() { return { width: this.width, height: this.height }; } getSubimage(r, dest) { if (r == this.getBounds()) { return this; } else { const comps = this.colorComponents.length; dest = dest !== null && dest !== void 0 ? dest : Buffer.allocUnsafe(r.width * r.height * comps); if (dest.length < r.width * r.height * comps) { throw new Error(`buffer capacity (${dest.length}) < requested image size (${r.width * r.height * comps})`); } if (r.x + r.width > this.width || r.y + r.height > this.height) { throw new Error(`image: ${this.width}x${this.height} subimage: ${r.x}:${r.y} ${r.width}x${r.height}`); } let offset = r.y * this.stride + r.x * comps; for (let i = 0; i < r.height; i++) { dest.fill(this.data.subarray(offset, offset + r.width * comps), i * r.width * comps, (i + 1) * r.width * comps); offset += this.stride; } return new InterleavedPlane(r.width, r.height, this.colorComponents, dest); } } } exports.InterleavedPlane = InterleavedPlane;