comparison mupdf-source/thirdparty/lcms2/src/cmsio1.c @ 2:b50eed0cc0ef upstream

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