UNPKG

37 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22'use strict';
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.ColorSpace = undefined;
28
29var _util = require('../shared/util');
30
31var _primitives = require('./primitives');
32
33var ColorSpace = function ColorSpaceClosure() {
34 function resizeRgbImage(src, bpc, w1, h1, w2, h2, alpha01, dest) {
35 var COMPONENTS = 3;
36 alpha01 = alpha01 !== 1 ? 0 : alpha01;
37 var xRatio = w1 / w2;
38 var yRatio = h1 / h2;
39 var i,
40 j,
41 py,
42 newIndex = 0,
43 oldIndex;
44 var xScaled = new Uint16Array(w2);
45 var w1Scanline = w1 * COMPONENTS;
46 for (i = 0; i < w2; i++) {
47 xScaled[i] = Math.floor(i * xRatio) * COMPONENTS;
48 }
49 for (i = 0; i < h2; i++) {
50 py = Math.floor(i * yRatio) * w1Scanline;
51 for (j = 0; j < w2; j++) {
52 oldIndex = py + xScaled[j];
53 dest[newIndex++] = src[oldIndex++];
54 dest[newIndex++] = src[oldIndex++];
55 dest[newIndex++] = src[oldIndex++];
56 newIndex += alpha01;
57 }
58 }
59 }
60 function ColorSpace() {
61 (0, _util.unreachable)('should not call ColorSpace constructor');
62 }
63 ColorSpace.prototype = {
64 getRgb: function ColorSpace_getRgb(src, srcOffset) {
65 var rgb = new Uint8Array(3);
66 this.getRgbItem(src, srcOffset, rgb, 0);
67 return rgb;
68 },
69 getRgbItem: function ColorSpace_getRgbItem(src, srcOffset, dest, destOffset) {
70 (0, _util.unreachable)('Should not call ColorSpace.getRgbItem');
71 },
72 getRgbBuffer: function ColorSpace_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
73 (0, _util.unreachable)('Should not call ColorSpace.getRgbBuffer');
74 },
75 getOutputLength: function ColorSpace_getOutputLength(inputLength, alpha01) {
76 (0, _util.unreachable)('Should not call ColorSpace.getOutputLength');
77 },
78 isPassthrough: function ColorSpace_isPassthrough(bits) {
79 return false;
80 },
81 fillRgb: function ColorSpace_fillRgb(dest, originalWidth, originalHeight, width, height, actualHeight, bpc, comps, alpha01) {
82 var count = originalWidth * originalHeight;
83 var rgbBuf = null;
84 var numComponentColors = 1 << bpc;
85 var needsResizing = originalHeight !== height || originalWidth !== width;
86 var i, ii;
87 if (this.isPassthrough(bpc)) {
88 rgbBuf = comps;
89 } else if (this.numComps === 1 && count > numComponentColors && this.name !== 'DeviceGray' && this.name !== 'DeviceRGB') {
90 var allColors = bpc <= 8 ? new Uint8Array(numComponentColors) : new Uint16Array(numComponentColors);
91 var key;
92 for (i = 0; i < numComponentColors; i++) {
93 allColors[i] = i;
94 }
95 var colorMap = new Uint8Array(numComponentColors * 3);
96 this.getRgbBuffer(allColors, 0, numComponentColors, colorMap, 0, bpc, 0);
97 var destPos, rgbPos;
98 if (!needsResizing) {
99 destPos = 0;
100 for (i = 0; i < count; ++i) {
101 key = comps[i] * 3;
102 dest[destPos++] = colorMap[key];
103 dest[destPos++] = colorMap[key + 1];
104 dest[destPos++] = colorMap[key + 2];
105 destPos += alpha01;
106 }
107 } else {
108 rgbBuf = new Uint8Array(count * 3);
109 rgbPos = 0;
110 for (i = 0; i < count; ++i) {
111 key = comps[i] * 3;
112 rgbBuf[rgbPos++] = colorMap[key];
113 rgbBuf[rgbPos++] = colorMap[key + 1];
114 rgbBuf[rgbPos++] = colorMap[key + 2];
115 }
116 }
117 } else {
118 if (!needsResizing) {
119 this.getRgbBuffer(comps, 0, width * actualHeight, dest, 0, bpc, alpha01);
120 } else {
121 rgbBuf = new Uint8Array(count * 3);
122 this.getRgbBuffer(comps, 0, count, rgbBuf, 0, bpc, 0);
123 }
124 }
125 if (rgbBuf) {
126 if (needsResizing) {
127 resizeRgbImage(rgbBuf, bpc, originalWidth, originalHeight, width, height, alpha01, dest);
128 } else {
129 rgbPos = 0;
130 destPos = 0;
131 for (i = 0, ii = width * actualHeight; i < ii; i++) {
132 dest[destPos++] = rgbBuf[rgbPos++];
133 dest[destPos++] = rgbBuf[rgbPos++];
134 dest[destPos++] = rgbBuf[rgbPos++];
135 destPos += alpha01;
136 }
137 }
138 }
139 },
140 usesZeroToOneRange: true
141 };
142 ColorSpace.parse = function (cs, xref, res, pdfFunctionFactory) {
143 var IR = ColorSpace.parseToIR(cs, xref, res, pdfFunctionFactory);
144 return ColorSpace.fromIR(IR, pdfFunctionFactory);
145 };
146 ColorSpace.fromIR = function (IR, pdfFunctionFactory) {
147 var name = Array.isArray(IR) ? IR[0] : IR;
148 var whitePoint, blackPoint, gamma;
149 switch (name) {
150 case 'DeviceGrayCS':
151 return this.singletons.gray;
152 case 'DeviceRgbCS':
153 return this.singletons.rgb;
154 case 'DeviceCmykCS':
155 return this.singletons.cmyk;
156 case 'CalGrayCS':
157 whitePoint = IR[1];
158 blackPoint = IR[2];
159 gamma = IR[3];
160 return new CalGrayCS(whitePoint, blackPoint, gamma);
161 case 'CalRGBCS':
162 whitePoint = IR[1];
163 blackPoint = IR[2];
164 gamma = IR[3];
165 var matrix = IR[4];
166 return new CalRGBCS(whitePoint, blackPoint, gamma, matrix);
167 case 'PatternCS':
168 var basePatternCS = IR[1];
169 if (basePatternCS) {
170 basePatternCS = ColorSpace.fromIR(basePatternCS, pdfFunctionFactory);
171 }
172 return new PatternCS(basePatternCS);
173 case 'IndexedCS':
174 var baseIndexedCS = IR[1];
175 var hiVal = IR[2];
176 var lookup = IR[3];
177 return new IndexedCS(ColorSpace.fromIR(baseIndexedCS, pdfFunctionFactory), hiVal, lookup);
178 case 'AlternateCS':
179 var numComps = IR[1];
180 var alt = IR[2];
181 var tintFnIR = IR[3];
182 return new AlternateCS(numComps, ColorSpace.fromIR(alt, pdfFunctionFactory), pdfFunctionFactory.createFromIR(tintFnIR));
183 case 'LabCS':
184 whitePoint = IR[1];
185 blackPoint = IR[2];
186 var range = IR[3];
187 return new LabCS(whitePoint, blackPoint, range);
188 default:
189 throw new _util.FormatError('Unknown colorspace name: ' + name);
190 }
191 };
192 ColorSpace.parseToIR = function (cs, xref) {
193 var res = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
194 var pdfFunctionFactory = arguments[3];
195
196 cs = xref.fetchIfRef(cs);
197 if ((0, _primitives.isName)(cs)) {
198 switch (cs.name) {
199 case 'DeviceGray':
200 case 'G':
201 return 'DeviceGrayCS';
202 case 'DeviceRGB':
203 case 'RGB':
204 return 'DeviceRgbCS';
205 case 'DeviceCMYK':
206 case 'CMYK':
207 return 'DeviceCmykCS';
208 case 'Pattern':
209 return ['PatternCS', null];
210 default:
211 if ((0, _primitives.isDict)(res)) {
212 var colorSpaces = res.get('ColorSpace');
213 if ((0, _primitives.isDict)(colorSpaces)) {
214 var resCS = colorSpaces.get(cs.name);
215 if (resCS) {
216 if ((0, _primitives.isName)(resCS)) {
217 return ColorSpace.parseToIR(resCS, xref, res, pdfFunctionFactory);
218 }
219 cs = resCS;
220 break;
221 }
222 }
223 }
224 throw new _util.FormatError('unrecognized colorspace ' + cs.name);
225 }
226 }
227 if (Array.isArray(cs)) {
228 var mode = xref.fetchIfRef(cs[0]).name;
229 var numComps, params, alt, whitePoint, blackPoint, gamma;
230 switch (mode) {
231 case 'DeviceGray':
232 case 'G':
233 return 'DeviceGrayCS';
234 case 'DeviceRGB':
235 case 'RGB':
236 return 'DeviceRgbCS';
237 case 'DeviceCMYK':
238 case 'CMYK':
239 return 'DeviceCmykCS';
240 case 'CalGray':
241 params = xref.fetchIfRef(cs[1]);
242 whitePoint = params.getArray('WhitePoint');
243 blackPoint = params.getArray('BlackPoint');
244 gamma = params.get('Gamma');
245 return ['CalGrayCS', whitePoint, blackPoint, gamma];
246 case 'CalRGB':
247 params = xref.fetchIfRef(cs[1]);
248 whitePoint = params.getArray('WhitePoint');
249 blackPoint = params.getArray('BlackPoint');
250 gamma = params.getArray('Gamma');
251 var matrix = params.getArray('Matrix');
252 return ['CalRGBCS', whitePoint, blackPoint, gamma, matrix];
253 case 'ICCBased':
254 var stream = xref.fetchIfRef(cs[1]);
255 var dict = stream.dict;
256 numComps = dict.get('N');
257 alt = dict.get('Alternate');
258 if (alt) {
259 var altIR = ColorSpace.parseToIR(alt, xref, res, pdfFunctionFactory);
260 var altCS = ColorSpace.fromIR(altIR, pdfFunctionFactory);
261 if (altCS.numComps === numComps) {
262 return altIR;
263 }
264 (0, _util.warn)('ICCBased color space: Ignoring incorrect /Alternate entry.');
265 }
266 if (numComps === 1) {
267 return 'DeviceGrayCS';
268 } else if (numComps === 3) {
269 return 'DeviceRgbCS';
270 } else if (numComps === 4) {
271 return 'DeviceCmykCS';
272 }
273 break;
274 case 'Pattern':
275 var basePatternCS = cs[1] || null;
276 if (basePatternCS) {
277 basePatternCS = ColorSpace.parseToIR(basePatternCS, xref, res, pdfFunctionFactory);
278 }
279 return ['PatternCS', basePatternCS];
280 case 'Indexed':
281 case 'I':
282 var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res, pdfFunctionFactory);
283 var hiVal = xref.fetchIfRef(cs[2]) + 1;
284 var lookup = xref.fetchIfRef(cs[3]);
285 if ((0, _primitives.isStream)(lookup)) {
286 lookup = lookup.getBytes();
287 }
288 return ['IndexedCS', baseIndexedCS, hiVal, lookup];
289 case 'Separation':
290 case 'DeviceN':
291 var name = xref.fetchIfRef(cs[1]);
292 numComps = Array.isArray(name) ? name.length : 1;
293 alt = ColorSpace.parseToIR(cs[2], xref, res, pdfFunctionFactory);
294 var tintFnIR = pdfFunctionFactory.createIR(xref.fetchIfRef(cs[3]));
295 return ['AlternateCS', numComps, alt, tintFnIR];
296 case 'Lab':
297 params = xref.fetchIfRef(cs[1]);
298 whitePoint = params.getArray('WhitePoint');
299 blackPoint = params.getArray('BlackPoint');
300 var range = params.getArray('Range');
301 return ['LabCS', whitePoint, blackPoint, range];
302 default:
303 throw new _util.FormatError('unimplemented color space object "' + mode + '"');
304 }
305 }
306 throw new _util.FormatError('unrecognized color space object: "' + cs + '"');
307 };
308 ColorSpace.isDefaultDecode = function ColorSpace_isDefaultDecode(decode, n) {
309 if (!Array.isArray(decode)) {
310 return true;
311 }
312 if (n * 2 !== decode.length) {
313 (0, _util.warn)('The decode map is not the correct length');
314 return true;
315 }
316 for (var i = 0, ii = decode.length; i < ii; i += 2) {
317 if (decode[i] !== 0 || decode[i + 1] !== 1) {
318 return false;
319 }
320 }
321 return true;
322 };
323 ColorSpace.singletons = {
324 get gray() {
325 return (0, _util.shadow)(this, 'gray', new DeviceGrayCS());
326 },
327 get rgb() {
328 return (0, _util.shadow)(this, 'rgb', new DeviceRgbCS());
329 },
330 get cmyk() {
331 return (0, _util.shadow)(this, 'cmyk', new DeviceCmykCS());
332 }
333 };
334 return ColorSpace;
335}();
336var AlternateCS = function AlternateCSClosure() {
337 function AlternateCS(numComps, base, tintFn) {
338 this.name = 'Alternate';
339 this.numComps = numComps;
340 this.defaultColor = new Float32Array(numComps);
341 for (var i = 0; i < numComps; ++i) {
342 this.defaultColor[i] = 1;
343 }
344 this.base = base;
345 this.tintFn = tintFn;
346 this.tmpBuf = new Float32Array(base.numComps);
347 }
348 AlternateCS.prototype = {
349 getRgb: ColorSpace.prototype.getRgb,
350 getRgbItem: function AlternateCS_getRgbItem(src, srcOffset, dest, destOffset) {
351 var tmpBuf = this.tmpBuf;
352 this.tintFn(src, srcOffset, tmpBuf, 0);
353 this.base.getRgbItem(tmpBuf, 0, dest, destOffset);
354 },
355 getRgbBuffer: function AlternateCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
356 var tintFn = this.tintFn;
357 var base = this.base;
358 var scale = 1 / ((1 << bits) - 1);
359 var baseNumComps = base.numComps;
360 var usesZeroToOneRange = base.usesZeroToOneRange;
361 var isPassthrough = (base.isPassthrough(8) || !usesZeroToOneRange) && alpha01 === 0;
362 var pos = isPassthrough ? destOffset : 0;
363 var baseBuf = isPassthrough ? dest : new Uint8Array(baseNumComps * count);
364 var numComps = this.numComps;
365 var scaled = new Float32Array(numComps);
366 var tinted = new Float32Array(baseNumComps);
367 var i, j;
368 for (i = 0; i < count; i++) {
369 for (j = 0; j < numComps; j++) {
370 scaled[j] = src[srcOffset++] * scale;
371 }
372 tintFn(scaled, 0, tinted, 0);
373 if (usesZeroToOneRange) {
374 for (j = 0; j < baseNumComps; j++) {
375 baseBuf[pos++] = tinted[j] * 255;
376 }
377 } else {
378 base.getRgbItem(tinted, 0, baseBuf, pos);
379 pos += baseNumComps;
380 }
381 }
382 if (!isPassthrough) {
383 base.getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01);
384 }
385 },
386 getOutputLength: function AlternateCS_getOutputLength(inputLength, alpha01) {
387 return this.base.getOutputLength(inputLength * this.base.numComps / this.numComps, alpha01);
388 },
389 isPassthrough: ColorSpace.prototype.isPassthrough,
390 fillRgb: ColorSpace.prototype.fillRgb,
391 isDefaultDecode: function AlternateCS_isDefaultDecode(decodeMap) {
392 return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
393 },
394 usesZeroToOneRange: true
395 };
396 return AlternateCS;
397}();
398var PatternCS = function PatternCSClosure() {
399 function PatternCS(baseCS) {
400 this.name = 'Pattern';
401 this.base = baseCS;
402 }
403 PatternCS.prototype = {};
404 return PatternCS;
405}();
406var IndexedCS = function IndexedCSClosure() {
407 function IndexedCS(base, highVal, lookup) {
408 this.name = 'Indexed';
409 this.numComps = 1;
410 this.defaultColor = new Uint8Array(this.numComps);
411 this.base = base;
412 this.highVal = highVal;
413 var baseNumComps = base.numComps;
414 var length = baseNumComps * highVal;
415 if ((0, _primitives.isStream)(lookup)) {
416 this.lookup = new Uint8Array(length);
417 var bytes = lookup.getBytes(length);
418 this.lookup.set(bytes);
419 } else if ((0, _util.isString)(lookup)) {
420 this.lookup = new Uint8Array(length);
421 for (var i = 0; i < length; ++i) {
422 this.lookup[i] = lookup.charCodeAt(i);
423 }
424 } else if (lookup instanceof Uint8Array) {
425 this.lookup = lookup;
426 } else {
427 throw new _util.FormatError('Unrecognized lookup table: ' + lookup);
428 }
429 }
430 IndexedCS.prototype = {
431 getRgb: ColorSpace.prototype.getRgb,
432 getRgbItem: function IndexedCS_getRgbItem(src, srcOffset, dest, destOffset) {
433 var numComps = this.base.numComps;
434 var start = src[srcOffset] * numComps;
435 this.base.getRgbBuffer(this.lookup, start, 1, dest, destOffset, 8, 0);
436 },
437 getRgbBuffer: function IndexedCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
438 var base = this.base;
439 var numComps = base.numComps;
440 var outputDelta = base.getOutputLength(numComps, alpha01);
441 var lookup = this.lookup;
442 for (var i = 0; i < count; ++i) {
443 var lookupPos = src[srcOffset++] * numComps;
444 base.getRgbBuffer(lookup, lookupPos, 1, dest, destOffset, 8, alpha01);
445 destOffset += outputDelta;
446 }
447 },
448 getOutputLength: function IndexedCS_getOutputLength(inputLength, alpha01) {
449 return this.base.getOutputLength(inputLength * this.base.numComps, alpha01);
450 },
451 isPassthrough: ColorSpace.prototype.isPassthrough,
452 fillRgb: ColorSpace.prototype.fillRgb,
453 isDefaultDecode: function IndexedCS_isDefaultDecode(decodeMap) {
454 return true;
455 },
456 usesZeroToOneRange: true
457 };
458 return IndexedCS;
459}();
460var DeviceGrayCS = function DeviceGrayCSClosure() {
461 function DeviceGrayCS() {
462 this.name = 'DeviceGray';
463 this.numComps = 1;
464 this.defaultColor = new Float32Array(this.numComps);
465 }
466 DeviceGrayCS.prototype = {
467 getRgb: ColorSpace.prototype.getRgb,
468 getRgbItem: function DeviceGrayCS_getRgbItem(src, srcOffset, dest, destOffset) {
469 var c = src[srcOffset] * 255 | 0;
470 c = c < 0 ? 0 : c > 255 ? 255 : c;
471 dest[destOffset] = dest[destOffset + 1] = dest[destOffset + 2] = c;
472 },
473 getRgbBuffer: function DeviceGrayCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
474 var scale = 255 / ((1 << bits) - 1);
475 var j = srcOffset,
476 q = destOffset;
477 for (var i = 0; i < count; ++i) {
478 var c = scale * src[j++] | 0;
479 dest[q++] = c;
480 dest[q++] = c;
481 dest[q++] = c;
482 q += alpha01;
483 }
484 },
485 getOutputLength: function DeviceGrayCS_getOutputLength(inputLength, alpha01) {
486 return inputLength * (3 + alpha01);
487 },
488 isPassthrough: ColorSpace.prototype.isPassthrough,
489 fillRgb: ColorSpace.prototype.fillRgb,
490 isDefaultDecode: function DeviceGrayCS_isDefaultDecode(decodeMap) {
491 return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
492 },
493 usesZeroToOneRange: true
494 };
495 return DeviceGrayCS;
496}();
497var DeviceRgbCS = function DeviceRgbCSClosure() {
498 function DeviceRgbCS() {
499 this.name = 'DeviceRGB';
500 this.numComps = 3;
501 this.defaultColor = new Float32Array(this.numComps);
502 }
503 DeviceRgbCS.prototype = {
504 getRgb: ColorSpace.prototype.getRgb,
505 getRgbItem: function DeviceRgbCS_getRgbItem(src, srcOffset, dest, destOffset) {
506 var r = src[srcOffset] * 255 | 0;
507 var g = src[srcOffset + 1] * 255 | 0;
508 var b = src[srcOffset + 2] * 255 | 0;
509 dest[destOffset] = r < 0 ? 0 : r > 255 ? 255 : r;
510 dest[destOffset + 1] = g < 0 ? 0 : g > 255 ? 255 : g;
511 dest[destOffset + 2] = b < 0 ? 0 : b > 255 ? 255 : b;
512 },
513 getRgbBuffer: function DeviceRgbCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
514 if (bits === 8 && alpha01 === 0) {
515 dest.set(src.subarray(srcOffset, srcOffset + count * 3), destOffset);
516 return;
517 }
518 var scale = 255 / ((1 << bits) - 1);
519 var j = srcOffset,
520 q = destOffset;
521 for (var i = 0; i < count; ++i) {
522 dest[q++] = scale * src[j++] | 0;
523 dest[q++] = scale * src[j++] | 0;
524 dest[q++] = scale * src[j++] | 0;
525 q += alpha01;
526 }
527 },
528 getOutputLength: function DeviceRgbCS_getOutputLength(inputLength, alpha01) {
529 return inputLength * (3 + alpha01) / 3 | 0;
530 },
531 isPassthrough: function DeviceRgbCS_isPassthrough(bits) {
532 return bits === 8;
533 },
534 fillRgb: ColorSpace.prototype.fillRgb,
535 isDefaultDecode: function DeviceRgbCS_isDefaultDecode(decodeMap) {
536 return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
537 },
538 usesZeroToOneRange: true
539 };
540 return DeviceRgbCS;
541}();
542var DeviceCmykCS = function DeviceCmykCSClosure() {
543 function convertToRgb(src, srcOffset, srcScale, dest, destOffset) {
544 var c = src[srcOffset + 0] * srcScale;
545 var m = src[srcOffset + 1] * srcScale;
546 var y = src[srcOffset + 2] * srcScale;
547 var k = src[srcOffset + 3] * srcScale;
548 var r = 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) + 255 | 0;
549 var g = 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) + 255 | 0;
550 var b = 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) + 255 | 0;
551 dest[destOffset] = r > 255 ? 255 : r < 0 ? 0 : r;
552 dest[destOffset + 1] = g > 255 ? 255 : g < 0 ? 0 : g;
553 dest[destOffset + 2] = b > 255 ? 255 : b < 0 ? 0 : b;
554 }
555 function DeviceCmykCS() {
556 this.name = 'DeviceCMYK';
557 this.numComps = 4;
558 this.defaultColor = new Float32Array(this.numComps);
559 this.defaultColor[3] = 1;
560 }
561 DeviceCmykCS.prototype = {
562 getRgb: ColorSpace.prototype.getRgb,
563 getRgbItem: function DeviceCmykCS_getRgbItem(src, srcOffset, dest, destOffset) {
564 convertToRgb(src, srcOffset, 1, dest, destOffset);
565 },
566 getRgbBuffer: function DeviceCmykCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
567 var scale = 1 / ((1 << bits) - 1);
568 for (var i = 0; i < count; i++) {
569 convertToRgb(src, srcOffset, scale, dest, destOffset);
570 srcOffset += 4;
571 destOffset += 3 + alpha01;
572 }
573 },
574 getOutputLength: function DeviceCmykCS_getOutputLength(inputLength, alpha01) {
575 return inputLength / 4 * (3 + alpha01) | 0;
576 },
577 isPassthrough: ColorSpace.prototype.isPassthrough,
578 fillRgb: ColorSpace.prototype.fillRgb,
579 isDefaultDecode: function DeviceCmykCS_isDefaultDecode(decodeMap) {
580 return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
581 },
582 usesZeroToOneRange: true
583 };
584 return DeviceCmykCS;
585}();
586var CalGrayCS = function CalGrayCSClosure() {
587 function CalGrayCS(whitePoint, blackPoint, gamma) {
588 this.name = 'CalGray';
589 this.numComps = 1;
590 this.defaultColor = new Float32Array(this.numComps);
591 if (!whitePoint) {
592 throw new _util.FormatError('WhitePoint missing - required for color space CalGray');
593 }
594 blackPoint = blackPoint || [0, 0, 0];
595 gamma = gamma || 1;
596 this.XW = whitePoint[0];
597 this.YW = whitePoint[1];
598 this.ZW = whitePoint[2];
599 this.XB = blackPoint[0];
600 this.YB = blackPoint[1];
601 this.ZB = blackPoint[2];
602 this.G = gamma;
603 if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
604 throw new _util.FormatError('Invalid WhitePoint components for ' + this.name + ', no fallback available');
605 }
606 if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
607 (0, _util.info)('Invalid BlackPoint for ' + this.name + ', falling back to default');
608 this.XB = this.YB = this.ZB = 0;
609 }
610 if (this.XB !== 0 || this.YB !== 0 || this.ZB !== 0) {
611 (0, _util.warn)(this.name + ', BlackPoint: XB: ' + this.XB + ', YB: ' + this.YB + ', ZB: ' + this.ZB + ', only default values are supported.');
612 }
613 if (this.G < 1) {
614 (0, _util.info)('Invalid Gamma: ' + this.G + ' for ' + this.name + ', falling back to default');
615 this.G = 1;
616 }
617 }
618 function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
619 var A = src[srcOffset] * scale;
620 var AG = Math.pow(A, cs.G);
621 var L = cs.YW * AG;
622 var val = Math.max(295.8 * Math.pow(L, 0.333333333333333333) - 40.8, 0) | 0;
623 dest[destOffset] = val;
624 dest[destOffset + 1] = val;
625 dest[destOffset + 2] = val;
626 }
627 CalGrayCS.prototype = {
628 getRgb: ColorSpace.prototype.getRgb,
629 getRgbItem: function CalGrayCS_getRgbItem(src, srcOffset, dest, destOffset) {
630 convertToRgb(this, src, srcOffset, dest, destOffset, 1);
631 },
632 getRgbBuffer: function CalGrayCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
633 var scale = 1 / ((1 << bits) - 1);
634 for (var i = 0; i < count; ++i) {
635 convertToRgb(this, src, srcOffset, dest, destOffset, scale);
636 srcOffset += 1;
637 destOffset += 3 + alpha01;
638 }
639 },
640 getOutputLength: function CalGrayCS_getOutputLength(inputLength, alpha01) {
641 return inputLength * (3 + alpha01);
642 },
643 isPassthrough: ColorSpace.prototype.isPassthrough,
644 fillRgb: ColorSpace.prototype.fillRgb,
645 isDefaultDecode: function CalGrayCS_isDefaultDecode(decodeMap) {
646 return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
647 },
648 usesZeroToOneRange: true
649 };
650 return CalGrayCS;
651}();
652var CalRGBCS = function CalRGBCSClosure() {
653 var BRADFORD_SCALE_MATRIX = new Float32Array([0.8951, 0.2664, -0.1614, -0.7502, 1.7135, 0.0367, 0.0389, -0.0685, 1.0296]);
654 var BRADFORD_SCALE_INVERSE_MATRIX = new Float32Array([0.9869929, -0.1470543, 0.1599627, 0.4323053, 0.5183603, 0.0492912, -0.0085287, 0.0400428, 0.9684867]);
655 var SRGB_D65_XYZ_TO_RGB_MATRIX = new Float32Array([3.2404542, -1.5371385, -0.4985314, -0.9692660, 1.8760108, 0.0415560, 0.0556434, -0.2040259, 1.0572252]);
656 var FLAT_WHITEPOINT_MATRIX = new Float32Array([1, 1, 1]);
657 var tempNormalizeMatrix = new Float32Array(3);
658 var tempConvertMatrix1 = new Float32Array(3);
659 var tempConvertMatrix2 = new Float32Array(3);
660 var DECODE_L_CONSTANT = Math.pow((8 + 16) / 116, 3) / 8.0;
661 function CalRGBCS(whitePoint, blackPoint, gamma, matrix) {
662 this.name = 'CalRGB';
663 this.numComps = 3;
664 this.defaultColor = new Float32Array(this.numComps);
665 if (!whitePoint) {
666 throw new _util.FormatError('WhitePoint missing - required for color space CalRGB');
667 }
668 blackPoint = blackPoint || new Float32Array(3);
669 gamma = gamma || new Float32Array([1, 1, 1]);
670 matrix = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
671 var XW = whitePoint[0];
672 var YW = whitePoint[1];
673 var ZW = whitePoint[2];
674 this.whitePoint = whitePoint;
675 var XB = blackPoint[0];
676 var YB = blackPoint[1];
677 var ZB = blackPoint[2];
678 this.blackPoint = blackPoint;
679 this.GR = gamma[0];
680 this.GG = gamma[1];
681 this.GB = gamma[2];
682 this.MXA = matrix[0];
683 this.MYA = matrix[1];
684 this.MZA = matrix[2];
685 this.MXB = matrix[3];
686 this.MYB = matrix[4];
687 this.MZB = matrix[5];
688 this.MXC = matrix[6];
689 this.MYC = matrix[7];
690 this.MZC = matrix[8];
691 if (XW < 0 || ZW < 0 || YW !== 1) {
692 throw new _util.FormatError('Invalid WhitePoint components for ' + this.name + ', no fallback available');
693 }
694 if (XB < 0 || YB < 0 || ZB < 0) {
695 (0, _util.info)('Invalid BlackPoint for ' + this.name + ' [' + XB + ', ' + YB + ', ' + ZB + '], falling back to default');
696 this.blackPoint = new Float32Array(3);
697 }
698 if (this.GR < 0 || this.GG < 0 || this.GB < 0) {
699 (0, _util.info)('Invalid Gamma [' + this.GR + ', ' + this.GG + ', ' + this.GB + '] for ' + this.name + ', falling back to default');
700 this.GR = this.GG = this.GB = 1;
701 }
702 if (this.MXA < 0 || this.MYA < 0 || this.MZA < 0 || this.MXB < 0 || this.MYB < 0 || this.MZB < 0 || this.MXC < 0 || this.MYC < 0 || this.MZC < 0) {
703 (0, _util.info)('Invalid Matrix for ' + this.name + ' [' + this.MXA + ', ' + this.MYA + ', ' + this.MZA + this.MXB + ', ' + this.MYB + ', ' + this.MZB + this.MXC + ', ' + this.MYC + ', ' + this.MZC + '], falling back to default');
704 this.MXA = this.MYB = this.MZC = 1;
705 this.MXB = this.MYA = this.MZA = this.MXC = this.MYC = this.MZB = 0;
706 }
707 }
708 function matrixProduct(a, b, result) {
709 result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
710 result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
711 result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
712 }
713 function convertToFlat(sourceWhitePoint, LMS, result) {
714 result[0] = LMS[0] * 1 / sourceWhitePoint[0];
715 result[1] = LMS[1] * 1 / sourceWhitePoint[1];
716 result[2] = LMS[2] * 1 / sourceWhitePoint[2];
717 }
718 function convertToD65(sourceWhitePoint, LMS, result) {
719 var D65X = 0.95047;
720 var D65Y = 1;
721 var D65Z = 1.08883;
722 result[0] = LMS[0] * D65X / sourceWhitePoint[0];
723 result[1] = LMS[1] * D65Y / sourceWhitePoint[1];
724 result[2] = LMS[2] * D65Z / sourceWhitePoint[2];
725 }
726 function sRGBTransferFunction(color) {
727 if (color <= 0.0031308) {
728 return adjustToRange(0, 1, 12.92 * color);
729 }
730 return adjustToRange(0, 1, (1 + 0.055) * Math.pow(color, 1 / 2.4) - 0.055);
731 }
732 function adjustToRange(min, max, value) {
733 return Math.max(min, Math.min(max, value));
734 }
735 function decodeL(L) {
736 if (L < 0) {
737 return -decodeL(-L);
738 }
739 if (L > 8.0) {
740 return Math.pow((L + 16) / 116, 3);
741 }
742 return L * DECODE_L_CONSTANT;
743 }
744 function compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {
745 if (sourceBlackPoint[0] === 0 && sourceBlackPoint[1] === 0 && sourceBlackPoint[2] === 0) {
746 result[0] = XYZ_Flat[0];
747 result[1] = XYZ_Flat[1];
748 result[2] = XYZ_Flat[2];
749 return;
750 }
751 var zeroDecodeL = decodeL(0);
752 var X_DST = zeroDecodeL;
753 var X_SRC = decodeL(sourceBlackPoint[0]);
754 var Y_DST = zeroDecodeL;
755 var Y_SRC = decodeL(sourceBlackPoint[1]);
756 var Z_DST = zeroDecodeL;
757 var Z_SRC = decodeL(sourceBlackPoint[2]);
758 var X_Scale = (1 - X_DST) / (1 - X_SRC);
759 var X_Offset = 1 - X_Scale;
760 var Y_Scale = (1 - Y_DST) / (1 - Y_SRC);
761 var Y_Offset = 1 - Y_Scale;
762 var Z_Scale = (1 - Z_DST) / (1 - Z_SRC);
763 var Z_Offset = 1 - Z_Scale;
764 result[0] = XYZ_Flat[0] * X_Scale + X_Offset;
765 result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;
766 result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;
767 }
768 function normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {
769 if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {
770 result[0] = XYZ_In[0];
771 result[1] = XYZ_In[1];
772 result[2] = XYZ_In[2];
773 return;
774 }
775 var LMS = result;
776 matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
777 var LMS_Flat = tempNormalizeMatrix;
778 convertToFlat(sourceWhitePoint, LMS, LMS_Flat);
779 matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_Flat, result);
780 }
781 function normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {
782 var LMS = result;
783 matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
784 var LMS_D65 = tempNormalizeMatrix;
785 convertToD65(sourceWhitePoint, LMS, LMS_D65);
786 matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_D65, result);
787 }
788 function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
789 var A = adjustToRange(0, 1, src[srcOffset] * scale);
790 var B = adjustToRange(0, 1, src[srcOffset + 1] * scale);
791 var C = adjustToRange(0, 1, src[srcOffset + 2] * scale);
792 var AGR = Math.pow(A, cs.GR);
793 var BGG = Math.pow(B, cs.GG);
794 var CGB = Math.pow(C, cs.GB);
795 var X = cs.MXA * AGR + cs.MXB * BGG + cs.MXC * CGB;
796 var Y = cs.MYA * AGR + cs.MYB * BGG + cs.MYC * CGB;
797 var Z = cs.MZA * AGR + cs.MZB * BGG + cs.MZC * CGB;
798 var XYZ = tempConvertMatrix1;
799 XYZ[0] = X;
800 XYZ[1] = Y;
801 XYZ[2] = Z;
802 var XYZ_Flat = tempConvertMatrix2;
803 normalizeWhitePointToFlat(cs.whitePoint, XYZ, XYZ_Flat);
804 var XYZ_Black = tempConvertMatrix1;
805 compensateBlackPoint(cs.blackPoint, XYZ_Flat, XYZ_Black);
806 var XYZ_D65 = tempConvertMatrix2;
807 normalizeWhitePointToD65(FLAT_WHITEPOINT_MATRIX, XYZ_Black, XYZ_D65);
808 var SRGB = tempConvertMatrix1;
809 matrixProduct(SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
810 var sR = sRGBTransferFunction(SRGB[0]);
811 var sG = sRGBTransferFunction(SRGB[1]);
812 var sB = sRGBTransferFunction(SRGB[2]);
813 dest[destOffset] = Math.round(sR * 255);
814 dest[destOffset + 1] = Math.round(sG * 255);
815 dest[destOffset + 2] = Math.round(sB * 255);
816 }
817 CalRGBCS.prototype = {
818 getRgb: ColorSpace.prototype.getRgb,
819 getRgbItem: function CalRGBCS_getRgbItem(src, srcOffset, dest, destOffset) {
820 convertToRgb(this, src, srcOffset, dest, destOffset, 1);
821 },
822 getRgbBuffer: function CalRGBCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
823 var scale = 1 / ((1 << bits) - 1);
824 for (var i = 0; i < count; ++i) {
825 convertToRgb(this, src, srcOffset, dest, destOffset, scale);
826 srcOffset += 3;
827 destOffset += 3 + alpha01;
828 }
829 },
830 getOutputLength: function CalRGBCS_getOutputLength(inputLength, alpha01) {
831 return inputLength * (3 + alpha01) / 3 | 0;
832 },
833 isPassthrough: ColorSpace.prototype.isPassthrough,
834 fillRgb: ColorSpace.prototype.fillRgb,
835 isDefaultDecode: function CalRGBCS_isDefaultDecode(decodeMap) {
836 return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
837 },
838 usesZeroToOneRange: true
839 };
840 return CalRGBCS;
841}();
842var LabCS = function LabCSClosure() {
843 function LabCS(whitePoint, blackPoint, range) {
844 this.name = 'Lab';
845 this.numComps = 3;
846 this.defaultColor = new Float32Array(this.numComps);
847 if (!whitePoint) {
848 throw new _util.FormatError('WhitePoint missing - required for color space Lab');
849 }
850 blackPoint = blackPoint || [0, 0, 0];
851 range = range || [-100, 100, -100, 100];
852 this.XW = whitePoint[0];
853 this.YW = whitePoint[1];
854 this.ZW = whitePoint[2];
855 this.amin = range[0];
856 this.amax = range[1];
857 this.bmin = range[2];
858 this.bmax = range[3];
859 this.XB = blackPoint[0];
860 this.YB = blackPoint[1];
861 this.ZB = blackPoint[2];
862 if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
863 throw new _util.FormatError('Invalid WhitePoint components, no fallback available');
864 }
865 if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
866 (0, _util.info)('Invalid BlackPoint, falling back to default');
867 this.XB = this.YB = this.ZB = 0;
868 }
869 if (this.amin > this.amax || this.bmin > this.bmax) {
870 (0, _util.info)('Invalid Range, falling back to defaults');
871 this.amin = -100;
872 this.amax = 100;
873 this.bmin = -100;
874 this.bmax = 100;
875 }
876 }
877 function fn_g(x) {
878 var result;
879 if (x >= 6 / 29) {
880 result = x * x * x;
881 } else {
882 result = 108 / 841 * (x - 4 / 29);
883 }
884 return result;
885 }
886 function decode(value, high1, low2, high2) {
887 return low2 + value * (high2 - low2) / high1;
888 }
889 function convertToRgb(cs, src, srcOffset, maxVal, dest, destOffset) {
890 var Ls = src[srcOffset];
891 var as = src[srcOffset + 1];
892 var bs = src[srcOffset + 2];
893 if (maxVal !== false) {
894 Ls = decode(Ls, maxVal, 0, 100);
895 as = decode(as, maxVal, cs.amin, cs.amax);
896 bs = decode(bs, maxVal, cs.bmin, cs.bmax);
897 }
898 as = as > cs.amax ? cs.amax : as < cs.amin ? cs.amin : as;
899 bs = bs > cs.bmax ? cs.bmax : bs < cs.bmin ? cs.bmin : bs;
900 var M = (Ls + 16) / 116;
901 var L = M + as / 500;
902 var N = M - bs / 200;
903 var X = cs.XW * fn_g(L);
904 var Y = cs.YW * fn_g(M);
905 var Z = cs.ZW * fn_g(N);
906 var r, g, b;
907 if (cs.ZW < 1) {
908 r = X * 3.1339 + Y * -1.6170 + Z * -0.4906;
909 g = X * -0.9785 + Y * 1.9160 + Z * 0.0333;
910 b = X * 0.0720 + Y * -0.2290 + Z * 1.4057;
911 } else {
912 r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
913 g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
914 b = X * 0.0557 + Y * -0.2040 + Z * 1.0570;
915 }
916 dest[destOffset] = r <= 0 ? 0 : r >= 1 ? 255 : Math.sqrt(r) * 255 | 0;
917 dest[destOffset + 1] = g <= 0 ? 0 : g >= 1 ? 255 : Math.sqrt(g) * 255 | 0;
918 dest[destOffset + 2] = b <= 0 ? 0 : b >= 1 ? 255 : Math.sqrt(b) * 255 | 0;
919 }
920 LabCS.prototype = {
921 getRgb: ColorSpace.prototype.getRgb,
922 getRgbItem: function LabCS_getRgbItem(src, srcOffset, dest, destOffset) {
923 convertToRgb(this, src, srcOffset, false, dest, destOffset);
924 },
925 getRgbBuffer: function LabCS_getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
926 var maxVal = (1 << bits) - 1;
927 for (var i = 0; i < count; i++) {
928 convertToRgb(this, src, srcOffset, maxVal, dest, destOffset);
929 srcOffset += 3;
930 destOffset += 3 + alpha01;
931 }
932 },
933 getOutputLength: function LabCS_getOutputLength(inputLength, alpha01) {
934 return inputLength * (3 + alpha01) / 3 | 0;
935 },
936 isPassthrough: ColorSpace.prototype.isPassthrough,
937 fillRgb: ColorSpace.prototype.fillRgb,
938 isDefaultDecode: function LabCS_isDefaultDecode(decodeMap) {
939 return true;
940 },
941 usesZeroToOneRange: false
942 };
943 return LabCS;
944}();
945exports.ColorSpace = ColorSpace;
\No newline at end of file