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