forked from tsdgeos/poppler_mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGfxState.cc
7148 lines (6462 loc) · 221 KB
/
GfxState.cc
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========================================================================
//
// GfxState.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005 Kristian Høgsberg <[email protected]>
// Copyright (C) 2006, 2007 Jeff Muizelaar <[email protected]>
// Copyright (C) 2006, 2010 Carlos Garcia Campos <[email protected]>
// Copyright (C) 2006-2022, 2024 Albert Astals Cid <[email protected]>
// Copyright (C) 2009, 2012 Koji Otani <[email protected]>
// Copyright (C) 2009, 2011-2016, 2020, 2023 Thomas Freitag <[email protected]>
// Copyright (C) 2009, 2019 Christian Persch <[email protected]>
// Copyright (C) 2010 Paweł Wiejacha <[email protected]>
// Copyright (C) 2010 Christian Feuersänger <[email protected]>
// Copyright (C) 2011 Andrea Canciani <[email protected]>
// Copyright (C) 2012, 2020 William Bader <[email protected]>
// Copyright (C) 2013 Lu Wang <[email protected]>
// Copyright (C) 2013 Hib Eris <[email protected]>
// Copyright (C) 2013 Fabio D'Urso <[email protected]>
// Copyright (C) 2015, 2020 Adrian Johnson <[email protected]>
// Copyright (C) 2016 Marek Kasik <[email protected]>
// Copyright (C) 2017, 2019, 2022 Oliver Sander <[email protected]>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Volker Krause <[email protected]>
// Copyright (C) 2018, 2019 Adam Reichold <[email protected]>
// Copyright (C) 2019 LE GARREC Vincent <[email protected]>
// Copyright (C) 2020, 2021 Philipp Knechtges <[email protected]>
// Copyright (C) 2020 Lluís Batlle i Rossell <[email protected]>
// Copyright (C) 2024 Athul Raj Kollareth <[email protected]>
// Copyright (C) 2024 Nelson Benítez León <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#include <algorithm>
#include <memory>
#include <cstddef>
#include <cmath>
#include <cstring>
#include "goo/gfile.h"
#include "goo/gmem.h"
#include "Error.h"
#include "Object.h"
#include "Array.h"
#include "Page.h"
#include "Gfx.h"
#include "GfxState.h"
#include "GfxState_helpers.h"
#include "GfxFont.h"
#include "GlobalParams.h"
#include "PopplerCache.h"
#include "OutputDev.h"
#include "splash/SplashTypes.h"
//------------------------------------------------------------------------
// Max depth of nested color spaces. This is used to catch infinite
// loops in the color space object structure.
#define colorSpaceRecursionLimit 8
//------------------------------------------------------------------------
bool Matrix::invertTo(Matrix *other) const
{
const double det_denominator = determinant();
if (unlikely(det_denominator == 0)) {
*other = { 1, 0, 0, 1, 0, 0 };
return false;
}
const double det = 1 / det_denominator;
other->m[0] = m[3] * det;
other->m[1] = -m[1] * det;
other->m[2] = -m[2] * det;
other->m[3] = m[0] * det;
other->m[4] = (m[2] * m[5] - m[3] * m[4]) * det;
other->m[5] = (m[1] * m[4] - m[0] * m[5]) * det;
return true;
}
void Matrix::translate(double tx, double ty)
{
double x0 = tx * m[0] + ty * m[2] + m[4];
double y0 = tx * m[1] + ty * m[3] + m[5];
m[4] = x0;
m[5] = y0;
}
void Matrix::scale(double sx, double sy)
{
m[0] *= sx;
m[1] *= sx;
m[2] *= sy;
m[3] *= sy;
}
void Matrix::transform(double x, double y, double *tx, double *ty) const
{
double temp_x, temp_y;
temp_x = m[0] * x + m[2] * y + m[4];
temp_y = m[1] * x + m[3] * y + m[5];
*tx = temp_x;
*ty = temp_y;
}
// Matrix norm, taken from _cairo_matrix_transformed_circle_major_axis
double Matrix::norm() const
{
double f, g, h, i, j;
i = m[0] * m[0] + m[1] * m[1];
j = m[2] * m[2] + m[3] * m[3];
f = 0.5 * (i + j);
g = 0.5 * (i - j);
h = m[0] * m[2] + m[1] * m[3];
return sqrt(f + hypot(g, h));
}
//------------------------------------------------------------------------
struct GfxBlendModeInfo
{
const char *name;
GfxBlendMode mode;
};
static const GfxBlendModeInfo gfxBlendModeNames[] = { { "Normal", gfxBlendNormal }, { "Compatible", gfxBlendNormal },
{ "Multiply", gfxBlendMultiply }, { "Screen", gfxBlendScreen },
{ "Overlay", gfxBlendOverlay }, { "Darken", gfxBlendDarken },
{ "Lighten", gfxBlendLighten }, { "ColorDodge", gfxBlendColorDodge },
{ "ColorBurn", gfxBlendColorBurn }, { "HardLight", gfxBlendHardLight },
{ "SoftLight", gfxBlendSoftLight }, { "Difference", gfxBlendDifference },
{ "Exclusion", gfxBlendExclusion }, { "Hue", gfxBlendHue },
{ "Saturation", gfxBlendSaturation }, { "Color", gfxBlendColor },
{ "Luminosity", gfxBlendLuminosity } };
#define nGfxBlendModeNames ((int)((sizeof(gfxBlendModeNames) / sizeof(GfxBlendModeInfo))))
//------------------------------------------------------------------------
//
// NB: This must match the GfxColorSpaceMode enum defined in
// GfxState.h
static const char *gfxColorSpaceModeNames[] = { "DeviceGray", "CalGray", "DeviceRGB", "CalRGB", "DeviceCMYK", "Lab", "ICCBased", "Indexed", "Separation", "DeviceN", "Pattern", "DeviceRGBA" };
#define nGfxColorSpaceModes ((sizeof(gfxColorSpaceModeNames) / sizeof(char *)))
#ifdef USE_CMS
static const std::map<unsigned int, unsigned int>::size_type CMSCACHE_LIMIT = 2048;
# include <lcms2.h>
# define LCMS_FLAGS cmsFLAGS_NOOPTIMIZE | cmsFLAGS_BLACKPOINTCOMPENSATION
static void lcmsprofiledeleter(void *profile)
{
cmsCloseProfile(profile);
}
GfxLCMSProfilePtr make_GfxLCMSProfilePtr(void *profile)
{
if (profile == nullptr) {
return GfxLCMSProfilePtr();
}
return GfxLCMSProfilePtr(profile, lcmsprofiledeleter);
}
void GfxColorTransform::doTransform(void *in, void *out, unsigned int size)
{
cmsDoTransform(transform, in, out, size);
}
// transformA should be a cmsHTRANSFORM
GfxColorTransform::GfxColorTransform(void *transformA, int cmsIntentA, unsigned int inputPixelTypeA, unsigned int transformPixelTypeA)
{
transform = transformA;
cmsIntent = cmsIntentA;
inputPixelType = inputPixelTypeA;
transformPixelType = transformPixelTypeA;
}
GfxColorTransform::~GfxColorTransform()
{
cmsDeleteTransform(transform);
}
// convert color space signature to cmsColor type
static unsigned int getCMSColorSpaceType(cmsColorSpaceSignature cs);
static unsigned int getCMSNChannels(cmsColorSpaceSignature cs);
#endif
//------------------------------------------------------------------------
// GfxColorSpace
//------------------------------------------------------------------------
GfxColorSpace::GfxColorSpace()
{
overprintMask = 0x0f;
mapping = nullptr;
}
GfxColorSpace::~GfxColorSpace() { }
std::unique_ptr<GfxColorSpace> GfxColorSpace::parse(GfxResources *res, Object *csObj, OutputDev *out, GfxState *state, int recursion)
{
Object obj1;
if (recursion > colorSpaceRecursionLimit) {
error(errSyntaxError, -1, "Loop detected in color space objects");
return {};
}
if (csObj->isName()) {
if (csObj->isName("DeviceGray") || csObj->isName("G")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultGray");
if (objCS.isNull()) {
return state->copyDefaultGrayColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultGrayColorSpace();
}
} else if (csObj->isName("DeviceRGB") || csObj->isName("RGB")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultRGB");
if (objCS.isNull()) {
return state->copyDefaultRGBColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultRGBColorSpace();
}
} else if (csObj->isName("DeviceCMYK") || csObj->isName("CMYK")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultCMYK");
if (objCS.isNull()) {
return state->copyDefaultCMYKColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultCMYKColorSpace();
}
} else if (csObj->isName("Pattern")) {
return std::make_unique<GfxPatternColorSpace>(nullptr);
} else {
error(errSyntaxWarning, -1, "Bad color space '{0:s}'", csObj->getName());
}
} else if (csObj->isArray() && csObj->arrayGetLength() > 0) {
obj1 = csObj->arrayGet(0);
if (obj1.isName("DeviceGray") || obj1.isName("G")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultGray");
if (objCS.isNull()) {
return state->copyDefaultGrayColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultGrayColorSpace();
}
} else if (obj1.isName("DeviceRGB") || obj1.isName("RGB")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultRGB");
if (objCS.isNull()) {
return state->copyDefaultRGBColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultRGBColorSpace();
}
} else if (obj1.isName("DeviceCMYK") || obj1.isName("CMYK")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultCMYK");
if (objCS.isNull()) {
return state->copyDefaultCMYKColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultCMYKColorSpace();
}
} else if (obj1.isName("CalGray")) {
return GfxCalGrayColorSpace::parse(csObj->getArray(), state);
} else if (obj1.isName("CalRGB")) {
return GfxCalRGBColorSpace::parse(csObj->getArray(), state);
} else if (obj1.isName("Lab")) {
return GfxLabColorSpace::parse(csObj->getArray(), state);
} else if (obj1.isName("ICCBased")) {
return GfxICCBasedColorSpace::parse(csObj->getArray(), out, state, recursion);
} else if (obj1.isName("Indexed") || obj1.isName("I")) {
return GfxIndexedColorSpace::parse(res, csObj->getArray(), out, state, recursion);
} else if (obj1.isName("Separation")) {
return GfxSeparationColorSpace::parse(res, csObj->getArray(), out, state, recursion);
} else if (obj1.isName("DeviceN")) {
return GfxDeviceNColorSpace::parse(res, csObj->getArray(), out, state, recursion);
} else if (obj1.isName("Pattern")) {
return GfxPatternColorSpace::parse(res, csObj->getArray(), out, state, recursion);
} else {
error(errSyntaxWarning, -1, "Bad color space");
}
} else if (csObj->isDict()) {
obj1 = csObj->dictLookup("ColorSpace");
if (obj1.isName("DeviceGray")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultGray");
if (objCS.isNull()) {
return state->copyDefaultGrayColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultGrayColorSpace();
}
} else if (obj1.isName("DeviceRGB")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultRGB");
if (objCS.isNull()) {
return state->copyDefaultRGBColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultRGBColorSpace();
}
} else if (obj1.isName("DeviceCMYK")) {
if (res != nullptr) {
Object objCS = res->lookupColorSpace("DefaultCMYK");
if (objCS.isNull()) {
return state->copyDefaultCMYKColorSpace();
} else {
return GfxColorSpace::parse(nullptr, &objCS, out, state);
}
} else {
return state->copyDefaultCMYKColorSpace();
}
} else {
error(errSyntaxWarning, -1, "Bad color space dict'");
}
} else {
error(errSyntaxWarning, -1, "Bad color space - expected name or array or dict");
}
return {};
}
void GfxColorSpace::createMapping(std::vector<std::unique_ptr<GfxSeparationColorSpace>> *separationList, int maxSepComps)
{
return;
}
void GfxColorSpace::getDefaultRanges(double *decodeLow, double *decodeRange, int maxImgPixel) const
{
int i;
for (i = 0; i < getNComps(); ++i) {
decodeLow[i] = 0;
decodeRange[i] = 1;
}
}
int GfxColorSpace::getNumColorSpaceModes()
{
return nGfxColorSpaceModes;
}
const char *GfxColorSpace::getColorSpaceModeName(int idx)
{
return gfxColorSpaceModeNames[idx];
}
#ifdef USE_CMS
static void CMSError(cmsContext /*contextId*/, cmsUInt32Number /*ecode*/, const char *text)
{
error(errSyntaxWarning, -1, "{0:s}", text);
}
unsigned int getCMSColorSpaceType(cmsColorSpaceSignature cs)
{
switch (cs) {
case cmsSigXYZData:
return PT_XYZ;
break;
case cmsSigLabData:
return PT_Lab;
break;
case cmsSigLuvData:
return PT_YUV;
break;
case cmsSigYCbCrData:
return PT_YCbCr;
break;
case cmsSigYxyData:
return PT_Yxy;
break;
case cmsSigRgbData:
return PT_RGB;
break;
case cmsSigGrayData:
return PT_GRAY;
break;
case cmsSigHsvData:
return PT_HSV;
break;
case cmsSigHlsData:
return PT_HLS;
break;
case cmsSigCmykData:
return PT_CMYK;
break;
case cmsSigCmyData:
return PT_CMY;
break;
case cmsSig2colorData:
case cmsSig3colorData:
case cmsSig4colorData:
case cmsSig5colorData:
case cmsSig6colorData:
case cmsSig7colorData:
case cmsSig8colorData:
case cmsSig9colorData:
case cmsSig10colorData:
case cmsSig11colorData:
case cmsSig12colorData:
case cmsSig13colorData:
case cmsSig14colorData:
case cmsSig15colorData:
default:
break;
}
return PT_RGB;
}
unsigned int getCMSNChannels(cmsColorSpaceSignature cs)
{
switch (cs) {
case cmsSigXYZData:
case cmsSigLuvData:
case cmsSigLabData:
case cmsSigYCbCrData:
case cmsSigYxyData:
case cmsSigRgbData:
case cmsSigHsvData:
case cmsSigHlsData:
case cmsSigCmyData:
case cmsSig3colorData:
return 3;
break;
case cmsSigGrayData:
return 1;
break;
case cmsSigCmykData:
case cmsSig4colorData:
return 4;
break;
case cmsSig2colorData:
return 2;
break;
case cmsSig5colorData:
return 5;
break;
case cmsSig6colorData:
return 6;
break;
case cmsSig7colorData:
return 7;
break;
case cmsSig8colorData:
return 8;
break;
case cmsSig9colorData:
return 9;
break;
case cmsSig10colorData:
return 10;
break;
case cmsSig11colorData:
return 11;
break;
case cmsSig12colorData:
return 12;
break;
case cmsSig13colorData:
return 13;
break;
case cmsSig14colorData:
return 14;
break;
case cmsSig15colorData:
return 15;
default:
break;
}
return 3;
}
#endif
//------------------------------------------------------------------------
// GfxDeviceGrayColorSpace
//------------------------------------------------------------------------
GfxDeviceGrayColorSpace::GfxDeviceGrayColorSpace() { }
GfxDeviceGrayColorSpace::~GfxDeviceGrayColorSpace() { }
std::unique_ptr<GfxColorSpace> GfxDeviceGrayColorSpace::copy() const
{
return std::make_unique<GfxDeviceGrayColorSpace>();
}
void GfxDeviceGrayColorSpace::getGray(const GfxColor *color, GfxGray *gray) const
{
*gray = clip01(color->c[0]);
}
void GfxDeviceGrayColorSpace::getGrayLine(unsigned char *in, unsigned char *out, int length)
{
memcpy(out, in, length);
}
void GfxDeviceGrayColorSpace::getRGB(const GfxColor *color, GfxRGB *rgb) const
{
rgb->r = rgb->g = rgb->b = clip01(color->c[0]);
}
void GfxDeviceGrayColorSpace::getRGBLine(unsigned char *in, unsigned int *out, int length)
{
int i;
for (i = 0; i < length; i++) {
out[i] = (in[i] << 16) | (in[i] << 8) | (in[i] << 0);
}
}
void GfxDeviceGrayColorSpace::getRGBLine(unsigned char *in, unsigned char *out, int length)
{
for (int i = 0; i < length; i++) {
*out++ = in[i];
*out++ = in[i];
*out++ = in[i];
}
}
void GfxDeviceGrayColorSpace::getRGBXLine(unsigned char *in, unsigned char *out, int length)
{
for (int i = 0; i < length; i++) {
*out++ = in[i];
*out++ = in[i];
*out++ = in[i];
*out++ = 255;
}
}
void GfxDeviceGrayColorSpace::getCMYKLine(unsigned char *in, unsigned char *out, int length)
{
for (int i = 0; i < length; i++) {
*out++ = 0;
*out++ = 0;
*out++ = 0;
*out++ = in[i];
}
}
void GfxDeviceGrayColorSpace::getDeviceNLine(unsigned char *in, unsigned char *out, int length)
{
for (int i = 0; i < length; i++) {
for (int j = 0; j < SPOT_NCOMPS + 4; j++) {
out[j] = 0;
}
out[4] = in[i];
out += (SPOT_NCOMPS + 4);
}
}
void GfxDeviceGrayColorSpace::getCMYK(const GfxColor *color, GfxCMYK *cmyk) const
{
cmyk->c = cmyk->m = cmyk->y = 0;
cmyk->k = clip01(gfxColorComp1 - color->c[0]);
}
void GfxDeviceGrayColorSpace::getDeviceN(const GfxColor *color, GfxColor *deviceN) const
{
clearGfxColor(deviceN);
deviceN->c[3] = clip01(gfxColorComp1 - color->c[0]);
}
void GfxDeviceGrayColorSpace::getDefaultColor(GfxColor *color) const
{
color->c[0] = 0;
}
//------------------------------------------------------------------------
// GfxCalGrayColorSpace
//------------------------------------------------------------------------
GfxCalGrayColorSpace::GfxCalGrayColorSpace()
{
whiteX = whiteY = whiteZ = 1;
blackX = blackY = blackZ = 0;
gamma = 1;
}
GfxCalGrayColorSpace::~GfxCalGrayColorSpace() { }
std::unique_ptr<GfxColorSpace> GfxCalGrayColorSpace::copy() const
{
auto cs = std::make_unique<GfxCalGrayColorSpace>();
cs->whiteX = whiteX;
cs->whiteY = whiteY;
cs->whiteZ = whiteZ;
cs->blackX = blackX;
cs->blackY = blackY;
cs->blackZ = blackZ;
cs->gamma = gamma;
#ifdef USE_CMS
cs->transform = transform;
#endif
return cs;
}
// This is the inverse of MatrixLMN in Example 4.10 from the PostScript
// Language Reference, Third Edition.
static const double xyzrgb[3][3] = { { 3.240449, -1.537136, -0.498531 }, { -0.969265, 1.876011, 0.041556 }, { 0.055643, -0.204026, 1.057229 } };
// From the same reference as above, the inverse of the DecodeLMN function.
// This is essentially the gamma function of the sRGB profile.
static double srgb_gamma_function(double x)
{
// 0.04045 is what lcms2 uses, but the PS Reference Example 4.10 specifies 0.03928???
// if (x <= 0.04045 / 12.92321) {
if (x <= 0.03928 / 12.92321) {
return x * 12.92321;
}
return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
}
// D65 is the white point of the sRGB profile as it is specified above in the xyzrgb array
static const double white_d65_X = 0.9505;
static const double white_d65_Y = 1.0;
static const double white_d65_Z = 1.0890;
#ifdef USE_CMS
// D50 is the default white point as used in ICC profiles and in the lcms2 library
static const double white_d50_X = 0.96422;
static const double white_d50_Y = 1.0;
static const double white_d50_Z = 0.82521;
static void inline bradford_transform_to_d50(double &X, double &Y, double &Z, const double source_whiteX, const double source_whiteY, const double source_whiteZ)
{
if (source_whiteX == white_d50_X && source_whiteY == white_d50_Y && source_whiteZ == white_d50_Z) {
// early exit if noop
return;
}
// at first apply Bradford matrix
double rho_in = 0.8951000 * X + 0.2664000 * Y - 0.1614000 * Z;
double gamma_in = -0.7502000 * X + 1.7135000 * Y + 0.0367000 * Z;
double beta_in = 0.0389000 * X - 0.0685000 * Y + 1.0296000 * Z;
// apply a diagonal matrix with the diagonal entries being the inverse bradford-transformed white point
rho_in /= 0.8951000 * source_whiteX + 0.2664000 * source_whiteY - 0.1614000 * source_whiteZ;
gamma_in /= -0.7502000 * source_whiteX + 1.7135000 * source_whiteY + 0.0367000 * source_whiteZ;
beta_in /= 0.0389000 * source_whiteX - 0.0685000 * source_whiteY + 1.0296000 * source_whiteZ;
// now revert the two steps above, but substituting the source white point by the device white point (D50)
// Since the white point is known a priori this has been combined into a single operation.
X = 0.98332566 * rho_in - 0.15005819 * gamma_in + 0.13095252 * beta_in;
Y = 0.43069901 * rho_in + 0.52894900 * gamma_in + 0.04035199 * beta_in;
Z = 0.00849698 * rho_in + 0.04086079 * gamma_in + 0.79284618 * beta_in;
}
#endif
static void inline bradford_transform_to_d65(double &X, double &Y, double &Z, const double source_whiteX, const double source_whiteY, const double source_whiteZ)
{
if (source_whiteX == white_d65_X && source_whiteY == white_d65_Y && source_whiteZ == white_d65_Z) {
// early exit if noop
return;
}
// at first apply Bradford matrix
double rho_in = 0.8951000 * X + 0.2664000 * Y - 0.1614000 * Z;
double gamma_in = -0.7502000 * X + 1.7135000 * Y + 0.0367000 * Z;
double beta_in = 0.0389000 * X - 0.0685000 * Y + 1.0296000 * Z;
// apply a diagonal matrix with the diagonal entries being the inverse bradford-transformed white point
rho_in /= 0.8951000 * source_whiteX + 0.2664000 * source_whiteY - 0.1614000 * source_whiteZ;
gamma_in /= -0.7502000 * source_whiteX + 1.7135000 * source_whiteY + 0.0367000 * source_whiteZ;
beta_in /= 0.0389000 * source_whiteX - 0.0685000 * source_whiteY + 1.0296000 * source_whiteZ;
// now revert the two steps above, but substituting the source white point by the device white point (D65)
// Since the white point is known a priori this has been combined into a single operation.
X = 0.92918329 * rho_in - 0.15299782 * gamma_in + 0.17428453 * beta_in;
Y = 0.40698452 * rho_in + 0.53931108 * gamma_in + 0.05370440 * beta_in;
Z = -0.00802913 * rho_in + 0.04166125 * gamma_in + 1.05519788 * beta_in;
}
std::unique_ptr<GfxColorSpace> GfxCalGrayColorSpace::parse(Array *arr, GfxState *state)
{
Object obj1, obj2;
obj1 = arr->get(1);
if (!obj1.isDict()) {
error(errSyntaxWarning, -1, "Bad CalGray color space");
return {};
}
auto cs = std::make_unique<GfxCalGrayColorSpace>();
obj2 = obj1.dictLookup("WhitePoint");
if (obj2.isArray() && obj2.arrayGetLength() == 3) {
cs->whiteX = obj2.arrayGet(0).getNumWithDefaultValue(1);
cs->whiteY = obj2.arrayGet(1).getNumWithDefaultValue(1);
cs->whiteZ = obj2.arrayGet(2).getNumWithDefaultValue(1);
}
obj2 = obj1.dictLookup("BlackPoint");
if (obj2.isArray() && obj2.arrayGetLength() == 3) {
cs->blackX = obj2.arrayGet(0).getNumWithDefaultValue(0);
cs->blackY = obj2.arrayGet(1).getNumWithDefaultValue(0);
cs->blackZ = obj2.arrayGet(2).getNumWithDefaultValue(0);
}
cs->gamma = obj1.dictLookup("Gamma").getNumWithDefaultValue(1);
#ifdef USE_CMS
cs->transform = (state != nullptr) ? state->getXYZ2DisplayTransform() : nullptr;
#endif
return cs;
}
// convert CalGray to media XYZ color space
// (not multiply by the white point)
void GfxCalGrayColorSpace::getXYZ(const GfxColor *color, double *pX, double *pY, double *pZ) const
{
const double A = colToDbl(color->c[0]);
const double xyzColor = pow(A, gamma);
*pX = xyzColor;
*pY = xyzColor;
*pZ = xyzColor;
}
void GfxCalGrayColorSpace::getGray(const GfxColor *color, GfxGray *gray) const
{
GfxRGB rgb;
#ifdef USE_CMS
if (transform && transform->getTransformPixelType() == PT_GRAY) {
unsigned char out[gfxColorMaxComps];
double in[gfxColorMaxComps];
double X, Y, Z;
getXYZ(color, &X, &Y, &Z);
bradford_transform_to_d50(X, Y, Z, whiteX, whiteY, whiteZ);
in[0] = X;
in[1] = Y;
in[2] = Z;
transform->doTransform(in, out, 1);
*gray = byteToCol(out[0]);
return;
}
#endif
getRGB(color, &rgb);
*gray = clip01((GfxColorComp)(0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b + 0.5));
}
void GfxCalGrayColorSpace::getRGB(const GfxColor *color, GfxRGB *rgb) const
{
double X, Y, Z;
double r, g, b;
getXYZ(color, &X, &Y, &Z);
#ifdef USE_CMS
if (transform && transform->getTransformPixelType() == PT_RGB) {
unsigned char out[gfxColorMaxComps];
double in[gfxColorMaxComps];
bradford_transform_to_d50(X, Y, Z, whiteX, whiteY, whiteZ);
in[0] = X;
in[1] = Y;
in[2] = Z;
transform->doTransform(in, out, 1);
rgb->r = byteToCol(out[0]);
rgb->g = byteToCol(out[1]);
rgb->b = byteToCol(out[2]);
return;
}
#endif
bradford_transform_to_d65(X, Y, Z, whiteX, whiteY, whiteZ);
// convert XYZ to RGB, including gamut mapping and gamma correction
r = xyzrgb[0][0] * X + xyzrgb[0][1] * Y + xyzrgb[0][2] * Z;
g = xyzrgb[1][0] * X + xyzrgb[1][1] * Y + xyzrgb[1][2] * Z;
b = xyzrgb[2][0] * X + xyzrgb[2][1] * Y + xyzrgb[2][2] * Z;
rgb->r = dblToCol(srgb_gamma_function(clip01(r)));
rgb->g = dblToCol(srgb_gamma_function(clip01(g)));
rgb->b = dblToCol(srgb_gamma_function(clip01(b)));
}
void GfxCalGrayColorSpace::getCMYK(const GfxColor *color, GfxCMYK *cmyk) const
{
GfxRGB rgb;
GfxColorComp c, m, y, k;
#ifdef USE_CMS
if (transform && transform->getTransformPixelType() == PT_CMYK) {
double in[gfxColorMaxComps];
unsigned char out[gfxColorMaxComps];
double X, Y, Z;
getXYZ(color, &X, &Y, &Z);
bradford_transform_to_d50(X, Y, Z, whiteX, whiteY, whiteZ);
in[0] = X;
in[1] = Y;
in[2] = Z;
transform->doTransform(in, out, 1);
cmyk->c = byteToCol(out[0]);
cmyk->m = byteToCol(out[1]);
cmyk->y = byteToCol(out[2]);
cmyk->k = byteToCol(out[3]);
return;
}
#endif
getRGB(color, &rgb);
c = clip01(gfxColorComp1 - rgb.r);
m = clip01(gfxColorComp1 - rgb.g);
y = clip01(gfxColorComp1 - rgb.b);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;
cmyk->k = k;
}
void GfxCalGrayColorSpace::getDeviceN(const GfxColor *color, GfxColor *deviceN) const
{
GfxCMYK cmyk;
clearGfxColor(deviceN);
getCMYK(color, &cmyk);
deviceN->c[0] = cmyk.c;
deviceN->c[1] = cmyk.m;
deviceN->c[2] = cmyk.y;
deviceN->c[3] = cmyk.k;
}
void GfxCalGrayColorSpace::getDefaultColor(GfxColor *color) const
{
color->c[0] = 0;
}
//------------------------------------------------------------------------
// GfxDeviceRGBColorSpace
//------------------------------------------------------------------------
GfxDeviceRGBColorSpace::GfxDeviceRGBColorSpace() { }
GfxDeviceRGBColorSpace::~GfxDeviceRGBColorSpace() { }
std::unique_ptr<GfxColorSpace> GfxDeviceRGBColorSpace::copy() const
{
return std::make_unique<GfxDeviceRGBColorSpace>();
}
void GfxDeviceRGBColorSpace::getGray(const GfxColor *color, GfxGray *gray) const
{
*gray = clip01((GfxColorComp)(0.3 * color->c[0] + 0.59 * color->c[1] + 0.11 * color->c[2] + 0.5));
}
void GfxDeviceRGBColorSpace::getGrayLine(unsigned char *in, unsigned char *out, int length)
{
int i;
for (i = 0; i < length; i++) {
out[i] = (in[i * 3 + 0] * 19595 + in[i * 3 + 1] * 38469 + in[i * 3 + 2] * 7472) / 65536;
}
}
void GfxDeviceRGBColorSpace::getRGB(const GfxColor *color, GfxRGB *rgb) const
{
rgb->r = clip01(color->c[0]);
rgb->g = clip01(color->c[1]);
rgb->b = clip01(color->c[2]);
}
void GfxDeviceRGBColorSpace::getRGBLine(unsigned char *in, unsigned int *out, int length)
{
unsigned char *p;
int i;
for (i = 0, p = in; i < length; i++, p += 3) {
out[i] = (p[0] << 16) | (p[1] << 8) | (p[2] << 0);
}
}
void GfxDeviceRGBColorSpace::getRGBLine(unsigned char *in, unsigned char *out, int length)
{
for (int i = 0; i < length; i++) {
*out++ = *in++;
*out++ = *in++;
*out++ = *in++;
}
}
void GfxDeviceRGBColorSpace::getRGBXLine(unsigned char *in, unsigned char *out, int length)
{
for (int i = 0; i < length; i++) {
*out++ = *in++;
*out++ = *in++;
*out++ = *in++;
*out++ = 255;
}
}
void GfxDeviceRGBColorSpace::getCMYKLine(unsigned char *in, unsigned char *out, int length)
{
GfxColorComp c, m, y, k;
for (int i = 0; i < length; i++) {
c = byteToCol(255 - *in++);
m = byteToCol(255 - *in++);
y = byteToCol(255 - *in++);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
*out++ = colToByte(c - k);
*out++ = colToByte(m - k);
*out++ = colToByte(y - k);
*out++ = colToByte(k);
}
}
void GfxDeviceRGBColorSpace::getDeviceNLine(unsigned char *in, unsigned char *out, int length)
{
GfxColorComp c, m, y, k;
for (int i = 0; i < length; i++) {
for (int j = 0; j < SPOT_NCOMPS + 4; j++) {
out[j] = 0;
}
c = byteToCol(255 - *in++);
m = byteToCol(255 - *in++);
y = byteToCol(255 - *in++);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
out[0] = colToByte(c - k);
out[1] = colToByte(m - k);
out[2] = colToByte(y - k);
out[3] = colToByte(k);
out += (SPOT_NCOMPS + 4);
}
}
void GfxDeviceRGBColorSpace::getCMYK(const GfxColor *color, GfxCMYK *cmyk) const
{
GfxColorComp c, m, y, k;
c = clip01(gfxColorComp1 - color->c[0]);
m = clip01(gfxColorComp1 - color->c[1]);
y = clip01(gfxColorComp1 - color->c[2]);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;