1 //---------------------------------------------------------------------------------
2 //
3 // Little Color Management System
4 // Copyright (c) 1998-2020 Marti Maria Saguer
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the Software
11 // is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //---------------------------------------------------------------------------------
25 //
26
27 #include "lcms2_internal.h"
28
29 // Read tags using low-level functions, provides necessary glue code to adapt versions, etc.
30
31 // LUT tags
32 static const cmsTagSignature Device2PCS16[] = {cmsSigAToB0Tag, // Perceptual
33 cmsSigAToB1Tag, // Relative colorimetric
34 cmsSigAToB2Tag, // Saturation
35 cmsSigAToB1Tag }; // Absolute colorimetric
36
37 static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag, // Perceptual
38 cmsSigDToB1Tag, // Relative colorimetric
39 cmsSigDToB2Tag, // Saturation
40 cmsSigDToB3Tag }; // Absolute colorimetric
41
42 static const cmsTagSignature PCS2Device16[] = {cmsSigBToA0Tag, // Perceptual
43 cmsSigBToA1Tag, // Relative colorimetric
44 cmsSigBToA2Tag, // Saturation
45 cmsSigBToA1Tag }; // Absolute colorimetric
46
47 static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag, // Perceptual
48 cmsSigBToD1Tag, // Relative colorimetric
49 cmsSigBToD2Tag, // Saturation
50 cmsSigBToD3Tag }; // Absolute colorimetric
51
52
53 // Factors to convert from 1.15 fixed point to 0..1.0 range and vice-versa
54 #define InpAdj (1.0/MAX_ENCODEABLE_XYZ) // (65536.0/(65535.0*2.0))
55 #define OutpAdj (MAX_ENCODEABLE_XYZ) // ((2.0*65535.0)/65536.0)
56
57 // Several resources for gray conversions.
58 static const cmsFloat64Number GrayInputMatrix[] = { (InpAdj*cmsD50X), (InpAdj*cmsD50Y), (InpAdj*cmsD50Z) };
59 static const cmsFloat64Number OneToThreeInputMatrix[] = { 1, 1, 1 };
60 static const cmsFloat64Number PickYMatrix[] = { 0, (OutpAdj*cmsD50Y), 0 };
61 static const cmsFloat64Number PickLstarMatrix[] = { 1, 0, 0 };
62
63 // Get a media white point fixing some issues found in certain old profiles
_cmsReadMediaWhitePoint(cmsCIEXYZ * Dest,cmsHPROFILE hProfile)64 cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)
65 {
66 cmsCIEXYZ* Tag;
67
68 _cmsAssert(Dest != NULL);
69
70 Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
71
72 // If no wp, take D50
73 if (Tag == NULL) {
74 *Dest = *cmsD50_XYZ();
75 return TRUE;
76 }
77
78 // V2 display profiles should give D50
79 if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
80
81 if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
82 *Dest = *cmsD50_XYZ();
83 return TRUE;
84 }
85 }
86
87 // All seems ok
88 *Dest = *Tag;
89 return TRUE;
90 }
91
92
93 // Chromatic adaptation matrix. Fix some issues as well
_cmsReadCHAD(cmsMAT3 * Dest,cmsHPROFILE hProfile)94 cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)
95 {
96 cmsMAT3* Tag;
97
98 _cmsAssert(Dest != NULL);
99
100 Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);
101
102 if (Tag != NULL) {
103 *Dest = *Tag;
104 return TRUE;
105 }
106
107 // No CHAD available, default it to identity
108 _cmsMAT3identity(Dest);
109
110 // V2 display profiles should give D50
111 if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
112
113 if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
114
115 cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
116
117 if (White == NULL) {
118
119 _cmsMAT3identity(Dest);
120 return TRUE;
121 }
122
123 return _cmsAdaptationMatrix(Dest, NULL, White, cmsD50_XYZ());
124 }
125 }
126
127 return TRUE;
128 }
129
130
131 // Auxiliary, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper
132 static
ReadICCMatrixRGB2XYZ(cmsMAT3 * r,cmsHPROFILE hProfile)133 cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)
134 {
135 cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;
136
137 _cmsAssert(r != NULL);
138
139 PtrRed = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigRedColorantTag);
140 PtrGreen = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigGreenColorantTag);
141 PtrBlue = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigBlueColorantTag);
142
143 if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)
144 return FALSE;
145
146 _cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X, PtrBlue -> X);
147 _cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y, PtrBlue -> Y);
148 _cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z, PtrBlue -> Z);
149
150 return TRUE;
151 }
152
153
154 // Gray input pipeline
155 static
BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)156 cmsPipeline* BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)
157 {
158 cmsToneCurve *GrayTRC;
159 cmsPipeline* Lut;
160 cmsContext ContextID = cmsGetProfileContextID(hProfile);
161
162 GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
163 if (GrayTRC == NULL) return NULL;
164
165 Lut = cmsPipelineAlloc(ContextID, 1, 3);
166 if (Lut == NULL)
167 goto Error;
168
169 if (cmsGetPCS(hProfile) == cmsSigLabData) {
170
171 // In this case we implement the profile as an identity matrix plus 3 tone curves
172 cmsUInt16Number Zero[2] = { 0x8080, 0x8080 };
173 cmsToneCurve* EmptyTab;
174 cmsToneCurve* LabCurves[3];
175
176 EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
177
178 if (EmptyTab == NULL)
179 goto Error;
180
181 LabCurves[0] = GrayTRC;
182 LabCurves[1] = EmptyTab;
183 LabCurves[2] = EmptyTab;
184
185 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, OneToThreeInputMatrix, NULL)) ||
186 !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, LabCurves))) {
187 cmsFreeToneCurve(EmptyTab);
188 goto Error;
189 }
190
191 cmsFreeToneCurve(EmptyTab);
192
193 }
194 else {
195
196 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &GrayTRC)) ||
197 !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, GrayInputMatrix, NULL)))
198 goto Error;
199 }
200
201 return Lut;
202
203 Error:
204 cmsPipelineFree(Lut);
205 return NULL;
206 }
207
208 // RGB Matrix shaper
209 static
BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)210 cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
211 {
212 cmsPipeline* Lut;
213 cmsMAT3 Mat;
214 cmsToneCurve *Shapes[3];
215 cmsContext ContextID = cmsGetProfileContextID(hProfile);
216 int i, j;
217
218 if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
219
220 // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
221 // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
222 // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
223
224 for (i=0; i < 3; i++)
225 for (j=0; j < 3; j++)
226 Mat.v[i].n[j] *= InpAdj;
227
228
229 Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
230 Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
231 Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
232
233 if (!Shapes[0] || !Shapes[1] || !Shapes[2])
234 return NULL;
235
236 Lut = cmsPipelineAlloc(ContextID, 3, 3);
237 if (Lut != NULL) {
238
239 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes)) ||
240 !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL)))
241 goto Error;
242
243 // Note that it is certainly possible a single profile would have a LUT based
244 // tag for output working in lab and a matrix-shaper for the fallback cases.
245 // This is not allowed by the spec, but this code is tolerant to those cases
246 if (cmsGetPCS(hProfile) == cmsSigLabData) {
247
248 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID)))
249 goto Error;
250 }
251
252 }
253
254 return Lut;
255
256 Error:
257 cmsPipelineFree(Lut);
258 return NULL;
259 }
260
261
262
263 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
264 static
_cmsReadFloatInputTag(cmsHPROFILE hProfile,cmsTagSignature tagFloat)265 cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
266 {
267 cmsContext ContextID = cmsGetProfileContextID(hProfile);
268 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
269 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
270 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
271
272 if (Lut == NULL) return NULL;
273
274 // input and output of transform are in lcms 0..1 encoding. If XYZ or Lab spaces are used,
275 // these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)
276 if ( spc == cmsSigLabData)
277 {
278 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
279 goto Error;
280 }
281 else if (spc == cmsSigXYZData)
282 {
283 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
284 goto Error;
285 }
286
287 if ( PCS == cmsSigLabData)
288 {
289 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
290 goto Error;
291 }
292 else if( PCS == cmsSigXYZData)
293 {
294 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
295 goto Error;
296 }
297
298 return Lut;
299
300 Error:
301 cmsPipelineFree(Lut);
302 return NULL;
303 }
304
305
306 // Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
307 // is adjusted here in order to create a LUT that takes care of all those details.
308 // We add intent = 0xffffffff as a way to read matrix shaper always, no matter of other LUT
_cmsReadInputLUT(cmsHPROFILE hProfile,cmsUInt32Number Intent)309 cmsPipeline* CMSEXPORT _cmsReadInputLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent)
310 {
311 cmsTagTypeSignature OriginalType;
312 cmsTagSignature tag16;
313 cmsTagSignature tagFloat;
314 cmsContext ContextID = cmsGetProfileContextID(hProfile);
315
316 // On named color, take the appropriate tag
317 if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
318
319 cmsPipeline* Lut;
320 cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
321
322 if (nc == NULL) return NULL;
323
324 Lut = cmsPipelineAlloc(ContextID, 0, 0);
325 if (Lut == NULL) {
326 cmsFreeNamedColorList(nc);
327 return NULL;
328 }
329
330 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE)) ||
331 !cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID))) {
332 cmsPipelineFree(Lut);
333 return NULL;
334 }
335 return Lut;
336 }
337
338 // This is an attempt to reuse this function to retrieve the matrix-shaper as pipeline no
339 // matter other LUT are present and have precedence. Intent = 0xffffffff can be used for that.
340 if (Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {
341
342 tag16 = Device2PCS16[Intent];
343 tagFloat = Device2PCSFloat[Intent];
344
345 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
346
347 // Floating point LUT are always V4, but the encoding range is no
348 // longer 0..1.0, so we need to add an stage depending on the color space
349 return _cmsReadFloatInputTag(hProfile, tagFloat);
350 }
351
352 // Revert to perceptual if no tag is found
353 if (!cmsIsTag(hProfile, tag16)) {
354 tag16 = Device2PCS16[0];
355 }
356
357 if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
358
359 // Check profile version and LUT type. Do the necessary adjustments if needed
360
361 // First read the tag
362 cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
363 if (Lut == NULL) return NULL;
364
365 // After reading it, we have now info about the original type
366 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
367
368 // The profile owns the Lut, so we need to copy it
369 Lut = cmsPipelineDup(Lut);
370
371 // We need to adjust data only for Lab16 on output
372 if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
373 return Lut;
374
375 // If the input is Lab, add also a conversion at the begin
376 if (cmsGetColorSpace(hProfile) == cmsSigLabData &&
377 !cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
378 goto Error;
379
380 // Add a matrix for conversion V2 to V4 Lab PCS
381 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
382 goto Error;
383
384 return Lut;
385 Error:
386 cmsPipelineFree(Lut);
387 return NULL;
388 }
389 }
390
391 // Lut was not found, try to create a matrix-shaper
392
393 // Check if this is a grayscale profile.
394 if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
395
396 // if so, build appropriate conversion tables.
397 // The tables are the PCS iluminant, scaled across GrayTRC
398 return BuildGrayInputMatrixPipeline(hProfile);
399 }
400
401 // Not gray, create a normal matrix-shaper
402 return BuildRGBInputMatrixShaper(hProfile);
403 }
404
405 // ---------------------------------------------------------------------------------------------------------------
406
407 // Gray output pipeline.
408 // XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
409 // given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
410 // The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
411
412 static
BuildGrayOutputPipeline(cmsHPROFILE hProfile)413 cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
414 {
415 cmsToneCurve *GrayTRC, *RevGrayTRC;
416 cmsPipeline* Lut;
417 cmsContext ContextID = cmsGetProfileContextID(hProfile);
418
419 GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
420 if (GrayTRC == NULL) return NULL;
421
422 RevGrayTRC = cmsReverseToneCurve(GrayTRC);
423 if (RevGrayTRC == NULL) return NULL;
424
425 Lut = cmsPipelineAlloc(ContextID, 3, 1);
426 if (Lut == NULL) {
427 cmsFreeToneCurve(RevGrayTRC);
428 return NULL;
429 }
430
431 if (cmsGetPCS(hProfile) == cmsSigLabData) {
432
433 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL)))
434 goto Error;
435 }
436 else {
437 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickYMatrix, NULL)))
438 goto Error;
439 }
440
441 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))
442 goto Error;
443
444 cmsFreeToneCurve(RevGrayTRC);
445 return Lut;
446
447 Error:
448 cmsFreeToneCurve(RevGrayTRC);
449 cmsPipelineFree(Lut);
450 return NULL;
451 }
452
453
454 static
BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)455 cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
456 {
457 cmsPipeline* Lut;
458 cmsToneCurve *Shapes[3], *InvShapes[3];
459 cmsMAT3 Mat, Inv;
460 int i, j;
461 cmsContext ContextID = cmsGetProfileContextID(hProfile);
462
463 if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
464 return NULL;
465
466 if (!_cmsMAT3inverse(&Mat, &Inv))
467 return NULL;
468
469 // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
470 // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
471 // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
472
473 for (i=0; i < 3; i++)
474 for (j=0; j < 3; j++)
475 Inv.v[i].n[j] *= OutpAdj;
476
477 Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
478 Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
479 Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
480
481 if (!Shapes[0] || !Shapes[1] || !Shapes[2])
482 return NULL;
483
484 InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
485 InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
486 InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
487
488 if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
489 return NULL;
490 }
491
492 Lut = cmsPipelineAlloc(ContextID, 3, 3);
493 if (Lut != NULL) {
494
495 // Note that it is certainly possible a single profile would have a LUT based
496 // tag for output working in lab and a matrix-shaper for the fallback cases.
497 // This is not allowed by the spec, but this code is tolerant to those cases
498 if (cmsGetPCS(hProfile) == cmsSigLabData) {
499
500 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))
501 goto Error;
502 }
503
504 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||
505 !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))
506 goto Error;
507 }
508
509 cmsFreeToneCurveTriple(InvShapes);
510 return Lut;
511 Error:
512 cmsFreeToneCurveTriple(InvShapes);
513 cmsPipelineFree(Lut);
514 return NULL;
515 }
516
517
518 // Change CLUT interpolation to trilinear
519 static
ChangeInterpolationToTrilinear(cmsPipeline * Lut)520 void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
521 {
522 cmsStage* Stage;
523
524 for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
525 Stage != NULL;
526 Stage = cmsStageNext(Stage)) {
527
528 if (cmsStageType(Stage) == cmsSigCLutElemType) {
529
530 _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
531
532 CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
533 _cmsSetInterpolationRoutine(Lut->ContextID, CLUT ->Params);
534 }
535 }
536 }
537
538
539 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
540 static
_cmsReadFloatOutputTag(cmsHPROFILE hProfile,cmsTagSignature tagFloat)541 cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
542 {
543 cmsContext ContextID = cmsGetProfileContextID(hProfile);
544 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
545 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
546 cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
547
548 if (Lut == NULL) return NULL;
549
550 // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
551 // and since the formatter has already accommodated to 0..1.0, we should undo this change
552 if ( PCS == cmsSigLabData)
553 {
554 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
555 goto Error;
556 }
557 else
558 if (PCS == cmsSigXYZData)
559 {
560 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
561 goto Error;
562 }
563
564 // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
565 if ( dataSpace == cmsSigLabData)
566 {
567 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
568 goto Error;
569 }
570 else if (dataSpace == cmsSigXYZData)
571 {
572 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
573 goto Error;
574 }
575
576 return Lut;
577
578 Error:
579 cmsPipelineFree(Lut);
580 return NULL;
581 }
582
583 // Create an output MPE LUT from agiven profile. Version mismatches are handled here
_cmsReadOutputLUT(cmsHPROFILE hProfile,cmsUInt32Number Intent)584 cmsPipeline* CMSEXPORT _cmsReadOutputLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent)
585 {
586 cmsTagTypeSignature OriginalType;
587 cmsTagSignature tag16;
588 cmsTagSignature tagFloat;
589 cmsContext ContextID = cmsGetProfileContextID(hProfile);
590
591
592 if (Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {
593
594 tag16 = PCS2Device16[Intent];
595 tagFloat = PCS2DeviceFloat[Intent];
596
597 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
598
599 // Floating point LUT are always V4
600 return _cmsReadFloatOutputTag(hProfile, tagFloat);
601 }
602
603 // Revert to perceptual if no tag is found
604 if (!cmsIsTag(hProfile, tag16)) {
605 tag16 = PCS2Device16[0];
606 }
607
608 if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
609
610 // Check profile version and LUT type. Do the necessary adjustments if needed
611
612 // First read the tag
613 cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
614 if (Lut == NULL) return NULL;
615
616 // After reading it, we have info about the original type
617 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
618
619 // The profile owns the Lut, so we need to copy it
620 Lut = cmsPipelineDup(Lut);
621 if (Lut == NULL) return NULL;
622
623 // Now it is time for a controversial stuff. I found that for 3D LUTS using
624 // Lab used as indexer space, trilinear interpolation should be used
625 if (cmsGetPCS(hProfile) == cmsSigLabData)
626 ChangeInterpolationToTrilinear(Lut);
627
628 // We need to adjust data only for Lab and Lut16 type
629 if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
630 return Lut;
631
632 // Add a matrix for conversion V4 to V2 Lab PCS
633 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
634 goto Error;
635
636 // If the output is Lab, add also a conversion at the end
637 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
638 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
639 goto Error;
640
641 return Lut;
642 Error:
643 cmsPipelineFree(Lut);
644 return NULL;
645 }
646 }
647
648 // Lut not found, try to create a matrix-shaper
649
650 // Check if this is a grayscale profile.
651 if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
652
653 // if so, build appropriate conversion tables.
654 // The tables are the PCS iluminant, scaled across GrayTRC
655 return BuildGrayOutputPipeline(hProfile);
656 }
657
658 // Not gray, create a normal matrix-shaper, which only operates in XYZ space
659 return BuildRGBOutputMatrixShaper(hProfile);
660 }
661
662 // ---------------------------------------------------------------------------------------------------------------
663
664 // Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
665 static
_cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile,cmsTagSignature tagFloat)666 cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
667 {
668 cmsContext ContextID = cmsGetProfileContextID(hProfile);
669 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*)cmsReadTag(hProfile, tagFloat));
670 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
671 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
672
673 if (Lut == NULL) return NULL;
674
675 if (spc == cmsSigLabData)
676 {
677 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
678 goto Error;
679 }
680 else
681 if (spc == cmsSigXYZData)
682 {
683 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
684 goto Error;
685 }
686
687 if (PCS == cmsSigLabData)
688 {
689 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
690 goto Error;
691 }
692 else
693 if (PCS == cmsSigXYZData)
694 {
695 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
696 goto Error;
697 }
698
699 return Lut;
700 Error:
701 cmsPipelineFree(Lut);
702 return NULL;
703 }
704
705 // This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
706 // tag name here may default to AToB0
_cmsReadDevicelinkLUT(cmsHPROFILE hProfile,cmsUInt32Number Intent)707 cmsPipeline* CMSEXPORT _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent)
708 {
709 cmsPipeline* Lut;
710 cmsTagTypeSignature OriginalType;
711 cmsTagSignature tag16;
712 cmsTagSignature tagFloat;
713 cmsContext ContextID = cmsGetProfileContextID(hProfile);
714
715
716 if (Intent > INTENT_ABSOLUTE_COLORIMETRIC)
717 return NULL;
718
719 tag16 = Device2PCS16[Intent];
720 tagFloat = Device2PCSFloat[Intent];
721
722 // On named color, take the appropriate tag
723 if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
724
725 cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*)cmsReadTag(hProfile, cmsSigNamedColor2Tag);
726
727 if (nc == NULL) return NULL;
728
729 Lut = cmsPipelineAlloc(ContextID, 0, 0);
730 if (Lut == NULL)
731 goto Error;
732
733 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))
734 goto Error;
735
736 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
737 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
738 goto Error;
739
740 return Lut;
741 Error:
742 cmsPipelineFree(Lut);
743 cmsFreeNamedColorList(nc);
744 return NULL;
745 }
746
747
748 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
749
750 // Floating point LUT are always V
751 return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
752 }
753
754 tagFloat = Device2PCSFloat[0];
755 if (cmsIsTag(hProfile, tagFloat)) {
756
757 return cmsPipelineDup((cmsPipeline*)cmsReadTag(hProfile, tagFloat));
758 }
759
760 if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
761
762 tag16 = Device2PCS16[0];
763 if (!cmsIsTag(hProfile, tag16)) return NULL;
764 }
765
766 // Check profile version and LUT type. Do the necessary adjustments if needed
767
768 // Read the tag
769 Lut = (cmsPipeline*)cmsReadTag(hProfile, tag16);
770 if (Lut == NULL) return NULL;
771
772 // The profile owns the Lut, so we need to copy it
773 Lut = cmsPipelineDup(Lut);
774 if (Lut == NULL) return NULL;
775
776 // Now it is time for a controversial stuff. I found that for 3D LUTS using
777 // Lab used as indexer space, trilinear interpolation should be used
778 if (cmsGetPCS(hProfile) == cmsSigLabData)
779 ChangeInterpolationToTrilinear(Lut);
780
781 // After reading it, we have info about the original type
782 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
783
784 // We need to adjust data for Lab16 on output
785 if (OriginalType != cmsSigLut16Type) return Lut;
786
787 // Here it is possible to get Lab on both sides
788
789 if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
790 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
791 goto Error2;
792 }
793
794 if (cmsGetPCS(hProfile) == cmsSigLabData) {
795 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
796 goto Error2;
797 }
798
799 return Lut;
800
801 Error2:
802 cmsPipelineFree(Lut);
803 return NULL;
804 }
805
806 // ---------------------------------------------------------------------------------------------------------------
807
808 // Returns TRUE if the profile is implemented as matrix-shaper
cmsIsMatrixShaper(cmsHPROFILE hProfile)809 cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
810 {
811 switch (cmsGetColorSpace(hProfile)) {
812
813 case cmsSigGrayData:
814
815 return cmsIsTag(hProfile, cmsSigGrayTRCTag);
816
817 case cmsSigRgbData:
818
819 return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
820 cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
821 cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
822 cmsIsTag(hProfile, cmsSigRedTRCTag) &&
823 cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
824 cmsIsTag(hProfile, cmsSigBlueTRCTag));
825
826 default:
827
828 return FALSE;
829 }
830 }
831
832 // Returns TRUE if the intent is implemented as CLUT
cmsIsCLUT(cmsHPROFILE hProfile,cmsUInt32Number Intent,cmsUInt32Number UsedDirection)833 cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
834 {
835 const cmsTagSignature* TagTable;
836
837 // For devicelinks, the supported intent is that one stated in the header
838 if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
839 return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
840 }
841
842 switch (UsedDirection) {
843
844 case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
845 case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
846
847 // For proofing, we need rel. colorimetric in output. Let's do some recursion
848 case LCMS_USED_AS_PROOF:
849 return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
850 cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
851
852 default:
853 cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
854 return FALSE;
855 }
856
857 return cmsIsTag(hProfile, TagTable[Intent]);
858
859 }
860
861
862 // Return info about supported intents
cmsIsIntentSupported(cmsHPROFILE hProfile,cmsUInt32Number Intent,cmsUInt32Number UsedDirection)863 cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
864 cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
865 {
866
867 if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
868
869 // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
870 // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
871 // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
872 // the accuracy would be less than optimal in rel.col and v2 case.
873
874 return cmsIsMatrixShaper(hProfile);
875 }
876
877
878 // ---------------------------------------------------------------------------------------------------------------
879
880 // Read both, profile sequence description and profile sequence id if present. Then combine both to
881 // create qa unique structure holding both. Shame on ICC to store things in such complicated way.
_cmsReadProfileSequence(cmsHPROFILE hProfile)882 cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
883 {
884 cmsSEQ* ProfileSeq;
885 cmsSEQ* ProfileId;
886 cmsSEQ* NewSeq;
887 cmsUInt32Number i;
888
889 // Take profile sequence description first
890 ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
891
892 // Take profile sequence ID
893 ProfileId = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
894
895 if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
896
897 if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
898 if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
899
900 // We have to mix both together. For that they must agree
901 if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
902
903 NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
904
905 // Ok, proceed to the mixing
906 if (NewSeq != NULL) {
907 for (i=0; i < ProfileSeq ->n; i++) {
908
909 memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
910 NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
911 }
912 }
913 return NewSeq;
914 }
915
916 // Dump the contents of profile sequence in both tags (if v4 available)
_cmsWriteProfileSequence(cmsHPROFILE hProfile,const cmsSEQ * seq)917 cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
918 {
919 if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
920
921 if (cmsGetEncodedICCversion(hProfile) >= 0x4000000) {
922
923 if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
924 }
925
926 return TRUE;
927 }
928
929
930 // Auxiliary, read and duplicate a MLU if found.
931 static
GetMLUFromProfile(cmsHPROFILE h,cmsTagSignature sig)932 cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
933 {
934 cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
935 if (mlu == NULL) return NULL;
936
937 return cmsMLUdup(mlu);
938 }
939
940 // Create a sequence description out of an array of profiles
_cmsCompileProfileSequence(cmsContext ContextID,cmsUInt32Number nProfiles,cmsHPROFILE hProfiles[])941 cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
942 {
943 cmsUInt32Number i;
944 cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
945
946 if (seq == NULL) return NULL;
947
948 for (i=0; i < nProfiles; i++) {
949
950 cmsPSEQDESC* ps = &seq ->seq[i];
951 cmsHPROFILE h = hProfiles[i];
952 cmsTechnologySignature* techpt;
953
954 cmsGetHeaderAttributes(h, &ps ->attributes);
955 cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
956 ps ->deviceMfg = cmsGetHeaderManufacturer(h);
957 ps ->deviceModel = cmsGetHeaderModel(h);
958
959 techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
960 if (techpt == NULL)
961 ps ->technology = (cmsTechnologySignature) 0;
962 else
963 ps ->technology = *techpt;
964
965 ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);
966 ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);
967 ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
968
969 }
970
971 return seq;
972 }
973
974 // -------------------------------------------------------------------------------------------------------------------
975
976
977 static
GetInfo(cmsHPROFILE hProfile,cmsInfoType Info)978 const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
979 {
980 cmsTagSignature sig;
981
982 switch (Info) {
983
984 case cmsInfoDescription:
985 sig = cmsSigProfileDescriptionTag;
986 break;
987
988 case cmsInfoManufacturer:
989 sig = cmsSigDeviceMfgDescTag;
990 break;
991
992 case cmsInfoModel:
993 sig = cmsSigDeviceModelDescTag;
994 break;
995
996 case cmsInfoCopyright:
997 sig = cmsSigCopyrightTag;
998 break;
999
1000 default: return NULL;
1001 }
1002
1003
1004 return (cmsMLU*) cmsReadTag(hProfile, sig);
1005 }
1006
1007
1008
cmsGetProfileInfo(cmsHPROFILE hProfile,cmsInfoType Info,const char LanguageCode[3],const char CountryCode[3],wchar_t * Buffer,cmsUInt32Number BufferSize)1009 cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
1010 const char LanguageCode[3], const char CountryCode[3],
1011 wchar_t* Buffer, cmsUInt32Number BufferSize)
1012 {
1013 const cmsMLU* mlu = GetInfo(hProfile, Info);
1014 if (mlu == NULL) return 0;
1015
1016 return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
1017 }
1018
1019
cmsGetProfileInfoASCII(cmsHPROFILE hProfile,cmsInfoType Info,const char LanguageCode[3],const char CountryCode[3],char * Buffer,cmsUInt32Number BufferSize)1020 cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
1021 const char LanguageCode[3], const char CountryCode[3],
1022 char* Buffer, cmsUInt32Number BufferSize)
1023 {
1024 const cmsMLU* mlu = GetInfo(hProfile, Info);
1025 if (mlu == NULL) return 0;
1026
1027 return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
1028 }
1029