var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { _isLittleEndian, _mathClamp, _PdfBaseStream, _unreachable, FormatError } from '@syncfusion/ej2-pdf';
var _PdfColorPalette = /** @class */ (function () {
    function _PdfColorPalette(name, numComps) {
        this.name = name;
        this.numComps = numComps;
    }
    _PdfColorPalette.prototype._resizeRgbImage = function (src, dest, w1, h1, w2, h2, alpha01) {
        var components = 3;
        alpha01 = alpha01 !== 1 ? 0 : alpha01;
        var xRatio = w1 / w2;
        var yRatio = h1 / h2;
        var newIndex = 0;
        var oldIndex;
        var xScaled = new Uint16Array(w2);
        var w1Scanline = w1 * components;
        for (var i = 0; i < w2; i++) {
            xScaled[i] = Math.floor(i * xRatio) * components;
        }
        for (var i = 0; i < h2; i++) {
            var py = Math.floor(i * yRatio) * w1Scanline;
            for (var j = 0; j < w2; j++) {
                oldIndex = py + xScaled[j];
                dest[newIndex++] = src[oldIndex++];
                dest[newIndex++] = src[oldIndex++];
                dest[newIndex++] = src[oldIndex++];
                newIndex += alpha01;
            }
        }
    };
    _PdfColorPalette.prototype._isDefaultDecode = function (decode, numComps) {
        numComps = this.numComps;
        if (!Array.isArray(decode)) {
            return true;
        }
        if (numComps * 2 !== decode.length) {
            return true;
        }
        for (var i = 0, ii = decode.length; i < ii; i += 2) {
            if (decode[i] !== 0 || decode[i + 1] !== 1) {
                return false;
            }
        }
        return true;
    };
    _PdfColorPalette.prototype._getRgb = function (src, srcOffset) {
        var rgb = new Uint8ClampedArray(3);
        this._getRgbItem(src, srcOffset, rgb, 0);
        return rgb;
    };
    _PdfColorPalette.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        _unreachable('Should not call _PdfColorPalette._getRgbItem');
    };
    _PdfColorPalette.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, bits, // eslint-disable-line
    alpha01) {
        _unreachable('Should not call _PdfColorPalette._getRgbBuffer');
    };
    _PdfColorPalette.prototype._getOutputLength = function (inputLength, alpha01) {
        _unreachable('Should not call _PdfColorPalette._getOutputLength');
    };
    _PdfColorPalette.prototype._isPass = function (bits) {
        return false;
    };
    _PdfColorPalette.prototype._fillRgb = function (dest, originalWidth, originalHeight, width, height, // eslint-disable-line
    actualHeight, bpc, comps, alpha01) {
        return __awaiter(this, void 0, void 0, function () {
            var count, numComponentColors, needsResizing, rgbBuf, allColors, i, colorMap, destPos, i, key, rgbPos, i, key, destPos, rgbPos, i, ii;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        count = originalWidth * originalHeight;
                        numComponentColors = 1 << bpc;
                        needsResizing = originalHeight !== height || originalWidth !== width;
                        rgbBuf = null;
                        if (!this._isPass(bpc)) return [3 /*break*/, 1];
                        rgbBuf = comps;
                        return [3 /*break*/, 7];
                    case 1:
                        if (!(this.numComps === 1 && count > numComponentColors && this.name !== 'DeviceGray' && this.name !== 'DeviceRGB')) return [3 /*break*/, 3];
                        allColors = bpc <= 8 ? new Uint8Array(numComponentColors) : new Uint16Array(numComponentColors);
                        for (i = 0; i < numComponentColors; i++) {
                            allColors[i] = i;
                        }
                        colorMap = new Uint8ClampedArray(numComponentColors * 3);
                        return [4 /*yield*/, this._getRgbBuffer(allColors, 0, numComponentColors, colorMap, 0, bpc, 0)];
                    case 2:
                        _a.sent();
                        if (!needsResizing) {
                            destPos = 0;
                            for (i = 0; i < count; ++i) {
                                key = comps[i] * 3;
                                dest[destPos++] = colorMap[key];
                                dest[destPos++] = colorMap[key + 1];
                                dest[destPos++] = colorMap[key + 2];
                                destPos += alpha01;
                            }
                        }
                        else {
                            rgbBuf = new Uint8Array(count * 3);
                            rgbPos = 0;
                            for (i = 0; i < count; ++i) {
                                key = comps[i] * 3;
                                rgbBuf[rgbPos++] = colorMap[key];
                                rgbBuf[rgbPos++] = colorMap[key + 1];
                                rgbBuf[rgbPos++] = colorMap[key + 2];
                            }
                        }
                        return [3 /*break*/, 7];
                    case 3:
                        if (!!needsResizing) return [3 /*break*/, 5];
                        return [4 /*yield*/, this._getRgbBuffer(comps, 0, width * actualHeight, dest, 0, bpc, alpha01)];
                    case 4:
                        _a.sent();
                        return [3 /*break*/, 7];
                    case 5:
                        rgbBuf = new Uint8ClampedArray(count * 3);
                        return [4 /*yield*/, this._getRgbBuffer(comps, 0, count, rgbBuf, 0, bpc, 0)];
                    case 6:
                        _a.sent();
                        _a.label = 7;
                    case 7:
                        if (rgbBuf) {
                            if (needsResizing) {
                                this._resizeRgbImage(rgbBuf, dest, originalWidth, originalHeight, width, height, alpha01);
                            }
                            else {
                                destPos = 0;
                                rgbPos = 0;
                                for (i = 0, ii = width * actualHeight; i < ii; i++) {
                                    dest[destPos++] = rgbBuf[rgbPos++];
                                    dest[destPos++] = rgbBuf[rgbPos++];
                                    dest[destPos++] = rgbBuf[rgbPos++];
                                    destPos += alpha01;
                                }
                            }
                        }
                        return [2 /*return*/, dest];
                }
            });
        });
    };
    return _PdfColorPalette;
}());
export { _PdfColorPalette };
var _PdfAlternateCS = /** @class */ (function (_super) {
    __extends(_PdfAlternateCS, _super);
    function _PdfAlternateCS(numComps, base) {
        var _this = _super.call(this, 'Alternate', numComps) || this;
        _this.base = base;
        _this.tmpBuf = new Float32Array(base.numComps);
        return _this;
    }
    _PdfAlternateCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        var tmpBuf = this.tmpBuf; // eslint-disable-line
        this.base._getRgbItem(tmpBuf, 0, dest, destOffset);
    };
    _PdfAlternateCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, bits, alpha01) {
        var base = this.base; // eslint-disable-line
        var scale = 1 / ((1 << bits) - 1);
        var baseNumComps = base.numComps; // eslint-disable-line
        var usesZeroToOneRange = base.usesZeroToOneRange; // eslint-disable-line
        var isPass = (base._isPass(8) || !usesZeroToOneRange) && alpha01 === 0;
        var pos = isPass ? destOffset : 0;
        var baseBuf = isPass ? dest : new Uint8ClampedArray(baseNumComps * count); // eslint-disable-line;
        var numComps = this.numComps;
        var scaled = new Float32Array(numComps);
        var tinted = new Float32Array(baseNumComps);
        for (var i = 0; i < count; i++) {
            for (var j = 0; j < numComps; j++) {
                scaled[j] = src[srcOffset++] * scale;
            }
            if (usesZeroToOneRange) {
                for (var j = 0; j < baseNumComps; j++) {
                    baseBuf[pos++] = tinted[j] * 255;
                }
            }
            else {
                base._getRgbItem(tinted, 0, baseBuf, pos);
                pos += baseNumComps;
            }
        }
        if (!isPass) {
            base._getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01);
        }
    };
    return _PdfAlternateCS;
}(_PdfColorPalette));
export { _PdfAlternateCS };
var _PdfPatternCS = /** @class */ (function (_super) {
    __extends(_PdfPatternCS, _super);
    function _PdfPatternCS(baseCS) {
        var _this = _super.call(this, 'Pattern', null) || this;
        _this.base = baseCS;
        return _this;
    }
    _PdfPatternCS.prototype._isDefaultDecode = function (decodeMap, bpc) {
        throw new Error('PatternCS._isDefaultDecode should not be called.');
    };
    return _PdfPatternCS;
}(_PdfColorPalette));
export { _PdfPatternCS };
var _PdfIndexedCS = /** @class */ (function (_super) {
    __extends(_PdfIndexedCS, _super);
    function _PdfIndexedCS(base, highVal, lookup) {
        var _this = _super.call(this, 'Indexed', 1) || this;
        _this.base = base;
        _this.highVal = highVal;
        var length = base.numComps * (highVal + 1);
        _this.lookup = new Uint8Array(length);
        if (lookup instanceof _PdfBaseStream) {
            var bytes = lookup.getBytes(length);
            _this.lookup.set(bytes);
        }
        else if (typeof lookup === 'string') {
            for (var i = 0; i < length; ++i) {
                _this.lookup[i] = lookup.charCodeAt(i) & 0xff;
            }
        }
        else {
            throw new FormatError("IndexedCS - unrecognized lookup table: " + lookup);
        }
        return _this;
    }
    _PdfIndexedCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        var _a = this, base = _a.base, highVal = _a.highVal, lookup = _a.lookup;
        var start = _mathClamp(Math.round(src[srcOffset]), 0, highVal) * base.numComps;
        base._getRgbBuffer(lookup, start, 1, dest, destOffset, 8, 0);
    };
    _PdfIndexedCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, // eslint-disable-line
    bits, alpha01) {
        var _a = this, base = _a.base, highVal = _a.highVal, lookup = _a.lookup;
        var numComps = base.numComps;
        var outputDelta = base._getOutputLength(numComps, alpha01); // eslint-disable-line
        for (var i = 0; i < count; ++i) {
            var lookupPos = _mathClamp(Math.round(src[srcOffset++]), 0, highVal) * numComps;
            base._getRgbBuffer(lookup, lookupPos, 1, dest, destOffset, 8, alpha01);
            destOffset += outputDelta;
        }
    };
    _PdfIndexedCS.prototype._getOutputLength = function (inputLength, alpha01) {
        return this.base._getOutputLength(inputLength * this.base.numComps, alpha01);
    };
    _PdfIndexedCS.prototype._isDefaultDecode = function (decodeMap, bpc) {
        if (!Array.isArray(decodeMap)) {
            return true;
        }
        if (decodeMap.length !== 2) {
            return true;
        }
        if (!Number.isInteger(bpc) || bpc < 1) {
            return true;
        }
        return decodeMap[0] === 0 && decodeMap[1] === (1 << bpc) - 1;
    };
    return _PdfIndexedCS;
}(_PdfColorPalette));
export { _PdfIndexedCS };
var _PdfDeviceGrayCS = /** @class */ (function (_super) {
    __extends(_PdfDeviceGrayCS, _super);
    function _PdfDeviceGrayCS() {
        return _super.call(this, 'DeviceGray', 1) || this;
    }
    _PdfDeviceGrayCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        var c = src[srcOffset] * 255;
        dest[destOffset] = dest[destOffset + 1] = dest[destOffset + 2] = c;
    };
    _PdfDeviceGrayCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, // eslint-disable-line
    bits, alpha01) {
        var scale = 255 / ((1 << bits) - 1);
        var j = srcOffset;
        var q = destOffset;
        for (var i = 0; i < count; ++i) {
            var c = scale * src[j++];
            dest[q++] = c;
            dest[q++] = c;
            dest[q++] = c;
            q += alpha01;
        }
    };
    _PdfDeviceGrayCS.prototype._getOutputLength = function (inputLength, alpha01) {
        return inputLength * (3 + alpha01);
    };
    return _PdfDeviceGrayCS;
}(_PdfColorPalette));
export { _PdfDeviceGrayCS };
var _PdfDeviceRgbCS = /** @class */ (function (_super) {
    __extends(_PdfDeviceRgbCS, _super);
    function _PdfDeviceRgbCS() {
        return _super.call(this, 'DeviceRGB', 3) || this;
    }
    _PdfDeviceRgbCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        dest[destOffset] = src[srcOffset] * 255;
        dest[destOffset + 1] = src[srcOffset + 1] * 255;
        dest[destOffset + 2] = src[srcOffset + 2] * 255;
    };
    _PdfDeviceRgbCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, // eslint-disable-line
    bits, alpha01) {
        if (bits === 8 && alpha01 === 0) {
            dest.set(src.subarray(srcOffset, srcOffset + count * 3), destOffset);
            return;
        }
        var scale = 255 / ((1 << bits) - 1);
        var j = srcOffset;
        var q = destOffset;
        for (var i = 0; i < count; ++i) {
            dest[q++] = scale * src[j++];
            dest[q++] = scale * src[j++];
            dest[q++] = scale * src[j++];
            q += alpha01;
        }
    };
    _PdfDeviceRgbCS.prototype._getOutputLength = function (inputLength, alpha01) {
        return ((inputLength * (3 + alpha01)) / 3) | 0;
    };
    _PdfDeviceRgbCS.prototype._isPass = function (bits) {
        return bits === 8;
    };
    return _PdfDeviceRgbCS;
}(_PdfColorPalette));
export { _PdfDeviceRgbCS };
var _PdfDeviceRgbaCS = /** @class */ (function (_super) {
    __extends(_PdfDeviceRgbaCS, _super);
    function _PdfDeviceRgbaCS() {
        return _super.call(this, 'DeviceRGBA', 4) || this;
    }
    _PdfDeviceRgbaCS.prototype._resizeRgbaImage = function (src, dest, w1, h1, w2, h2, alpha01) {
        var xRatio = w1 / w2;
        var yRatio = h1 / h2;
        var newIndex = 0;
        var xScaled = new Uint16Array(w2);
        if (alpha01 === 1) {
            for (var i = 0; i < w2; i++) {
                xScaled[i] = Math.floor(i * xRatio);
            }
            var src32 = new Uint32Array(src.buffer);
            var dest32 = new Uint32Array(dest.buffer);
            var rgbMask = _isLittleEndian() ? 0x00ffffff : 0xffffff00; // eslint-disable-line
            for (var i = 0; i < h2; i++) {
                var buf = src32.subarray(Math.floor(i * yRatio) * w1); // eslint-disable-line
                for (var j = 0; j < w2; j++) {
                    dest32[newIndex++] |= buf[xScaled[j]] & rgbMask;
                }
            }
        }
        else {
            var components = 4;
            var w1Scanline = w1 * components;
            for (var i = 0; i < w2; i++) {
                xScaled[i] = Math.floor(i * xRatio) * components;
            }
            for (var i = 0; i < h2; i++) {
                var buf = src.subarray(Math.floor(i * yRatio) * w1Scanline); // eslint-disable-line
                for (var j = 0; j < w2; j++) {
                    var oldIndex = xScaled[j];
                    dest[newIndex++] = buf[oldIndex];
                    dest[newIndex++] = buf[oldIndex + 1];
                    dest[newIndex++] = buf[oldIndex + 2];
                }
            }
        }
    };
    _PdfDeviceRgbaCS.prototype._copyRgbaImage = function (src, dest, alpha01) {
        if (alpha01 === 1) {
            var src32 = new Uint32Array(src.buffer);
            var dest32 = new Uint32Array(dest.buffer);
            var rgbMask = _isLittleEndian() ? 0x00ffffff : 0xffffff00; // eslint-disable-line
            for (var i = 0, ii = src32.length; i < ii; i++) {
                dest32[i] |= src32[i] & rgbMask;
            }
        }
        else {
            var j = 0;
            for (var i = 0, ii = src.length; i < ii; i += 4) {
                dest[j++] = src[i];
                dest[j++] = src[i + 1];
                dest[j++] = src[i + 2];
            }
        }
    };
    _PdfDeviceRgbaCS.prototype._getOutputLength = function (inputLength, _alpha01) {
        return inputLength * 4;
    };
    _PdfDeviceRgbaCS.prototype._isPass = function (bits) {
        return bits === 8;
    };
    _PdfDeviceRgbaCS.prototype._fillRgb = function (dest, originalWidth, originalHeight, width, height, // eslint-disable-line
    actualHeight, bpc, comps, alpha01) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (originalHeight !== height || originalWidth !== width) {
                    this._resizeRgbaImage(comps, dest, originalWidth, originalHeight, width, height, alpha01);
                }
                else {
                    this._copyRgbaImage(comps, dest, alpha01);
                }
                return [2 /*return*/];
            });
        });
    };
    return _PdfDeviceRgbaCS;
}(_PdfColorPalette));
export { _PdfDeviceRgbaCS };
var _PdfDeviceCmykCS = /** @class */ (function (_super) {
    __extends(_PdfDeviceCmykCS, _super);
    function _PdfDeviceCmykCS() {
        return _super.call(this, 'DeviceCMYK', 4) || this;
    }
    _PdfDeviceCmykCS.prototype._toRgb = function (src, srcOffset, srcScale, dest, destOffset) {
        var c = src[srcOffset] * srcScale;
        var m = src[srcOffset + 1] * srcScale;
        var y = src[srcOffset + 2] * srcScale;
        var k = src[srcOffset + 3] * srcScale;
        dest[destOffset] = 255 + c * (-4.387332384609988 * c + 54.48615194189176 * m + 18.82290502165302 *
            y + 212.25662451639585 * k - 285.2331026137004) +
            m * (1.7149763477362134 * m - 5.6096736904047315 * y - 17.873870861415444 * k - 5.497006427196366) +
            y * (-2.5217340131683033 * y - 21.248923337353073 * k + 17.5119270841813) +
            k * (-21.86122147463605 * k - 189.48180835922747);
        dest[destOffset + 1] = 255 + c * (8.841041422036149 * c + 60.118027045597366 * m + 6.871425592049007 * y + 31.159100130055922 *
            k - 79.2970844816548) +
            m * (-15.310361306967817 * m + 17.575251261109482 * y + 131.35250912493976 * k - 190.9453302588951) +
            y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) +
            k * (-20.737325471181034 * k - 187.80453709719578);
        dest[destOffset + 2] = 255 + c * (0.8842522430003296 * c + 8.078677503112928 * m + 30.89978309703729 * y - 0.23883238689178934 *
            k - 14.183576799673286) +
            m * (10.49593273432072 * m + 63.02378494754052 * y + 50.606957656360734 * k - 112.23884253719248) +
            y * (0.03296041114873217 * y + 115.60384449646641 * k - 193.58209356861505) +
            k * (-22.33816807309886 * k - 180.12613974708367);
    };
    _PdfDeviceCmykCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        this._toRgb(src, srcOffset, 1, dest, destOffset);
    };
    _PdfDeviceCmykCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, // eslint-disable-line
    bits, alpha01) {
        var scale = 1 / ((1 << bits) - 1);
        for (var i = 0; i < count; i++) {
            this._toRgb(src, srcOffset, scale, dest, destOffset);
            srcOffset += 4;
            destOffset += 3 + alpha01;
        }
    };
    _PdfDeviceCmykCS.prototype._getOutputLength = function (inputLength, alpha01) {
        return ((inputLength / 4) * (3 + alpha01)) | 0;
    };
    return _PdfDeviceCmykCS;
}(_PdfColorPalette));
export { _PdfDeviceCmykCS };
var _PdfLabCS = /** @class */ (function (_super) {
    __extends(_PdfLabCS, _super);
    function _PdfLabCS(whitePoint, blackPoint, range) {
        var _a, _b;
        var _this = _super.call(this, 'Lab', 3) || this;
        if (!whitePoint) {
            throw new FormatError('WhitePoint missing - required for color space Lab');
        }
        _this._xw = whitePoint[0], _this._yw = whitePoint[1], _this._zw = whitePoint[2];
        _a = range || [-100, 100, -100, 100], _this._amin = _a[0], _this._amax = _a[1], _this._bmin = _a[2], _this._bmax = _a[3];
        _b = blackPoint || [0, 0, 0], _this._xb = _b[0], _this._yb = _b[1], _this._zb = _b[2];
        if (_this._xw < 0 || _this._zw < 0 || _this._yw !== 1) {
            throw new FormatError('Invalid WhitePoint components, no fallback available');
        }
        if (_this._xb < 0 || _this._yb < 0 || _this._zb < 0) {
            _this._xb = _this._yb = _this._zb = 0;
        }
        if (_this._amin > _this._amax || _this._bmin > _this._bmax) {
            _this._amin = -100;
            _this._amax = 100;
            _this._bmin = -100;
            _this._bmax = 100;
        }
        return _this;
    }
    _PdfLabCS.prototype._fng = function (x) {
        return x >= 6 / 29 ? Math.pow(x, 3) : (108 / 841) * (x - 4 / 29);
    };
    _PdfLabCS.prototype._decode = function (value, high1, low2, high2) {
        return low2 + (value * (high2 - low2)) / high1;
    };
    _PdfLabCS.prototype._toRgb = function (src, srcOffset, maxVal, dest, destOffset) {
        var ls = src[srcOffset];
        var as = src[srcOffset + 1];
        var bs = src[srcOffset + 2];
        if (maxVal !== false) {
            ls = this._decode(ls, maxVal, 0, 100);
            as = this._decode(as, maxVal, this._amin, this._amax);
            bs = this._decode(bs, maxVal, this._bmin, this._bmax);
        }
        if (as > this._amax) {
            as = this._amax;
        }
        else if (as < this._amin) {
            as = this._amin;
        }
        if (bs > this._bmax) {
            bs = this._bmax;
        }
        else if (bs < this._bmin) {
            bs = this._bmin;
        }
        var m = (ls + 16) / 116;
        var l = m + as / 500;
        var n = m - bs / 200;
        var x = this._xw * this._fng(l);
        var y = this._yw * this._fng(m);
        var z = this._zw * this._fng(n);
        var r;
        var g;
        var b;
        if (this._zw < 1) {
            r = x * 3.1339 + y * -1.617 + z * -0.4906;
            g = x * -0.9785 + y * 1.916 + z * 0.0333;
            b = x * 0.072 + y * -0.229 + z * 1.4057;
        }
        else {
            r = x * 3.2406 + y * -1.5372 + z * -0.4986;
            g = x * -0.9689 + y * 1.8758 + z * 0.0415;
            b = x * 0.0557 + y * -0.204 + z * 1.057;
        }
        dest[destOffset] = Math.sqrt(r) * 255;
        dest[destOffset + 1] = Math.sqrt(g) * 255;
        dest[destOffset + 2] = Math.sqrt(b) * 255;
    };
    _PdfLabCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        this._toRgb(src, srcOffset, false, dest, destOffset);
    };
    _PdfLabCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, // eslint-disable-line
    bits, alpha01) {
        var maxVal = (1 << bits) - 1;
        for (var i = 0; i < count; i++) {
            this._toRgb(src, srcOffset, maxVal, dest, destOffset);
            srcOffset += 3;
            destOffset += 3 + alpha01;
        }
    };
    _PdfLabCS.prototype._getOutputLength = function (inputLength, alpha01) {
        return ((inputLength * (3 + alpha01)) / 3) | 0;
    };
    _PdfLabCS.prototype._isDefaultDecode = function (decodeMap, bpc) {
        return true;
    };
    Object.defineProperty(_PdfLabCS.prototype, "usesZeroToOneRange", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    return _PdfLabCS;
}(_PdfColorPalette));
export { _PdfLabCS };
var _PdfCalGrayCS = /** @class */ (function (_super) {
    __extends(_PdfCalGrayCS, _super);
    function _PdfCalGrayCS(whitePoint, blackPoint, gamma) {
        var _a;
        var _this = _super.call(this, 'CalGray', 1) || this;
        if (!whitePoint) {
            throw new FormatError('WhitePoint missing - required for color space CalGray');
        }
        _this._xw = whitePoint[0], _this._yw = whitePoint[1], _this._zw = whitePoint[2];
        _a = blackPoint || [0, 0, 0], _this._xb = _a[0], _this._yb = _a[1], _this._zb = _a[2];
        _this._g = gamma || 1;
        if (_this._xw < 0 || _this._zw < 0 || _this._yw !== 1) {
            throw new FormatError("Invalid WhitePoint components for " + _this.name + ", no fallback available");
        }
        if (_this._xb < 0 || _this._yb < 0 || _this._zb < 0) {
            _this._xb = _this._yb = _this._zb = 0;
        }
        if (_this._g < 1) {
            _this._g = 1;
        }
        return _this;
    }
    _PdfCalGrayCS.prototype._toRgb = function (src, srcOffset, dest, destOffset, scale) {
        var a = src[srcOffset] * scale;
        var ag = Math.pow(a, this._g);
        var l = this._yw * ag;
        var val = Math.max(295.8 * Math.pow(l, 0.3333333333333333) - 40.8, 0);
        dest[destOffset] = val;
        dest[destOffset + 1] = val;
        dest[destOffset + 2] = val;
    };
    _PdfCalGrayCS.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        this._toRgb(src, srcOffset, dest, destOffset, 1);
    };
    _PdfCalGrayCS.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, bits, alpha01) {
        var scale = 1 / ((1 << bits) - 1);
        for (var i = 0; i < count; ++i) {
            this._toRgb(src, srcOffset, dest, destOffset, scale);
            srcOffset += 1;
            destOffset += 3 + alpha01;
        }
    };
    _PdfCalGrayCS.prototype._getOutputLength = function (inputLength, alpha01) {
        return inputLength * (3 + alpha01);
    };
    return _PdfCalGrayCS;
}(_PdfColorPalette));
export { _PdfCalGrayCS };
var _PdfColorRgbConverter = /** @class */ (function (_super) {
    __extends(_PdfColorRgbConverter, _super);
    function _PdfColorRgbConverter(whitePoint, blackPoint, gamma, matrix) {
        var _a, _b;
        var _this = _super.call(this, 'CalRGB', 3) || this;
        _this.scaleMatrix = new Float32Array([
            0.8951, 0.2664, -0.1614,
            -0.7502, 1.7135, 0.0367,
            0.0389, -0.0685, 1.0296
        ]);
        _this.inverseMatrix = new Float32Array([
            0.9869929, -0.1470543, 0.1599627,
            0.4323053, 0.5183603, 0.0492912,
            -0.0085287, 0.0400428, 0.9684867
        ]);
        _this.rgbMatrix = new Float32Array([
            3.2404542, -1.5371385, -0.4985314,
            -0.9692660, 1.8760108, 0.0415560,
            0.0556434, -0.2040259, 1.0572252
        ]);
        _this.whitePointMatrix = new Float32Array([1, 1, 1]);
        _this.tempNormalizeMatrix = new Float32Array(3);
        _this.tempConvertMatrix1 = new Float32Array(3);
        _this.tempConvertMatrix2 = new Float32Array(3);
        _this.decodeConstant = Math.pow(((8 + 16) / 116), 3) / 8.0;
        if (!whitePoint) {
            throw new FormatError('WhitePoint missing - required for color space CalRGB');
        }
        var _c = (_this._whitePoint = whitePoint), xw = _c[0], yw = _c[1], zw = _c[2];
        var _d = (_this._blackPoint = blackPoint || new Float32Array(3)), xb = _d[0], yb = _d[1], zb = _d[2];
        _a = gamma || new Float32Array([1, 1, 1]), _this.gr = _a[0], _this.gg = _a[1], _this.gb = _a[2];
        _b = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]), _this.mxa = _b[0], _this.mya = _b[1], _this.mza = _b[2], _this.mxb = _b[3], _this.myb = _b[4], _this.mzb = _b[5], _this.mxc = _b[6], _this.myc = _b[7], _this.mzc = _b[8];
        if (xw < 0 || zw < 0 || yw !== 1) {
            throw new FormatError("Invalid WhitePoint components for " + _this.name + ", no fallback available");
        }
        if (xb < 0 || yb < 0 || zb < 0) {
            _this._blackPoint = new Float32Array(3);
        }
        if (_this.gr < 0 || _this.gg < 0 || _this.gb < 0) {
            _this.gr = _this.gg = _this.gb = 1;
        }
        return _this;
    }
    _PdfColorRgbConverter.prototype._matrixProduct = function (a, b, result) {
        result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
        result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
        result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
    };
    _PdfColorRgbConverter.prototype._toFlat = function (sourceWhitePoint, lms, result) {
        result[0] = (lms[0] * 1) / sourceWhitePoint[0];
        result[1] = (lms[1] * 1) / sourceWhitePoint[1];
        result[2] = (lms[2] * 1) / sourceWhitePoint[2];
    };
    _PdfColorRgbConverter.prototype._toD65 = function (sourceWhitePoint, lms, result) {
        var d65X = 0.95047;
        var d65Y = 1;
        var d65Z = 1.08883;
        result[0] = (lms[0] * d65X) / sourceWhitePoint[0];
        result[1] = (lms[1] * d65Y) / sourceWhitePoint[1];
        result[2] = (lms[2] * d65Z) / sourceWhitePoint[2];
    };
    _PdfColorRgbConverter.prototype._srgbTransferFunction = function (color) {
        if (color <= 0.0031308) {
            return _mathClamp(12.92 * color, 0, 1);
        }
        if (color >= 0.99554525) {
            return 1;
        }
        return _mathClamp((1 + 0.055) * Math.pow(color, (1 / 2.4)) - 0.055, 0, 1);
    };
    _PdfColorRgbConverter.prototype._decodeL = function (L) {
        if (L < 0) {
            return -this._decodeL(-L);
        }
        if (L > 8.0) {
            return Math.pow(((L + 16) / 116), 3);
        }
        return L * this.decodeConstant;
    };
    _PdfColorRgbConverter.prototype._compensateBlackPoint = function (sourceBlackPoint, xyzFlat, result) {
        if (sourceBlackPoint[0] === 0 && sourceBlackPoint[1] === 0 && sourceBlackPoint[2] === 0) {
            result[0] = xyzFlat[0];
            result[1] = xyzFlat[1];
            result[2] = xyzFlat[2];
            return;
        }
        var zeroDecodeL = this._decodeL(0);
        var xdST = zeroDecodeL;
        var xsRC = this._decodeL(sourceBlackPoint[0]);
        var ydST = zeroDecodeL;
        var ysRC = this._decodeL(sourceBlackPoint[1]);
        var zdST = zeroDecodeL;
        var zsRC = this._decodeL(sourceBlackPoint[2]);
        var xScale = (1 - xdST) / (1 - xsRC);
        var xOffset = 1 - xScale;
        var yScale = (1 - ydST) / (1 - ysRC);
        var yOffset = 1 - yScale;
        var zScale = (1 - zdST) / (1 - zsRC);
        var zOffset = 1 - zScale;
        result[0] = xyzFlat[0] * xScale + xOffset;
        result[1] = xyzFlat[1] * yScale + yOffset;
        result[2] = xyzFlat[2] * zScale + zOffset;
    };
    _PdfColorRgbConverter.prototype._normalizeWhitePointToFlat = function (sourceWhitePoint, xyzIn, result) {
        if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {
            result[0] = xyzIn[0];
            result[1] = xyzIn[1];
            result[2] = xyzIn[2];
            return;
        }
        var lms = result; // eslint-disable-line
        this._matrixProduct(this.scaleMatrix, xyzIn, lms);
        var lmsFlat = this.tempNormalizeMatrix; // eslint-disable-line
        this._toFlat(sourceWhitePoint, lms, lmsFlat);
        this._matrixProduct(this.inverseMatrix, lmsFlat, result);
    };
    _PdfColorRgbConverter.prototype._normalizeWhitePointToD65 = function (sourceWhitePoint, xyzIn, result) {
        var lms = result; // eslint-disable-line
        this._matrixProduct(this.scaleMatrix, xyzIn, lms);
        var lmsD65 = this.tempNormalizeMatrix; // eslint-disable-line
        this._toD65(sourceWhitePoint, lms, lmsD65);
        this._matrixProduct(this.inverseMatrix, lmsD65, result);
    };
    _PdfColorRgbConverter.prototype._toRgb = function (src, srcOffset, dest, destOffset, scale) {
        var a = _mathClamp(src[srcOffset] * scale, 0, 1);
        var b = _mathClamp(src[srcOffset + 1] * scale, 0, 1);
        var c = _mathClamp(src[srcOffset + 2] * scale, 0, 1);
        var agr = a === 1 ? 1 : Math.pow(a, this.gr);
        var bgg = b === 1 ? 1 : Math.pow(b, this.gg);
        var cgb = c === 1 ? 1 : Math.pow(c, this.gb);
        var x = this.mxa * agr + this.mxb * bgg + this.mxc * cgb;
        var y = this.mya * agr + this.myb * bgg + this.myc * cgb;
        var z = this.mza * agr + this.mzb * bgg + this.mzc * cgb;
        var xyz = this.tempConvertMatrix1;
        xyz[0] = x;
        xyz[1] = y;
        xyz[2] = z;
        var xyzFlat = this.tempConvertMatrix2;
        this._normalizeWhitePointToFlat(this._whitePoint, xyz, xyzFlat);
        var xyzBlack = this.tempConvertMatrix1;
        this._compensateBlackPoint(this._blackPoint, xyzFlat, xyzFlat);
        var xyzD65 = this.tempConvertMatrix2;
        this._normalizeWhitePointToD65(this.whitePointMatrix, xyzBlack, xyzD65);
        var srgb = this.tempConvertMatrix1; // eslint-disable-line
        this._matrixProduct(this.rgbMatrix, xyzD65, srgb);
        dest[destOffset] = this._srgbTransferFunction(srgb[0]) * 255;
        dest[destOffset + 1] = this._srgbTransferFunction(srgb[1]) * 255;
        dest[destOffset + 2] = this._srgbTransferFunction(srgb[2]) * 255;
    };
    _PdfColorRgbConverter.prototype._getRgbItem = function (src, srcOffset, dest, destOffset) {
        this._toRgb(src, srcOffset, dest, destOffset, 1);
    };
    _PdfColorRgbConverter.prototype._getRgbBuffer = function (src, srcOffset, count, dest, destOffset, bits, alpha01) {
        var scale = 1 / ((1 << bits) - 1);
        for (var i = 0; i < count; ++i) {
            this._toRgb(src, srcOffset, dest, destOffset, scale);
            srcOffset += 3;
            destOffset += 3 + alpha01;
        }
    };
    _PdfColorRgbConverter.prototype._getOutputLength = function (inputLength, alpha01) {
        return ((inputLength * (3 + alpha01)) / 3) | 0;
    };
    return _PdfColorRgbConverter;
}(_PdfColorPalette));
export { _PdfColorRgbConverter };
