all files / core/graphics/images/ colorspace-utils.js

11.61% Statements 13/112
0% Branches 0/66
20% Functions 2/10
11.61% Lines 13/112
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                                                                                                                                                                                                                                                                                                                                      
define(["require", "exports", "./colorspace", "../../pdf-primitives", "../../utils"], function (require, exports, colorspace_1, pdf_primitives_1, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfColorSpaceUtils = (function () {
        function _PdfColorSpaceUtils() {
        }
        _PdfColorSpaceUtils.prototype._parse = function (colorSpace, xref, resources, pdfFunctionFactory, globalColorSpaceCache, localColorSpaceCache, asyncIfNotCached) {
            var options = { xref: xref, resources: resources, pdfFunctionFactory: pdfFunctionFactory, globalColorSpaceCache: globalColorSpaceCache, localColorSpaceCache: localColorSpaceCache };
            var csRef;
            if (colorSpace instanceof pdf_primitives_1._PdfReference) {
                csRef = colorSpace;
                var cachedCS = globalColorSpaceCache && globalColorSpaceCache.get(csRef) ||
                    localColorSpaceCache && localColorSpaceCache.get(csRef);
                if (cachedCS) {
                    return cachedCS;
                }
                colorSpace = xref._fetch(colorSpace);
            }
            var parsedCS = this._parseColorspace(colorSpace, options);
            return asyncIfNotCached ? Promise.resolve(parsedCS) : parsedCS;
        };
        _PdfColorSpaceUtils.prototype._subParse = function (cs, options) {
            var globalColorSpaceCache = options.globalColorSpaceCache;
            var csRef;
            if (cs instanceof pdf_primitives_1._PdfReference) {
                csRef = cs;
                var cachedCS = globalColorSpaceCache.get(csRef);
                if (cachedCS) {
                    return cachedCS;
                }
            }
            var parsedCS = this._parseColorspace(cs, options);
            if (csRef) {
                globalColorSpaceCache.set(null, csRef, parsedCS);
            }
            return parsedCS;
        };
        _PdfColorSpaceUtils.prototype._parseColorspace = function (cs, options) {
            var xref = options.xref, resources = options.resources, pdfFunctionFactory = options.pdfFunctionFactory;
            if (cs instanceof pdf_primitives_1._PdfReference) {
                cs = xref._fetch(cs);
            }
            if (cs instanceof pdf_primitives_1._PdfName) {
                switch (cs.name) {
                    case 'G':
                    case 'DeviceGray':
                        return this.gray;
                    case 'RGB':
                    case 'DeviceRGB':
                        return this.rgb;
                    case 'DeviceRGBA':
                        return this.rgba;
                    case 'CMYK':
                    case 'DeviceCMYK':
                        return this.cmyk;
                    case 'Pattern':
                        return new colorspace_1._PdfPatternCS(null);
                    default:
                        if (resources instanceof pdf_primitives_1._PdfDictionary) {
                            var colorSpaces = resources.get('ColorSpace');
                            if (colorSpaces instanceof pdf_primitives_1._PdfDictionary) {
                                var resourcesCS = colorSpaces.get(cs.name);
                                if (resourcesCS) {
                                    if (resourcesCS instanceof pdf_primitives_1._PdfName) {
                                        return this._parseColorspace(resourcesCS, options);
                                    }
                                    cs = resourcesCS;
                                    break;
                                }
                            }
                        }
                        return this.gray;
                }
            }
            if (Array.isArray(cs)) {
                var mode = void 0;
                if (cs[0].name instanceof pdf_primitives_1._PdfReference) {
                    mode = xref._fetch(cs[0]).name;
                }
                mode = cs[0].name;
                var params = void 0;
                var numComps = void 0;
                var baseCS = void 0;
                var whitePoint = void 0;
                var blackPoint = void 0;
                var gamma = void 0;
                var matrix = void 0;
                var hiVal = void 0;
                var lookup = void 0;
                var name_1;
                var range = void 0;
                var tintFn = void 0;
                switch (mode) {
                    case 'G':
                    case 'DeviceGray':
                        return this.gray;
                    case 'RGB':
                    case 'DeviceRGB':
                        return this.rgb;
                    case 'CMYK':
                    case 'DeviceCMYK':
                        return this.cmyk;
                    case 'CalGray':
                        params = xref.fetchIfRef(cs[1]);
                        whitePoint = params.getArray('WhitePoint');
                        blackPoint = params.getArray('BlackPoint');
                        gamma = params.get('Gamma');
                        return new colorspace_1._PdfCalGrayCS(whitePoint, blackPoint, gamma);
                    case 'CalRGB':
                        params = xref.fetchIfRef(cs[1]);
                        whitePoint = params.getArray('WhitePoint');
                        blackPoint = params.getArray('BlackPoint');
                        gamma = params.getArray('Gamma');
                        matrix = params.getArray('Matrix');
                        return new colorspace_1._PdfColorRgbConverter(whitePoint, blackPoint, gamma, matrix);
                    case 'Pattern':
                        baseCS = cs[1] || null;
                        if (baseCS) {
                            baseCS = this._subParse(baseCS, options);
                        }
                        return new colorspace_1._PdfPatternCS(baseCS);
                    case 'I':
                    case 'Indexed':
                        baseCS = this._subParse(cs[1], options);
                        hiVal = utils_1._mathClamp(cs[2] instanceof pdf_primitives_1._PdfReference ? xref._fetch(cs[2]) : cs[2], 0, 255);
                        if (cs[3] instanceof pdf_primitives_1._PdfReference) {
                            lookup = xref._fetch(cs[3]);
                        }
                        else {
                            lookup = cs[3];
                        }
                        return new colorspace_1._PdfIndexedCS(baseCS, hiVal, lookup);
                    case 'Separation':
                    case 'DeviceN':
                        name_1 = xref.fetchIfRef(cs[1]);
                        numComps = Array.isArray(name_1) ? name_1.length : 1;
                        baseCS = this._subParse(cs[2], options);
                        tintFn = pdfFunctionFactory.create(cs[3]);
                        return new colorspace_1._PdfAlternateCS(numComps, baseCS, tintFn);
                    case 'Lab':
                        params = xref.fetchIfRef(cs[1]);
                        whitePoint = params.getArray('WhitePoint');
                        blackPoint = params.getArray('BlackPoint');
                        range = params.getArray('Range');
                        return new colorspace_1._PdfLabCS(whitePoint, blackPoint, range);
                    default:
                        return this.gray;
                }
            }
            return this.gray;
        };
        Object.defineProperty(_PdfColorSpaceUtils.prototype, "gray", {
            get: function () {
                return utils_1._defineLazyProperty(this, 'gray', new colorspace_1._PdfDeviceGrayCS());
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(_PdfColorSpaceUtils.prototype, "rgb", {
            get: function () {
                return utils_1._defineLazyProperty(this, 'rgb', new colorspace_1._PdfDeviceRgbCS());
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(_PdfColorSpaceUtils.prototype, "rgba", {
            get: function () {
                return utils_1._defineLazyProperty(this, 'rgba', new colorspace_1._PdfDeviceRgbaCS());
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(_PdfColorSpaceUtils.prototype, "cmyk", {
            get: function () {
                return utils_1._defineLazyProperty(this, 'cmyk', new colorspace_1._PdfDeviceCmykCS());
            },
            enumerable: true,
            configurable: true
        });
        return _PdfColorSpaceUtils;
    }());
    exports._PdfColorSpaceUtils = _PdfColorSpaceUtils;
});