-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
cmsvirt.c
1217 lines (879 loc) · 37 KB
/
cmsvirt.c
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
//---------------------------------------------------------------------------------
//
// Little Color Management System
// Copyright (c) 1998-2022 Marti Maria Saguer
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//---------------------------------------------------------------------------------
//
#include "lcms2_internal.h"
// Virtual (built-in) profiles
// -----------------------------------------------------------------------------------
static
cmsBool SetTextTags(cmsHPROFILE hProfile, const wchar_t* Description)
{
cmsMLU *DescriptionMLU, *CopyrightMLU;
cmsBool rc = FALSE;
cmsContext ContextID = cmsGetProfileContextID(hProfile);
DescriptionMLU = cmsMLUalloc(ContextID, 1);
CopyrightMLU = cmsMLUalloc(ContextID, 1);
if (DescriptionMLU == NULL || CopyrightMLU == NULL) goto Error;
if (!cmsMLUsetWide(DescriptionMLU, "en", "US", Description)) goto Error;
if (!cmsMLUsetWide(CopyrightMLU, "en", "US", L"No copyright, use freely")) goto Error;
if (!cmsWriteTag(hProfile, cmsSigProfileDescriptionTag, DescriptionMLU)) goto Error;
if (!cmsWriteTag(hProfile, cmsSigCopyrightTag, CopyrightMLU)) goto Error;
rc = TRUE;
Error:
if (DescriptionMLU)
cmsMLUfree(DescriptionMLU);
if (CopyrightMLU)
cmsMLUfree(CopyrightMLU);
return rc;
}
static
cmsBool SetSeqDescTag(cmsHPROFILE hProfile, const char* Model)
{
cmsBool rc = FALSE;
cmsContext ContextID = cmsGetProfileContextID(hProfile);
cmsSEQ* Seq = cmsAllocProfileSequenceDescription(ContextID, 1);
if (Seq == NULL) return FALSE;
Seq->seq[0].deviceMfg = (cmsSignature) 0;
Seq->seq[0].deviceModel = (cmsSignature) 0;
#ifdef CMS_DONT_USE_INT64
Seq->seq[0].attributes[0] = 0;
Seq->seq[0].attributes[1] = 0;
#else
Seq->seq[0].attributes = 0;
#endif
Seq->seq[0].technology = (cmsTechnologySignature) 0;
cmsMLUsetASCII( Seq->seq[0].Manufacturer, cmsNoLanguage, cmsNoCountry, "Little CMS");
cmsMLUsetASCII( Seq->seq[0].Model, cmsNoLanguage, cmsNoCountry, Model);
if (!_cmsWriteProfileSequence(hProfile, Seq)) goto Error;
rc = TRUE;
Error:
if (Seq)
cmsFreeProfileSequenceDescription(Seq);
return rc;
}
// This function creates a profile based on White point, primaries and
// transfer functions.
cmsHPROFILE CMSEXPORT cmsCreateRGBProfileTHR(cmsContext ContextID,
const cmsCIExyY* WhitePoint,
const cmsCIExyYTRIPLE* Primaries,
cmsToneCurve* const TransferFunction[3])
{
cmsHPROFILE hICC;
cmsMAT3 MColorants;
cmsCIEXYZTRIPLE Colorants;
cmsCIExyY MaxWhite;
cmsMAT3 CHAD;
cmsCIEXYZ WhitePointXYZ;
hICC = cmsCreateProfilePlaceholder(ContextID);
if (!hICC) // can't allocate
return NULL;
cmsSetProfileVersion(hICC, 4.3);
cmsSetDeviceClass(hICC, cmsSigDisplayClass);
cmsSetColorSpace(hICC, cmsSigRgbData);
cmsSetPCS(hICC, cmsSigXYZData);
cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
// Implement profile using following tags:
//
// 1 cmsSigProfileDescriptionTag
// 2 cmsSigMediaWhitePointTag
// 3 cmsSigRedColorantTag
// 4 cmsSigGreenColorantTag
// 5 cmsSigBlueColorantTag
// 6 cmsSigRedTRCTag
// 7 cmsSigGreenTRCTag
// 8 cmsSigBlueTRCTag
// 9 Chromatic adaptation Tag
// This conforms a standard RGB DisplayProfile as says ICC, and then I add (As per addendum II)
// 10 cmsSigChromaticityTag
if (!SetTextTags(hICC, L"RGB built-in")) goto Error;
if (WhitePoint) {
if (!cmsWriteTag(hICC, cmsSigMediaWhitePointTag, cmsD50_XYZ())) goto Error;
cmsxyY2XYZ(&WhitePointXYZ, WhitePoint);
_cmsAdaptationMatrix(&CHAD, NULL, &WhitePointXYZ, cmsD50_XYZ());
// This is a V4 tag, but many CMM does read and understand it no matter which version
if (!cmsWriteTag(hICC, cmsSigChromaticAdaptationTag, (void*) &CHAD)) goto Error;
}
if (WhitePoint && Primaries) {
MaxWhite.x = WhitePoint -> x;
MaxWhite.y = WhitePoint -> y;
MaxWhite.Y = 1.0;
if (!_cmsBuildRGB2XYZtransferMatrix(&MColorants, &MaxWhite, Primaries)) goto Error;
Colorants.Red.X = MColorants.v[0].n[0];
Colorants.Red.Y = MColorants.v[1].n[0];
Colorants.Red.Z = MColorants.v[2].n[0];
Colorants.Green.X = MColorants.v[0].n[1];
Colorants.Green.Y = MColorants.v[1].n[1];
Colorants.Green.Z = MColorants.v[2].n[1];
Colorants.Blue.X = MColorants.v[0].n[2];
Colorants.Blue.Y = MColorants.v[1].n[2];
Colorants.Blue.Z = MColorants.v[2].n[2];
if (!cmsWriteTag(hICC, cmsSigRedColorantTag, (void*) &Colorants.Red)) goto Error;
if (!cmsWriteTag(hICC, cmsSigBlueColorantTag, (void*) &Colorants.Blue)) goto Error;
if (!cmsWriteTag(hICC, cmsSigGreenColorantTag, (void*) &Colorants.Green)) goto Error;
}
if (TransferFunction) {
// Tries to minimize space. Thanks to Richard Hughes for this nice idea
if (!cmsWriteTag(hICC, cmsSigRedTRCTag, (void*) TransferFunction[0])) goto Error;
if (TransferFunction[1] == TransferFunction[0]) {
if (!cmsLinkTag (hICC, cmsSigGreenTRCTag, cmsSigRedTRCTag)) goto Error;
} else {
if (!cmsWriteTag(hICC, cmsSigGreenTRCTag, (void*) TransferFunction[1])) goto Error;
}
if (TransferFunction[2] == TransferFunction[0]) {
if (!cmsLinkTag (hICC, cmsSigBlueTRCTag, cmsSigRedTRCTag)) goto Error;
} else {
if (!cmsWriteTag(hICC, cmsSigBlueTRCTag, (void*) TransferFunction[2])) goto Error;
}
}
if (Primaries) {
if (!cmsWriteTag(hICC, cmsSigChromaticityTag, (void*) Primaries)) goto Error;
}
return hICC;
Error:
if (hICC)
cmsCloseProfile(hICC);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateRGBProfile(const cmsCIExyY* WhitePoint,
const cmsCIExyYTRIPLE* Primaries,
cmsToneCurve* const TransferFunction[3])
{
return cmsCreateRGBProfileTHR(NULL, WhitePoint, Primaries, TransferFunction);
}
// This function creates a profile based on White point and transfer function.
cmsHPROFILE CMSEXPORT cmsCreateGrayProfileTHR(cmsContext ContextID,
const cmsCIExyY* WhitePoint,
const cmsToneCurve* TransferFunction)
{
cmsHPROFILE hICC;
cmsCIEXYZ tmp;
hICC = cmsCreateProfilePlaceholder(ContextID);
if (!hICC) // can't allocate
return NULL;
cmsSetProfileVersion(hICC, 4.3);
cmsSetDeviceClass(hICC, cmsSigDisplayClass);
cmsSetColorSpace(hICC, cmsSigGrayData);
cmsSetPCS(hICC, cmsSigXYZData);
cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
// Implement profile using following tags:
//
// 1 cmsSigProfileDescriptionTag
// 2 cmsSigMediaWhitePointTag
// 3 cmsSigGrayTRCTag
// This conforms a standard Gray DisplayProfile
// Fill-in the tags
if (!SetTextTags(hICC, L"gray built-in")) goto Error;
if (WhitePoint) {
cmsxyY2XYZ(&tmp, WhitePoint);
if (!cmsWriteTag(hICC, cmsSigMediaWhitePointTag, (void*) &tmp)) goto Error;
}
if (TransferFunction) {
if (!cmsWriteTag(hICC, cmsSigGrayTRCTag, (void*) TransferFunction)) goto Error;
}
return hICC;
Error:
if (hICC)
cmsCloseProfile(hICC);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateGrayProfile(const cmsCIExyY* WhitePoint,
const cmsToneCurve* TransferFunction)
{
return cmsCreateGrayProfileTHR(NULL, WhitePoint, TransferFunction);
}
// This is a devicelink operating in the target colorspace with as many transfer functions as components
cmsHPROFILE CMSEXPORT cmsCreateLinearizationDeviceLinkTHR(cmsContext ContextID,
cmsColorSpaceSignature ColorSpace,
cmsToneCurve* const TransferFunctions[])
{
cmsHPROFILE hICC;
cmsPipeline* Pipeline;
cmsUInt32Number nChannels;
hICC = cmsCreateProfilePlaceholder(ContextID);
if (!hICC)
return NULL;
cmsSetProfileVersion(hICC, 4.3);
cmsSetDeviceClass(hICC, cmsSigLinkClass);
cmsSetColorSpace(hICC, ColorSpace);
cmsSetPCS(hICC, ColorSpace);
cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
// Set up channels
nChannels = cmsChannelsOf(ColorSpace);
// Creates a Pipeline with prelinearization step only
Pipeline = cmsPipelineAlloc(ContextID, nChannels, nChannels);
if (Pipeline == NULL) goto Error;
// Copy tables to Pipeline
if (!cmsPipelineInsertStage(Pipeline, cmsAT_BEGIN, cmsStageAllocToneCurves(ContextID, nChannels, TransferFunctions)))
goto Error;
// Create tags
if (!SetTextTags(hICC, L"Linearization built-in")) goto Error;
if (!cmsWriteTag(hICC, cmsSigAToB0Tag, (void*) Pipeline)) goto Error;
if (!SetSeqDescTag(hICC, "Linearization built-in")) goto Error;
// Pipeline is already on virtual profile
cmsPipelineFree(Pipeline);
// Ok, done
return hICC;
Error:
cmsPipelineFree(Pipeline);
if (hICC)
cmsCloseProfile(hICC);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateLinearizationDeviceLink(cmsColorSpaceSignature ColorSpace,
cmsToneCurve* const TransferFunctions[])
{
return cmsCreateLinearizationDeviceLinkTHR(NULL, ColorSpace, TransferFunctions);
}
// Ink-limiting algorithm
//
// Sum = C + M + Y + K
// If Sum > InkLimit
// Ratio= 1 - (Sum - InkLimit) / (C + M + Y)
// if Ratio <0
// Ratio=0
// endif
// Else
// Ratio=1
// endif
//
// C = Ratio * C
// M = Ratio * M
// Y = Ratio * Y
// K: Does not change
static
int InkLimitingSampler(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo)
{
cmsFloat64Number InkLimit = *(cmsFloat64Number *) Cargo;
cmsFloat64Number SumCMY, SumCMYK, Ratio;
InkLimit = (InkLimit * 655.35);
SumCMY = (cmsFloat64Number) In[0] + In[1] + In[2];
SumCMYK = SumCMY + In[3];
if (SumCMYK > InkLimit) {
Ratio = 1 - ((SumCMYK - InkLimit) / SumCMY);
if (Ratio < 0)
Ratio = 0;
}
else Ratio = 1;
Out[0] = _cmsQuickSaturateWord(In[0] * Ratio); // C
Out[1] = _cmsQuickSaturateWord(In[1] * Ratio); // M
Out[2] = _cmsQuickSaturateWord(In[2] * Ratio); // Y
Out[3] = In[3]; // K (untouched)
return TRUE;
}
// This is a devicelink operating in CMYK for ink-limiting
cmsHPROFILE CMSEXPORT cmsCreateInkLimitingDeviceLinkTHR(cmsContext ContextID,
cmsColorSpaceSignature ColorSpace,
cmsFloat64Number Limit)
{
cmsHPROFILE hICC;
cmsPipeline* LUT;
cmsStage* CLUT;
cmsUInt32Number nChannels;
if (ColorSpace != cmsSigCmykData) {
cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "InkLimiting: Only CMYK currently supported");
return NULL;
}
if (Limit < 0.0 || Limit > 400) {
cmsSignalError(ContextID, cmsERROR_RANGE, "InkLimiting: Limit should be between 0..400");
if (Limit < 0) Limit = 0;
if (Limit > 400) Limit = 400;
}
hICC = cmsCreateProfilePlaceholder(ContextID);
if (!hICC) // can't allocate
return NULL;
cmsSetProfileVersion(hICC, 4.3);
cmsSetDeviceClass(hICC, cmsSigLinkClass);
cmsSetColorSpace(hICC, ColorSpace);
cmsSetPCS(hICC, ColorSpace);
cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
// Creates a Pipeline with 3D grid only
LUT = cmsPipelineAlloc(ContextID, 4, 4);
if (LUT == NULL) goto Error;
nChannels = cmsChannelsOf(ColorSpace);
CLUT = cmsStageAllocCLut16bit(ContextID, 17, nChannels, nChannels, NULL);
if (CLUT == NULL) goto Error;
if (!cmsStageSampleCLut16bit(CLUT, InkLimitingSampler, (void*) &Limit, 0)) goto Error;
if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, nChannels)) ||
!cmsPipelineInsertStage(LUT, cmsAT_END, CLUT) ||
!cmsPipelineInsertStage(LUT, cmsAT_END, _cmsStageAllocIdentityCurves(ContextID, nChannels)))
goto Error;
// Create tags
if (!SetTextTags(hICC, L"ink-limiting built-in")) goto Error;
if (!cmsWriteTag(hICC, cmsSigAToB0Tag, (void*) LUT)) goto Error;
if (!SetSeqDescTag(hICC, "ink-limiting built-in")) goto Error;
// cmsPipeline is already on virtual profile
cmsPipelineFree(LUT);
// Ok, done
return hICC;
Error:
if (LUT != NULL)
cmsPipelineFree(LUT);
if (hICC != NULL)
cmsCloseProfile(hICC);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateInkLimitingDeviceLink(cmsColorSpaceSignature ColorSpace, cmsFloat64Number Limit)
{
return cmsCreateInkLimitingDeviceLinkTHR(NULL, ColorSpace, Limit);
}
// Creates a fake Lab identity.
cmsHPROFILE CMSEXPORT cmsCreateLab2ProfileTHR(cmsContext ContextID, const cmsCIExyY* WhitePoint)
{
cmsHPROFILE hProfile;
cmsPipeline* LUT = NULL;
hProfile = cmsCreateRGBProfileTHR(ContextID, WhitePoint == NULL ? cmsD50_xyY() : WhitePoint, NULL, NULL);
if (hProfile == NULL) return NULL;
cmsSetProfileVersion(hProfile, 2.1);
cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
cmsSetColorSpace(hProfile, cmsSigLabData);
cmsSetPCS(hProfile, cmsSigLabData);
if (!SetTextTags(hProfile, L"Lab identity built-in")) return NULL;
// An identity LUT is all we need
LUT = cmsPipelineAlloc(ContextID, 3, 3);
if (LUT == NULL) goto Error;
if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCLut(ContextID, 3)))
goto Error;
if (!cmsWriteTag(hProfile, cmsSigAToB0Tag, LUT)) goto Error;
cmsPipelineFree(LUT);
return hProfile;
Error:
if (LUT != NULL)
cmsPipelineFree(LUT);
if (hProfile != NULL)
cmsCloseProfile(hProfile);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateLab2Profile(const cmsCIExyY* WhitePoint)
{
return cmsCreateLab2ProfileTHR(NULL, WhitePoint);
}
// Creates a fake Lab V4 identity.
cmsHPROFILE CMSEXPORT cmsCreateLab4ProfileTHR(cmsContext ContextID, const cmsCIExyY* WhitePoint)
{
cmsHPROFILE hProfile;
cmsPipeline* LUT = NULL;
hProfile = cmsCreateRGBProfileTHR(ContextID, WhitePoint == NULL ? cmsD50_xyY() : WhitePoint, NULL, NULL);
if (hProfile == NULL) return NULL;
cmsSetProfileVersion(hProfile, 4.3);
cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
cmsSetColorSpace(hProfile, cmsSigLabData);
cmsSetPCS(hProfile, cmsSigLabData);
if (!SetTextTags(hProfile, L"Lab identity built-in")) goto Error;
// An empty LUTs is all we need
LUT = cmsPipelineAlloc(ContextID, 3, 3);
if (LUT == NULL) goto Error;
if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, 3)))
goto Error;
if (!cmsWriteTag(hProfile, cmsSigAToB0Tag, LUT)) goto Error;
cmsPipelineFree(LUT);
return hProfile;
Error:
if (LUT != NULL)
cmsPipelineFree(LUT);
if (hProfile != NULL)
cmsCloseProfile(hProfile);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateLab4Profile(const cmsCIExyY* WhitePoint)
{
return cmsCreateLab4ProfileTHR(NULL, WhitePoint);
}
// Creates a fake XYZ identity
cmsHPROFILE CMSEXPORT cmsCreateXYZProfileTHR(cmsContext ContextID)
{
cmsHPROFILE hProfile;
cmsPipeline* LUT = NULL;
hProfile = cmsCreateRGBProfileTHR(ContextID, cmsD50_xyY(), NULL, NULL);
if (hProfile == NULL) return NULL;
cmsSetProfileVersion(hProfile, 4.3);
cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
cmsSetColorSpace(hProfile, cmsSigXYZData);
cmsSetPCS(hProfile, cmsSigXYZData);
if (!SetTextTags(hProfile, L"XYZ identity built-in")) goto Error;
// An identity LUT is all we need
LUT = cmsPipelineAlloc(ContextID, 3, 3);
if (LUT == NULL) goto Error;
if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, 3)))
goto Error;
if (!cmsWriteTag(hProfile, cmsSigAToB0Tag, LUT)) goto Error;
cmsPipelineFree(LUT);
return hProfile;
Error:
if (LUT != NULL)
cmsPipelineFree(LUT);
if (hProfile != NULL)
cmsCloseProfile(hProfile);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateXYZProfile(void)
{
return cmsCreateXYZProfileTHR(NULL);
}
//sRGB Curves are defined by:
//
//If R'sRGB,G'sRGB, B'sRGB < 0.04045
//
// R = R'sRGB / 12.92
// G = G'sRGB / 12.92
// B = B'sRGB / 12.92
//
//
//else if R'sRGB,G'sRGB, B'sRGB >= 0.04045
//
// R = ((R'sRGB + 0.055) / 1.055)^2.4
// G = ((G'sRGB + 0.055) / 1.055)^2.4
// B = ((B'sRGB + 0.055) / 1.055)^2.4
static
cmsToneCurve* Build_sRGBGamma(cmsContext ContextID)
{
cmsFloat64Number Parameters[5];
Parameters[0] = 2.4;
Parameters[1] = 1. / 1.055;
Parameters[2] = 0.055 / 1.055;
Parameters[3] = 1. / 12.92;
Parameters[4] = 0.04045;
return cmsBuildParametricToneCurve(ContextID, 4, Parameters);
}
// Create the ICC virtual profile for sRGB space
cmsHPROFILE CMSEXPORT cmsCreate_sRGBProfileTHR(cmsContext ContextID)
{
cmsCIExyY D65 = { 0.3127, 0.3290, 1.0 };
cmsCIExyYTRIPLE Rec709Primaries = {
{0.6400, 0.3300, 1.0},
{0.3000, 0.6000, 1.0},
{0.1500, 0.0600, 1.0}
};
cmsToneCurve* Gamma22[3];
cmsHPROFILE hsRGB;
// cmsWhitePointFromTemp(&D65, 6504);
Gamma22[0] = Gamma22[1] = Gamma22[2] = Build_sRGBGamma(ContextID);
if (Gamma22[0] == NULL) return NULL;
hsRGB = cmsCreateRGBProfileTHR(ContextID, &D65, &Rec709Primaries, Gamma22);
cmsFreeToneCurve(Gamma22[0]);
if (hsRGB == NULL) return NULL;
if (!SetTextTags(hsRGB, L"sRGB built-in")) {
cmsCloseProfile(hsRGB);
return NULL;
}
return hsRGB;
}
cmsHPROFILE CMSEXPORT cmsCreate_sRGBProfile(void)
{
return cmsCreate_sRGBProfileTHR(NULL);
}
typedef struct {
cmsFloat64Number Brightness;
cmsFloat64Number Contrast;
cmsFloat64Number Hue;
cmsFloat64Number Saturation;
cmsBool lAdjustWP;
cmsCIEXYZ WPsrc, WPdest;
} BCHSWADJUSTS, *LPBCHSWADJUSTS;
static
int bchswSampler(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo)
{
cmsCIELab LabIn, LabOut;
cmsCIELCh LChIn, LChOut;
cmsCIEXYZ XYZ;
LPBCHSWADJUSTS bchsw = (LPBCHSWADJUSTS) Cargo;
cmsLabEncoded2Float(&LabIn, In);
cmsLab2LCh(&LChIn, &LabIn);
// Do some adjusts on LCh
LChOut.L = LChIn.L * bchsw ->Contrast + bchsw ->Brightness;
LChOut.C = LChIn.C + bchsw -> Saturation;
LChOut.h = LChIn.h + bchsw -> Hue;
cmsLCh2Lab(&LabOut, &LChOut);
// Move white point in Lab
if (bchsw->lAdjustWP) {
cmsLab2XYZ(&bchsw->WPsrc, &XYZ, &LabOut);
cmsXYZ2Lab(&bchsw->WPdest, &LabOut, &XYZ);
}
// Back to encoded
cmsFloat2LabEncoded(Out, &LabOut);
return TRUE;
}
// Creates an abstract profile operating in Lab space for Brightness,
// contrast, Saturation and white point displacement
cmsHPROFILE CMSEXPORT cmsCreateBCHSWabstractProfileTHR(cmsContext ContextID,
cmsUInt32Number nLUTPoints,
cmsFloat64Number Bright,
cmsFloat64Number Contrast,
cmsFloat64Number Hue,
cmsFloat64Number Saturation,
cmsUInt32Number TempSrc,
cmsUInt32Number TempDest)
{
cmsHPROFILE hICC;
cmsPipeline* Pipeline;
BCHSWADJUSTS bchsw;
cmsCIExyY WhitePnt;
cmsStage* CLUT;
cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
cmsUInt32Number i;
bchsw.Brightness = Bright;
bchsw.Contrast = Contrast;
bchsw.Hue = Hue;
bchsw.Saturation = Saturation;
if (TempSrc == TempDest) {
bchsw.lAdjustWP = FALSE;
}
else {
bchsw.lAdjustWP = TRUE;
cmsWhitePointFromTemp(&WhitePnt, TempSrc);
cmsxyY2XYZ(&bchsw.WPsrc, &WhitePnt);
cmsWhitePointFromTemp(&WhitePnt, TempDest);
cmsxyY2XYZ(&bchsw.WPdest, &WhitePnt);
}
hICC = cmsCreateProfilePlaceholder(ContextID);
if (!hICC) // can't allocate
return NULL;
cmsSetDeviceClass(hICC, cmsSigAbstractClass);
cmsSetColorSpace(hICC, cmsSigLabData);
cmsSetPCS(hICC, cmsSigLabData);
cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
// Creates a Pipeline with 3D grid only
Pipeline = cmsPipelineAlloc(ContextID, 3, 3);
if (Pipeline == NULL) {
cmsCloseProfile(hICC);
return NULL;
}
for (i=0; i < MAX_INPUT_DIMENSIONS; i++) Dimensions[i] = nLUTPoints;
CLUT = cmsStageAllocCLut16bitGranular(ContextID, Dimensions, 3, 3, NULL);
if (CLUT == NULL) goto Error;
if (!cmsStageSampleCLut16bit(CLUT, bchswSampler, (void*) &bchsw, 0)) {
// Shouldn't reach here
goto Error;
}
if (!cmsPipelineInsertStage(Pipeline, cmsAT_END, CLUT)) {
goto Error;
}
// Create tags
if (!SetTextTags(hICC, L"BCHS built-in")) return NULL;
cmsWriteTag(hICC, cmsSigMediaWhitePointTag, (void*) cmsD50_XYZ());
cmsWriteTag(hICC, cmsSigAToB0Tag, (void*) Pipeline);
// Pipeline is already on virtual profile
cmsPipelineFree(Pipeline);
// Ok, done
return hICC;
Error:
cmsPipelineFree(Pipeline);
cmsCloseProfile(hICC);
return NULL;
}
CMSAPI cmsHPROFILE CMSEXPORT cmsCreateBCHSWabstractProfile(cmsUInt32Number nLUTPoints,
cmsFloat64Number Bright,
cmsFloat64Number Contrast,
cmsFloat64Number Hue,
cmsFloat64Number Saturation,
cmsUInt32Number TempSrc,
cmsUInt32Number TempDest)
{
return cmsCreateBCHSWabstractProfileTHR(NULL, nLUTPoints, Bright, Contrast, Hue, Saturation, TempSrc, TempDest);
}
// Creates a fake NULL profile. This profile return 1 channel as always 0.
// Is useful only for gamut checking tricks
cmsHPROFILE CMSEXPORT cmsCreateNULLProfileTHR(cmsContext ContextID)
{
cmsHPROFILE hProfile;
cmsPipeline* LUT = NULL;
cmsStage* PostLin;
cmsStage* OutLin;
cmsToneCurve* EmptyTab[3];
cmsUInt16Number Zero[2] = { 0, 0 };
const cmsFloat64Number PickLstarMatrix[] = { 1, 0, 0 };
hProfile = cmsCreateProfilePlaceholder(ContextID);
if (!hProfile) // can't allocate
return NULL;
cmsSetProfileVersion(hProfile, 4.3);
if (!SetTextTags(hProfile, L"NULL profile built-in")) goto Error;
cmsSetDeviceClass(hProfile, cmsSigOutputClass);
cmsSetColorSpace(hProfile, cmsSigGrayData);
cmsSetPCS(hProfile, cmsSigLabData);
// Create a valid ICC 4 structure
LUT = cmsPipelineAlloc(ContextID, 3, 1);
if (LUT == NULL) goto Error;
EmptyTab[0] = EmptyTab[1] = EmptyTab[2] = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
PostLin = cmsStageAllocToneCurves(ContextID, 3, EmptyTab);
OutLin = cmsStageAllocToneCurves(ContextID, 1, EmptyTab);
cmsFreeToneCurve(EmptyTab[0]);
if (!cmsPipelineInsertStage(LUT, cmsAT_END, PostLin))
goto Error;
if (!cmsPipelineInsertStage(LUT, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL)))
goto Error;
if (!cmsPipelineInsertStage(LUT, cmsAT_END, OutLin))
goto Error;
if (!cmsWriteTag(hProfile, cmsSigBToA0Tag, (void*) LUT)) goto Error;
if (!cmsWriteTag(hProfile, cmsSigMediaWhitePointTag, cmsD50_XYZ())) goto Error;
cmsPipelineFree(LUT);
return hProfile;
Error:
if (LUT != NULL)
cmsPipelineFree(LUT);
if (hProfile != NULL)
cmsCloseProfile(hProfile);
return NULL;
}
cmsHPROFILE CMSEXPORT cmsCreateNULLProfile(void)
{
return cmsCreateNULLProfileTHR(NULL);
}
static
int IsPCS(cmsColorSpaceSignature ColorSpace)
{
return (ColorSpace == cmsSigXYZData ||
ColorSpace == cmsSigLabData);
}
static
void FixColorSpaces(cmsHPROFILE hProfile,
cmsColorSpaceSignature ColorSpace,
cmsColorSpaceSignature PCS,
cmsUInt32Number dwFlags)
{
if (dwFlags & cmsFLAGS_GUESSDEVICECLASS) {
if (IsPCS(ColorSpace) && IsPCS(PCS)) {
cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
cmsSetColorSpace(hProfile, ColorSpace);
cmsSetPCS(hProfile, PCS);
return;
}
if (IsPCS(ColorSpace) && !IsPCS(PCS)) {
cmsSetDeviceClass(hProfile, cmsSigOutputClass);
cmsSetPCS(hProfile, ColorSpace);
cmsSetColorSpace(hProfile, PCS);
return;
}
if (IsPCS(PCS) && !IsPCS(ColorSpace)) {
cmsSetDeviceClass(hProfile, cmsSigInputClass);
cmsSetColorSpace(hProfile, ColorSpace);
cmsSetPCS(hProfile, PCS);
return;
}
}
cmsSetDeviceClass(hProfile, cmsSigLinkClass);
cmsSetColorSpace(hProfile, ColorSpace);
cmsSetPCS(hProfile, PCS);
}
// This function creates a named color profile dumping all the contents of transform to a single profile
// In this way, LittleCMS may be used to "group" several named color databases into a single profile.
// It has, however, several minor limitations. PCS is always Lab, which is not very critic since this
// is the normal PCS for named color profiles.
static
cmsHPROFILE CreateNamedColorDevicelink(cmsHTRANSFORM xform)
{
_cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
cmsHPROFILE hICC = NULL;
cmsUInt32Number i, nColors;
cmsNAMEDCOLORLIST *nc2 = NULL, *Original = NULL;
// Create an empty placeholder
hICC = cmsCreateProfilePlaceholder(v->ContextID);
if (hICC == NULL) return NULL;
// Critical information
cmsSetDeviceClass(hICC, cmsSigNamedColorClass);
cmsSetColorSpace(hICC, v ->ExitColorSpace);
cmsSetPCS(hICC, cmsSigLabData);
// Tag profile with information
if (!SetTextTags(hICC, L"Named color devicelink")) goto Error;
Original = cmsGetNamedColorList(xform);
if (Original == NULL) goto Error;
nColors = cmsNamedColorCount(Original);
nc2 = cmsDupNamedColorList(Original);
if (nc2 == NULL) goto Error;
// Colorant count now depends on the output space
nc2 ->ColorantCount = cmsPipelineOutputChannels(v ->Lut);
// Make sure we have proper formatters
cmsChangeBuffersFormat(xform, TYPE_NAMED_COLOR_INDEX,
FLOAT_SH(0) | COLORSPACE_SH(_cmsLCMScolorSpace(v ->ExitColorSpace))
| BYTES_SH(2) | CHANNELS_SH(cmsChannelsOf(v ->ExitColorSpace)));
// Apply the transfor to colorants.
for (i=0; i < nColors; i++) {
cmsDoTransform(xform, &i, nc2 ->List[i].DeviceColorant, 1);
}
if (!cmsWriteTag(hICC, cmsSigNamedColor2Tag, (void*) nc2)) goto Error;
cmsFreeNamedColorList(nc2);
return hICC;
Error:
if (hICC != NULL) cmsCloseProfile(hICC);
return NULL;
}
// This structure holds information about which MPU can be stored on a profile based on the version
typedef struct {
cmsBool IsV4; // Is a V4 tag?
cmsTagSignature RequiredTag; // Set to 0 for both types
cmsTagTypeSignature LutType; // The LUT type