1 //---------------------------------------------------------------------------------
2 //
3 // Little Color Management System
4 // Copyright (c) 1998-2016 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 cmsFreeToneCurve(GrayTRC);
205 cmsPipelineFree(Lut);
206 return NULL;
207 }
208
209 // RGB Matrix shaper
210 static
BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)211 cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
212 {
213 cmsPipeline* Lut;
214 cmsMAT3 Mat;
215 cmsToneCurve *Shapes[3];
216 cmsContext ContextID = cmsGetProfileContextID(hProfile);
217 int i, j;
218
219 if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
220
221 // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
222 // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
223 // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
224
225 for (i=0; i < 3; i++)
226 for (j=0; j < 3; j++)
227 Mat.v[i].n[j] *= InpAdj;
228
229
230 Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
231 Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
232 Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
233
234 if (!Shapes[0] || !Shapes[1] || !Shapes[2])
235 return NULL;
236
237 Lut = cmsPipelineAlloc(ContextID, 3, 3);
238 if (Lut != NULL) {
239
240 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes)) ||
241 !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL)))
242 goto Error;
243
244 // Note that it is certainly possible a single profile would have a LUT based
245 // tag for output working in lab and a matrix-shaper for the fallback cases.
246 // This is not allowed by the spec, but this code is tolerant to those cases
247 if (cmsGetPCS(hProfile) == cmsSigLabData) {
248
249 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID)))
250 goto Error;
251 }
252
253 }
254
255 return Lut;
256
257 Error:
258 cmsPipelineFree(Lut);
259 return NULL;
260 }
261
262
263
264 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
265 static
_cmsReadFloatInputTag(cmsHPROFILE hProfile,cmsTagSignature tagFloat)266 cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
267 {
268 cmsContext ContextID = cmsGetProfileContextID(hProfile);
269 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
270 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
271 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
272
273 if (Lut == NULL) return NULL;
274
275 // input and output of transform are in lcms 0..1 encoding. If XYZ or Lab spaces are used,
276 // these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)
277 if ( spc == cmsSigLabData)
278 {
279 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
280 goto Error;
281 }
282 else if (spc == cmsSigXYZData)
283 {
284 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
285 goto Error;
286 }
287
288 if ( PCS == cmsSigLabData)
289 {
290 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
291 goto Error;
292 }
293 else if( PCS == cmsSigXYZData)
294 {
295 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
296 goto Error;
297 }
298
299 return Lut;
300
301 Error:
302 cmsPipelineFree(Lut);
303 return NULL;
304 }
305
306
307 // Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
308 // is adjusted here in order to create a LUT that takes care of all those details.
309 // We add intent = -1 as a way to read matrix shaper always, no matter of other LUT
_cmsReadInputLUT(cmsHPROFILE hProfile,int Intent)310 cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent)
311 {
312 cmsTagTypeSignature OriginalType;
313 cmsTagSignature tag16;
314 cmsTagSignature tagFloat;
315 cmsContext ContextID = cmsGetProfileContextID(hProfile);
316
317 // On named color, take the appropriate tag
318 if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
319
320 cmsPipeline* Lut;
321 cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
322
323 if (nc == NULL) return NULL;
324
325 Lut = cmsPipelineAlloc(ContextID, 0, 0);
326 if (Lut == NULL) {
327 cmsFreeNamedColorList(nc);
328 return NULL;
329 }
330
331 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE)) ||
332 !cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID))) {
333 cmsPipelineFree(Lut);
334 return NULL;
335 }
336 return Lut;
337 }
338
339 // This is an attempt to reuse this function to retrieve the matrix-shaper as pipeline no
340 // matter other LUT are present and have precedence. Intent = -1 means just this.
341 if (Intent >= INTENT_PERCEPTUAL && Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {
342
343 tag16 = Device2PCS16[Intent];
344 tagFloat = Device2PCSFloat[Intent];
345
346 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
347
348 // Floating point LUT are always V4, but the encoding range is no
349 // longer 0..1.0, so we need to add an stage depending on the color space
350 return _cmsReadFloatInputTag(hProfile, tagFloat);
351 }
352
353 // Revert to perceptual if no tag is found
354 if (!cmsIsTag(hProfile, tag16)) {
355 tag16 = Device2PCS16[0];
356 }
357
358 if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
359
360 // Check profile version and LUT type. Do the necessary adjustments if needed
361
362 // First read the tag
363 cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
364 if (Lut == NULL) return NULL;
365
366 // After reading it, we have now info about the original type
367 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
368
369 // The profile owns the Lut, so we need to copy it
370 Lut = cmsPipelineDup(Lut);
371
372 // We need to adjust data only for Lab16 on output
373 if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
374 return Lut;
375
376 // If the input is Lab, add also a conversion at the begin
377 if (cmsGetColorSpace(hProfile) == cmsSigLabData &&
378 !cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
379 goto Error;
380
381 // Add a matrix for conversion V2 to V4 Lab PCS
382 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
383 goto Error;
384
385 return Lut;
386 Error:
387 cmsPipelineFree(Lut);
388 return NULL;
389 }
390 }
391
392 // Lut was not found, try to create a matrix-shaper
393
394 // Check if this is a grayscale profile.
395 if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
396
397 // if so, build appropriate conversion tables.
398 // The tables are the PCS iluminant, scaled across GrayTRC
399 return BuildGrayInputMatrixPipeline(hProfile);
400 }
401
402 // Not gray, create a normal matrix-shaper
403 return BuildRGBInputMatrixShaper(hProfile);
404 }
405
406 // ---------------------------------------------------------------------------------------------------------------
407
408 // Gray output pipeline.
409 // XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
410 // given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
411 // The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
412
413 static
BuildGrayOutputPipeline(cmsHPROFILE hProfile)414 cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
415 {
416 cmsToneCurve *GrayTRC, *RevGrayTRC;
417 cmsPipeline* Lut;
418 cmsContext ContextID = cmsGetProfileContextID(hProfile);
419
420 GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
421 if (GrayTRC == NULL) return NULL;
422
423 RevGrayTRC = cmsReverseToneCurve(GrayTRC);
424 if (RevGrayTRC == NULL) return NULL;
425
426 Lut = cmsPipelineAlloc(ContextID, 3, 1);
427 if (Lut == NULL) {
428 cmsFreeToneCurve(RevGrayTRC);
429 return NULL;
430 }
431
432 if (cmsGetPCS(hProfile) == cmsSigLabData) {
433
434 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL)))
435 goto Error;
436 }
437 else {
438 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickYMatrix, NULL)))
439 goto Error;
440 }
441
442 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))
443 goto Error;
444
445 cmsFreeToneCurve(RevGrayTRC);
446 return Lut;
447
448 Error:
449 cmsFreeToneCurve(RevGrayTRC);
450 cmsPipelineFree(Lut);
451 return NULL;
452 }
453
454
455 static
BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)456 cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
457 {
458 cmsPipeline* Lut;
459 cmsToneCurve *Shapes[3], *InvShapes[3];
460 cmsMAT3 Mat, Inv;
461 int i, j;
462 cmsContext ContextID = cmsGetProfileContextID(hProfile);
463
464 if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
465 return NULL;
466
467 if (!_cmsMAT3inverse(&Mat, &Inv))
468 return NULL;
469
470 // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
471 // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
472 // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
473
474 for (i=0; i < 3; i++)
475 for (j=0; j < 3; j++)
476 Inv.v[i].n[j] *= OutpAdj;
477
478 Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
479 Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
480 Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
481
482 if (!Shapes[0] || !Shapes[1] || !Shapes[2])
483 return NULL;
484
485 InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
486 InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
487 InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
488
489 if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
490 return NULL;
491 }
492
493 Lut = cmsPipelineAlloc(ContextID, 3, 3);
494 if (Lut != NULL) {
495
496 // Note that it is certainly possible a single profile would have a LUT based
497 // tag for output working in lab and a matrix-shaper for the fallback cases.
498 // This is not allowed by the spec, but this code is tolerant to those cases
499 if (cmsGetPCS(hProfile) == cmsSigLabData) {
500
501 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))
502 goto Error;
503 }
504
505 if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||
506 !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))
507 goto Error;
508 }
509
510 cmsFreeToneCurveTriple(InvShapes);
511 return Lut;
512 Error:
513 cmsFreeToneCurveTriple(InvShapes);
514 cmsPipelineFree(Lut);
515 return NULL;
516 }
517
518
519 // Change CLUT interpolation to trilinear
520 static
ChangeInterpolationToTrilinear(cmsPipeline * Lut)521 void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
522 {
523 cmsStage* Stage;
524
525 for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
526 Stage != NULL;
527 Stage = cmsStageNext(Stage)) {
528
529 if (cmsStageType(Stage) == cmsSigCLutElemType) {
530
531 _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
532
533 CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
534 _cmsSetInterpolationRoutine(Lut->ContextID, CLUT ->Params);
535 }
536 }
537 }
538
539
540 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
541 static
_cmsReadFloatOutputTag(cmsHPROFILE hProfile,cmsTagSignature tagFloat)542 cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
543 {
544 cmsContext ContextID = cmsGetProfileContextID(hProfile);
545 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
546 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
547 cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
548
549 if (Lut == NULL) return NULL;
550
551 // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
552 // and since the formatter has already accommodated to 0..1.0, we should undo this change
553 if ( PCS == cmsSigLabData)
554 {
555 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
556 goto Error;
557 }
558 else
559 if (PCS == cmsSigXYZData)
560 {
561 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
562 goto Error;
563 }
564
565 // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
566 if ( dataSpace == cmsSigLabData)
567 {
568 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
569 goto Error;
570 }
571 else if (dataSpace == cmsSigXYZData)
572 {
573 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
574 goto Error;
575 }
576
577 return Lut;
578
579 Error:
580 cmsPipelineFree(Lut);
581 return NULL;
582 }
583
584 // Create an output MPE LUT from agiven profile. Version mismatches are handled here
_cmsReadOutputLUT(cmsHPROFILE hProfile,int Intent)585 cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
586 {
587 cmsTagTypeSignature OriginalType;
588 cmsTagSignature tag16;
589 cmsTagSignature tagFloat;
590 cmsContext ContextID = cmsGetProfileContextID(hProfile);
591
592
593 if (Intent >= INTENT_PERCEPTUAL && Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {
594
595 tag16 = PCS2Device16[Intent];
596 tagFloat = PCS2DeviceFloat[Intent];
597
598 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
599
600 // Floating point LUT are always V4
601 return _cmsReadFloatOutputTag(hProfile, tagFloat);
602 }
603
604 // Revert to perceptual if no tag is found
605 if (!cmsIsTag(hProfile, tag16)) {
606 tag16 = PCS2Device16[0];
607 }
608
609 if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
610
611 // Check profile version and LUT type. Do the necessary adjustments if needed
612
613 // First read the tag
614 cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
615 if (Lut == NULL) return NULL;
616
617 // After reading it, we have info about the original type
618 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
619
620 // The profile owns the Lut, so we need to copy it
621 Lut = cmsPipelineDup(Lut);
622 if (Lut == NULL) return NULL;
623
624 // Now it is time for a controversial stuff. I found that for 3D LUTS using
625 // Lab used as indexer space, trilinear interpolation should be used
626 if (cmsGetPCS(hProfile) == cmsSigLabData)
627 ChangeInterpolationToTrilinear(Lut);
628
629 // We need to adjust data only for Lab and Lut16 type
630 if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
631 return Lut;
632
633 // Add a matrix for conversion V4 to V2 Lab PCS
634 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
635 goto Error;
636
637 // If the output is Lab, add also a conversion at the end
638 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
639 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
640 goto Error;
641
642 return Lut;
643 Error:
644 cmsPipelineFree(Lut);
645 return NULL;
646 }
647 }
648
649 // Lut not found, try to create a matrix-shaper
650
651 // Check if this is a grayscale profile.
652 if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
653
654 // if so, build appropriate conversion tables.
655 // The tables are the PCS iluminant, scaled across GrayTRC
656 return BuildGrayOutputPipeline(hProfile);
657 }
658
659 // Not gray, create a normal matrix-shaper, which only operates in XYZ space
660 return BuildRGBOutputMatrixShaper(hProfile);
661 }
662
663 // ---------------------------------------------------------------------------------------------------------------
664
665 // Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
666 static
_cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile,cmsTagSignature tagFloat)667 cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
668 {
669 cmsContext ContextID = cmsGetProfileContextID(hProfile);
670 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
671 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
672 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
673
674 if (Lut == NULL) return NULL;
675
676 if (spc == cmsSigLabData)
677 {
678 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
679 goto Error;
680 }
681 else
682 if (spc == cmsSigXYZData)
683 {
684 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
685 goto Error;
686 }
687
688 if (PCS == cmsSigLabData)
689 {
690 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
691 goto Error;
692 }
693 else
694 if (PCS == cmsSigXYZData)
695 {
696 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
697 goto Error;
698 }
699
700 return Lut;
701 Error:
702 cmsPipelineFree(Lut);
703 return NULL;
704 }
705
706 // This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
707 // tag name here may default to AToB0
_cmsReadDevicelinkLUT(cmsHPROFILE hProfile,int Intent)708 cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
709 {
710 cmsPipeline* Lut;
711 cmsTagTypeSignature OriginalType;
712 cmsTagSignature tag16;
713 cmsTagSignature tagFloat;
714 cmsContext ContextID = cmsGetProfileContextID(hProfile);
715
716
717 if (Intent < INTENT_PERCEPTUAL || Intent > INTENT_ABSOLUTE_COLORIMETRIC)
718 return NULL;
719
720 tag16 = Device2PCS16[Intent];
721 tagFloat = Device2PCSFloat[Intent];
722
723 // On named color, take the appropriate tag
724 if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
725
726 cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*)cmsReadTag(hProfile, cmsSigNamedColor2Tag);
727
728 if (nc == NULL) return NULL;
729
730 Lut = cmsPipelineAlloc(ContextID, 0, 0);
731 if (Lut == NULL)
732 goto Error;
733
734 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))
735 goto Error;
736
737 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
738 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
739 goto Error;
740
741 return Lut;
742 Error:
743 cmsPipelineFree(Lut);
744 cmsFreeNamedColorList(nc);
745 return NULL;
746 }
747
748
749 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
750
751 // Floating point LUT are always V
752 return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
753 }
754
755 tagFloat = Device2PCSFloat[0];
756 if (cmsIsTag(hProfile, tagFloat)) {
757
758 return cmsPipelineDup((cmsPipeline*)cmsReadTag(hProfile, tagFloat));
759 }
760
761 if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
762
763 tag16 = Device2PCS16[0];
764 if (!cmsIsTag(hProfile, tag16)) return NULL;
765 }
766
767 // Check profile version and LUT type. Do the necessary adjustments if needed
768
769 // Read the tag
770 Lut = (cmsPipeline*)cmsReadTag(hProfile, tag16);
771 if (Lut == NULL) return NULL;
772
773 // The profile owns the Lut, so we need to copy it
774 Lut = cmsPipelineDup(Lut);
775 if (Lut == NULL) return NULL;
776
777 // Now it is time for a controversial stuff. I found that for 3D LUTS using
778 // Lab used as indexer space, trilinear interpolation should be used
779 if (cmsGetPCS(hProfile) == cmsSigLabData)
780 ChangeInterpolationToTrilinear(Lut);
781
782 // After reading it, we have info about the original type
783 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
784
785 // We need to adjust data for Lab16 on output
786 if (OriginalType != cmsSigLut16Type) return Lut;
787
788 // Here it is possible to get Lab on both sides
789
790 if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
791 if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
792 goto Error2;
793 }
794
795 if (cmsGetPCS(hProfile) == cmsSigLabData) {
796 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
797 goto Error2;
798 }
799
800 return Lut;
801
802 Error2:
803 cmsPipelineFree(Lut);
804 return NULL;
805 }
806
807 // ---------------------------------------------------------------------------------------------------------------
808
809 // Returns TRUE if the profile is implemented as matrix-shaper
cmsIsMatrixShaper(cmsHPROFILE hProfile)810 cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
811 {
812 switch (cmsGetColorSpace(hProfile)) {
813
814 case cmsSigGrayData:
815
816 return cmsIsTag(hProfile, cmsSigGrayTRCTag);
817
818 case cmsSigRgbData:
819
820 return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
821 cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
822 cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
823 cmsIsTag(hProfile, cmsSigRedTRCTag) &&
824 cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
825 cmsIsTag(hProfile, cmsSigBlueTRCTag));
826
827 default:
828
829 return FALSE;
830 }
831 }
832
833 // Returns TRUE if the intent is implemented as CLUT
cmsIsCLUT(cmsHPROFILE hProfile,cmsUInt32Number Intent,cmsUInt32Number UsedDirection)834 cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
835 {
836 const cmsTagSignature* TagTable;
837
838 // For devicelinks, the supported intent is that one stated in the header
839 if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
840 return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
841 }
842
843 switch (UsedDirection) {
844
845 case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
846 case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
847
848 // For proofing, we need rel. colorimetric in output. Let's do some recursion
849 case LCMS_USED_AS_PROOF:
850 return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
851 cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
852
853 default:
854 cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
855 return FALSE;
856 }
857
858 return cmsIsTag(hProfile, TagTable[Intent]);
859
860 }
861
862
863 // Return info about supported intents
cmsIsIntentSupported(cmsHPROFILE hProfile,cmsUInt32Number Intent,cmsUInt32Number UsedDirection)864 cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
865 cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
866 {
867
868 if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
869
870 // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
871 // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
872 // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
873 // the accuracy would be less than optimal in rel.col and v2 case.
874
875 return cmsIsMatrixShaper(hProfile);
876 }
877
878
879 // ---------------------------------------------------------------------------------------------------------------
880
881 // Read both, profile sequence description and profile sequence id if present. Then combine both to
882 // create qa unique structure holding both. Shame on ICC to store things in such complicated way.
_cmsReadProfileSequence(cmsHPROFILE hProfile)883 cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
884 {
885 cmsSEQ* ProfileSeq;
886 cmsSEQ* ProfileId;
887 cmsSEQ* NewSeq;
888 cmsUInt32Number i;
889
890 // Take profile sequence description first
891 ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
892
893 // Take profile sequence ID
894 ProfileId = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
895
896 if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
897
898 if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
899 if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
900
901 // We have to mix both together. For that they must agree
902 if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
903
904 NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
905
906 // Ok, proceed to the mixing
907 if (NewSeq != NULL) {
908 for (i=0; i < ProfileSeq ->n; i++) {
909
910 memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
911 NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
912 }
913 }
914 return NewSeq;
915 }
916
917 // Dump the contents of profile sequence in both tags (if v4 available)
_cmsWriteProfileSequence(cmsHPROFILE hProfile,const cmsSEQ * seq)918 cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
919 {
920 if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
921
922 if (cmsGetEncodedICCversion(hProfile) >= 0x4000000) {
923
924 if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
925 }
926
927 return TRUE;
928 }
929
930
931 // Auxiliary, read and duplicate a MLU if found.
932 static
GetMLUFromProfile(cmsHPROFILE h,cmsTagSignature sig)933 cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
934 {
935 cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
936 if (mlu == NULL) return NULL;
937
938 return cmsMLUdup(mlu);
939 }
940
941 // Create a sequence description out of an array of profiles
_cmsCompileProfileSequence(cmsContext ContextID,cmsUInt32Number nProfiles,cmsHPROFILE hProfiles[])942 cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
943 {
944 cmsUInt32Number i;
945 cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
946
947 if (seq == NULL) return NULL;
948
949 for (i=0; i < nProfiles; i++) {
950
951 cmsPSEQDESC* ps = &seq ->seq[i];
952 cmsHPROFILE h = hProfiles[i];
953 cmsTechnologySignature* techpt;
954
955 cmsGetHeaderAttributes(h, &ps ->attributes);
956 cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
957 ps ->deviceMfg = cmsGetHeaderManufacturer(h);
958 ps ->deviceModel = cmsGetHeaderModel(h);
959
960 techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
961 if (techpt == NULL)
962 ps ->technology = (cmsTechnologySignature) 0;
963 else
964 ps ->technology = *techpt;
965
966 ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);
967 ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);
968 ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
969
970 }
971
972 return seq;
973 }
974
975 // -------------------------------------------------------------------------------------------------------------------
976
977
978 static
GetInfo(cmsHPROFILE hProfile,cmsInfoType Info)979 const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
980 {
981 cmsTagSignature sig;
982
983 switch (Info) {
984
985 case cmsInfoDescription:
986 sig = cmsSigProfileDescriptionTag;
987 break;
988
989 case cmsInfoManufacturer:
990 sig = cmsSigDeviceMfgDescTag;
991 break;
992
993 case cmsInfoModel:
994 sig = cmsSigDeviceModelDescTag;
995 break;
996
997 case cmsInfoCopyright:
998 sig = cmsSigCopyrightTag;
999 break;
1000
1001 default: return NULL;
1002 }
1003
1004
1005 return (cmsMLU*) cmsReadTag(hProfile, sig);
1006 }
1007
1008
1009
cmsGetProfileInfo(cmsHPROFILE hProfile,cmsInfoType Info,const char LanguageCode[3],const char CountryCode[3],wchar_t * Buffer,cmsUInt32Number BufferSize)1010 cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
1011 const char LanguageCode[3], const char CountryCode[3],
1012 wchar_t* Buffer, cmsUInt32Number BufferSize)
1013 {
1014 const cmsMLU* mlu = GetInfo(hProfile, Info);
1015 if (mlu == NULL) return 0;
1016
1017 return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
1018 }
1019
1020
cmsGetProfileInfoASCII(cmsHPROFILE hProfile,cmsInfoType Info,const char LanguageCode[3],const char CountryCode[3],char * Buffer,cmsUInt32Number BufferSize)1021 cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
1022 const char LanguageCode[3], const char CountryCode[3],
1023 char* Buffer, cmsUInt32Number BufferSize)
1024 {
1025 const cmsMLU* mlu = GetInfo(hProfile, Info);
1026 if (mlu == NULL) return 0;
1027
1028 return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
1029 }
1030