cmsio1.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. //---------------------------------------------------------------------------------
  2. //
  3. // Little Color Management System
  4. // Copyright (c) 1998-2012 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. #include "lcms2_internal.h"
  27. // Read tags using low-level functions, provides necessary glue code to adapt versions, etc.
  28. // LUT tags
  29. static const cmsTagSignature Device2PCS16[] = {cmsSigAToB0Tag, // Perceptual
  30. cmsSigAToB1Tag, // Relative colorimetric
  31. cmsSigAToB2Tag, // Saturation
  32. cmsSigAToB1Tag }; // Absolute colorimetric
  33. static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag, // Perceptual
  34. cmsSigDToB1Tag, // Relative colorimetric
  35. cmsSigDToB2Tag, // Saturation
  36. cmsSigDToB3Tag }; // Absolute colorimetric
  37. static const cmsTagSignature PCS2Device16[] = {cmsSigBToA0Tag, // Perceptual
  38. cmsSigBToA1Tag, // Relative colorimetric
  39. cmsSigBToA2Tag, // Saturation
  40. cmsSigBToA1Tag }; // Absolute colorimetric
  41. static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag, // Perceptual
  42. cmsSigBToD1Tag, // Relative colorimetric
  43. cmsSigBToD2Tag, // Saturation
  44. cmsSigBToD3Tag }; // Absolute colorimetric
  45. // Factors to convert from 1.15 fixed point to 0..1.0 range and vice-versa
  46. #define InpAdj (1.0/MAX_ENCODEABLE_XYZ) // (65536.0/(65535.0*2.0))
  47. #define OutpAdj (MAX_ENCODEABLE_XYZ) // ((2.0*65535.0)/65536.0)
  48. // Several resources for gray conversions.
  49. static const cmsFloat64Number GrayInputMatrix[] = { (InpAdj*cmsD50X), (InpAdj*cmsD50Y), (InpAdj*cmsD50Z) };
  50. static const cmsFloat64Number OneToThreeInputMatrix[] = { 1, 1, 1 };
  51. static const cmsFloat64Number PickYMatrix[] = { 0, (OutpAdj*cmsD50Y), 0 };
  52. static const cmsFloat64Number PickLstarMatrix[] = { 1, 0, 0 };
  53. // Get a media white point fixing some issues found in certain old profiles
  54. cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)
  55. {
  56. cmsCIEXYZ* Tag;
  57. _cmsAssert(Dest != NULL);
  58. Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
  59. // If no wp, take D50
  60. if (Tag == NULL) {
  61. *Dest = *cmsD50_XYZ();
  62. return TRUE;
  63. }
  64. // V2 display profiles should give D50
  65. if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
  66. if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
  67. *Dest = *cmsD50_XYZ();
  68. return TRUE;
  69. }
  70. }
  71. // All seems ok
  72. *Dest = *Tag;
  73. return TRUE;
  74. }
  75. // Chromatic adaptation matrix. Fix some issues as well
  76. cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)
  77. {
  78. cmsMAT3* Tag;
  79. _cmsAssert(Dest != NULL);
  80. Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);
  81. if (Tag != NULL) {
  82. *Dest = *Tag;
  83. return TRUE;
  84. }
  85. // No CHAD available, default it to identity
  86. _cmsMAT3identity(Dest);
  87. // V2 display profiles should give D50
  88. if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
  89. if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
  90. cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
  91. if (White == NULL) {
  92. _cmsMAT3identity(Dest);
  93. return TRUE;
  94. }
  95. return _cmsAdaptationMatrix(Dest, NULL, White, cmsD50_XYZ());
  96. }
  97. }
  98. return TRUE;
  99. }
  100. // Auxiliar, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper
  101. static
  102. cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)
  103. {
  104. cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;
  105. _cmsAssert(r != NULL);
  106. PtrRed = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigRedColorantTag);
  107. PtrGreen = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigGreenColorantTag);
  108. PtrBlue = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigBlueColorantTag);
  109. if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)
  110. return FALSE;
  111. _cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X, PtrBlue -> X);
  112. _cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y, PtrBlue -> Y);
  113. _cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z, PtrBlue -> Z);
  114. return TRUE;
  115. }
  116. // Gray input pipeline
  117. static
  118. cmsPipeline* BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)
  119. {
  120. cmsToneCurve *GrayTRC;
  121. cmsPipeline* Lut;
  122. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  123. GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
  124. if (GrayTRC == NULL) return NULL;
  125. Lut = cmsPipelineAlloc(ContextID, 1, 3);
  126. if (Lut == NULL)
  127. goto Error;
  128. if (cmsGetPCS(hProfile) == cmsSigLabData) {
  129. // In this case we implement the profile as an identity matrix plus 3 tone curves
  130. cmsUInt16Number Zero[2] = { 0x8080, 0x8080 };
  131. cmsToneCurve* EmptyTab;
  132. cmsToneCurve* LabCurves[3];
  133. EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
  134. if (EmptyTab == NULL)
  135. goto Error;
  136. LabCurves[0] = GrayTRC;
  137. LabCurves[1] = EmptyTab;
  138. LabCurves[2] = EmptyTab;
  139. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, OneToThreeInputMatrix, NULL)) ||
  140. !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, LabCurves))) {
  141. cmsFreeToneCurve(EmptyTab);
  142. goto Error;
  143. }
  144. cmsFreeToneCurve(EmptyTab);
  145. }
  146. else {
  147. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &GrayTRC)) ||
  148. !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, GrayInputMatrix, NULL)))
  149. goto Error;
  150. }
  151. return Lut;
  152. Error:
  153. cmsFreeToneCurve(GrayTRC);
  154. cmsPipelineFree(Lut);
  155. return NULL;
  156. }
  157. // RGB Matrix shaper
  158. static
  159. cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
  160. {
  161. cmsPipeline* Lut;
  162. cmsMAT3 Mat;
  163. cmsToneCurve *Shapes[3];
  164. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  165. int i, j;
  166. if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
  167. // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
  168. // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
  169. // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
  170. for (i=0; i < 3; i++)
  171. for (j=0; j < 3; j++)
  172. Mat.v[i].n[j] *= InpAdj;
  173. Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
  174. Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
  175. Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
  176. if (!Shapes[0] || !Shapes[1] || !Shapes[2])
  177. return NULL;
  178. Lut = cmsPipelineAlloc(ContextID, 3, 3);
  179. if (Lut != NULL) {
  180. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes)) ||
  181. !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL)))
  182. goto Error;
  183. // Note that it is certainly possible a single profile would have a LUT based
  184. // tag for output working in lab and a matrix-shaper for the fallback cases.
  185. // This is not allowed by the spec, but this code is tolerant to those cases
  186. if (cmsGetPCS(hProfile) == cmsSigLabData) {
  187. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID)))
  188. goto Error;
  189. }
  190. }
  191. return Lut;
  192. Error:
  193. cmsPipelineFree(Lut);
  194. return NULL;
  195. }
  196. // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
  197. static
  198. cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
  199. {
  200. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  201. cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
  202. cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
  203. cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
  204. if (Lut == NULL) return NULL;
  205. // input and output of transform are in lcms 0..1 encoding. If XYZ or Lab spaces are used,
  206. // these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)
  207. if ( spc == cmsSigLabData)
  208. {
  209. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
  210. goto Error;
  211. }
  212. else if (spc == cmsSigXYZData)
  213. {
  214. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
  215. goto Error;
  216. }
  217. if ( PCS == cmsSigLabData)
  218. {
  219. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
  220. goto Error;
  221. }
  222. else if( PCS == cmsSigXYZData)
  223. {
  224. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
  225. goto Error;
  226. }
  227. return Lut;
  228. Error:
  229. cmsPipelineFree(Lut);
  230. return NULL;
  231. }
  232. // Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
  233. // is adjusted here in order to create a LUT that takes care of all those details
  234. cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent)
  235. {
  236. cmsTagTypeSignature OriginalType;
  237. cmsTagSignature tag16 = Device2PCS16[Intent];
  238. cmsTagSignature tagFloat = Device2PCSFloat[Intent];
  239. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  240. // On named color, take the appropiate tag
  241. if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
  242. cmsPipeline* Lut;
  243. cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
  244. if (nc == NULL) return NULL;
  245. Lut = cmsPipelineAlloc(ContextID, 0, 0);
  246. if (Lut == NULL) {
  247. cmsFreeNamedColorList(nc);
  248. return NULL;
  249. }
  250. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE)) ||
  251. !cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID))) {
  252. cmsPipelineFree(Lut);
  253. return NULL;
  254. }
  255. return Lut;
  256. }
  257. if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
  258. // Floating point LUT are always V4, but the encoding range is no
  259. // longer 0..1.0, so we need to add an stage depending on the color space
  260. return _cmsReadFloatInputTag(hProfile, tagFloat);
  261. }
  262. // Revert to perceptual if no tag is found
  263. if (!cmsIsTag(hProfile, tag16)) {
  264. tag16 = Device2PCS16[0];
  265. }
  266. if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
  267. // Check profile version and LUT type. Do the necessary adjustments if needed
  268. // First read the tag
  269. cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
  270. if (Lut == NULL) return NULL;
  271. // After reading it, we have now info about the original type
  272. OriginalType = _cmsGetTagTrueType(hProfile, tag16);
  273. // The profile owns the Lut, so we need to copy it
  274. Lut = cmsPipelineDup(Lut);
  275. // We need to adjust data only for Lab16 on output
  276. if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
  277. return Lut;
  278. // If the input is Lab, add also a conversion at the begin
  279. if (cmsGetColorSpace(hProfile) == cmsSigLabData &&
  280. !cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
  281. goto Error;
  282. // Add a matrix for conversion V2 to V4 Lab PCS
  283. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
  284. goto Error;
  285. return Lut;
  286. Error:
  287. cmsPipelineFree(Lut);
  288. return NULL;
  289. }
  290. // Lut was not found, try to create a matrix-shaper
  291. // Check if this is a grayscale profile.
  292. if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
  293. // if so, build appropiate conversion tables.
  294. // The tables are the PCS iluminant, scaled across GrayTRC
  295. return BuildGrayInputMatrixPipeline(hProfile);
  296. }
  297. // Not gray, create a normal matrix-shaper
  298. return BuildRGBInputMatrixShaper(hProfile);
  299. }
  300. // ---------------------------------------------------------------------------------------------------------------
  301. // Gray output pipeline.
  302. // XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
  303. // given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
  304. // The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
  305. static
  306. cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
  307. {
  308. cmsToneCurve *GrayTRC, *RevGrayTRC;
  309. cmsPipeline* Lut;
  310. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  311. GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
  312. if (GrayTRC == NULL) return NULL;
  313. RevGrayTRC = cmsReverseToneCurve(GrayTRC);
  314. if (RevGrayTRC == NULL) return NULL;
  315. Lut = cmsPipelineAlloc(ContextID, 3, 1);
  316. if (Lut == NULL) {
  317. cmsFreeToneCurve(RevGrayTRC);
  318. return NULL;
  319. }
  320. if (cmsGetPCS(hProfile) == cmsSigLabData) {
  321. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL)))
  322. goto Error;
  323. }
  324. else {
  325. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickYMatrix, NULL)))
  326. goto Error;
  327. }
  328. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))
  329. goto Error;
  330. cmsFreeToneCurve(RevGrayTRC);
  331. return Lut;
  332. Error:
  333. cmsFreeToneCurve(RevGrayTRC);
  334. cmsPipelineFree(Lut);
  335. return NULL;
  336. }
  337. static
  338. cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
  339. {
  340. cmsPipeline* Lut;
  341. cmsToneCurve *Shapes[3], *InvShapes[3];
  342. cmsMAT3 Mat, Inv;
  343. int i, j;
  344. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  345. if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
  346. return NULL;
  347. if (!_cmsMAT3inverse(&Mat, &Inv))
  348. return NULL;
  349. // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
  350. // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
  351. // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
  352. for (i=0; i < 3; i++)
  353. for (j=0; j < 3; j++)
  354. Inv.v[i].n[j] *= OutpAdj;
  355. Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
  356. Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
  357. Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
  358. if (!Shapes[0] || !Shapes[1] || !Shapes[2])
  359. return NULL;
  360. InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
  361. InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
  362. InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
  363. if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
  364. return NULL;
  365. }
  366. Lut = cmsPipelineAlloc(ContextID, 3, 3);
  367. if (Lut != NULL) {
  368. // Note that it is certainly possible a single profile would have a LUT based
  369. // tag for output working in lab and a matrix-shaper for the fallback cases.
  370. // This is not allowed by the spec, but this code is tolerant to those cases
  371. if (cmsGetPCS(hProfile) == cmsSigLabData) {
  372. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))
  373. goto Error;
  374. }
  375. if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||
  376. !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))
  377. goto Error;
  378. }
  379. cmsFreeToneCurveTriple(InvShapes);
  380. return Lut;
  381. Error:
  382. cmsFreeToneCurveTriple(InvShapes);
  383. cmsPipelineFree(Lut);
  384. return NULL;
  385. }
  386. // Change CLUT interpolation to trilinear
  387. static
  388. void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
  389. {
  390. cmsStage* Stage;
  391. for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
  392. Stage != NULL;
  393. Stage = cmsStageNext(Stage)) {
  394. if (cmsStageType(Stage) == cmsSigCLutElemType) {
  395. _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
  396. CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
  397. _cmsSetInterpolationRoutine(CLUT ->Params);
  398. }
  399. }
  400. }
  401. // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
  402. static
  403. cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
  404. {
  405. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  406. cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
  407. cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
  408. cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
  409. if (Lut == NULL) return NULL;
  410. // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
  411. // and since the formatter has already accomodated to 0..1.0, we should undo this change
  412. if ( PCS == cmsSigLabData)
  413. {
  414. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
  415. goto Error;
  416. }
  417. else
  418. if (PCS == cmsSigXYZData)
  419. {
  420. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
  421. goto Error;
  422. }
  423. // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
  424. if ( dataSpace == cmsSigLabData)
  425. {
  426. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
  427. goto Error;
  428. }
  429. else if (dataSpace == cmsSigXYZData)
  430. {
  431. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
  432. goto Error;
  433. }
  434. return Lut;
  435. Error:
  436. cmsPipelineFree(Lut);
  437. return NULL;
  438. }
  439. // Create an output MPE LUT from agiven profile. Version mismatches are handled here
  440. cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
  441. {
  442. cmsTagTypeSignature OriginalType;
  443. cmsTagSignature tag16 = PCS2Device16[Intent];
  444. cmsTagSignature tagFloat = PCS2DeviceFloat[Intent];
  445. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  446. if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
  447. // Floating point LUT are always V4
  448. return _cmsReadFloatOutputTag(hProfile, tagFloat);
  449. }
  450. // Revert to perceptual if no tag is found
  451. if (!cmsIsTag(hProfile, tag16)) {
  452. tag16 = PCS2Device16[0];
  453. }
  454. if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
  455. // Check profile version and LUT type. Do the necessary adjustments if needed
  456. // First read the tag
  457. cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
  458. if (Lut == NULL) return NULL;
  459. // After reading it, we have info about the original type
  460. OriginalType = _cmsGetTagTrueType(hProfile, tag16);
  461. // The profile owns the Lut, so we need to copy it
  462. Lut = cmsPipelineDup(Lut);
  463. if (Lut == NULL) return NULL;
  464. // Now it is time for a controversial stuff. I found that for 3D LUTS using
  465. // Lab used as indexer space, trilinear interpolation should be used
  466. if (cmsGetPCS(hProfile) == cmsSigLabData)
  467. ChangeInterpolationToTrilinear(Lut);
  468. // We need to adjust data only for Lab and Lut16 type
  469. if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
  470. return Lut;
  471. // Add a matrix for conversion V4 to V2 Lab PCS
  472. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
  473. goto Error;
  474. // If the output is Lab, add also a conversion at the end
  475. if (cmsGetColorSpace(hProfile) == cmsSigLabData)
  476. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
  477. goto Error;
  478. return Lut;
  479. Error:
  480. cmsPipelineFree(Lut);
  481. return NULL;
  482. }
  483. // Lut not found, try to create a matrix-shaper
  484. // Check if this is a grayscale profile.
  485. if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
  486. // if so, build appropiate conversion tables.
  487. // The tables are the PCS iluminant, scaled across GrayTRC
  488. return BuildGrayOutputPipeline(hProfile);
  489. }
  490. // Not gray, create a normal matrix-shaper, which only operates in XYZ space
  491. return BuildRGBOutputMatrixShaper(hProfile);
  492. }
  493. // ---------------------------------------------------------------------------------------------------------------
  494. // Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
  495. static
  496. cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
  497. {
  498. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  499. cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
  500. cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
  501. cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
  502. if (Lut == NULL) return NULL;
  503. if (spc == cmsSigLabData)
  504. {
  505. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
  506. goto Error;
  507. }
  508. else
  509. if (spc == cmsSigXYZData)
  510. {
  511. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
  512. goto Error;
  513. }
  514. if (PCS == cmsSigLabData)
  515. {
  516. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
  517. goto Error;
  518. }
  519. else
  520. if (PCS == cmsSigXYZData)
  521. {
  522. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
  523. goto Error;
  524. }
  525. return Lut;
  526. Error:
  527. cmsPipelineFree(Lut);
  528. return NULL;
  529. }
  530. // This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
  531. // tag name here may default to AToB0
  532. cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
  533. {
  534. cmsPipeline* Lut;
  535. cmsTagTypeSignature OriginalType;
  536. cmsTagSignature tag16 = Device2PCS16[Intent];
  537. cmsTagSignature tagFloat = Device2PCSFloat[Intent];
  538. cmsContext ContextID = cmsGetProfileContextID(hProfile);
  539. // On named color, take the appropiate tag
  540. if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
  541. cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
  542. if (nc == NULL) return NULL;
  543. Lut = cmsPipelineAlloc(ContextID, 0, 0);
  544. if (Lut == NULL)
  545. goto Error;
  546. if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))
  547. goto Error;
  548. if (cmsGetColorSpace(hProfile) == cmsSigLabData)
  549. if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
  550. goto Error;
  551. return Lut;
  552. Error:
  553. cmsPipelineFree(Lut);
  554. cmsFreeNamedColorList(nc);
  555. return NULL;
  556. }
  557. if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
  558. // Floating point LUT are always V
  559. return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
  560. }
  561. tagFloat = Device2PCSFloat[0];
  562. if (cmsIsTag(hProfile, tagFloat)) {
  563. return cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
  564. }
  565. if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
  566. tag16 = Device2PCS16[0];
  567. if (!cmsIsTag(hProfile, tag16)) return NULL;
  568. }
  569. // Check profile version and LUT type. Do the necessary adjustments if needed
  570. // Read the tag
  571. Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
  572. if (Lut == NULL) return NULL;
  573. // The profile owns the Lut, so we need to copy it
  574. Lut = cmsPipelineDup(Lut);
  575. if (Lut == NULL) return NULL;
  576. // Now it is time for a controversial stuff. I found that for 3D LUTS using
  577. // Lab used as indexer space, trilinear interpolation should be used
  578. if (cmsGetColorSpace(hProfile) == cmsSigLabData)
  579. ChangeInterpolationToTrilinear(Lut);
  580. // After reading it, we have info about the original type
  581. OriginalType = _cmsGetTagTrueType(hProfile, tag16);
  582. // We need to adjust data for Lab16 on output
  583. if (OriginalType != cmsSigLut16Type) return Lut;
  584. // Here it is possible to get Lab on both sides
  585. if (cmsGetPCS(hProfile) == cmsSigLabData) {
  586. if(!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
  587. goto Error2;
  588. }
  589. if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
  590. if(!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
  591. goto Error2;
  592. }
  593. return Lut;
  594. Error2:
  595. cmsPipelineFree(Lut);
  596. return NULL;
  597. }
  598. // ---------------------------------------------------------------------------------------------------------------
  599. // Returns TRUE if the profile is implemented as matrix-shaper
  600. cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
  601. {
  602. switch (cmsGetColorSpace(hProfile)) {
  603. case cmsSigGrayData:
  604. return cmsIsTag(hProfile, cmsSigGrayTRCTag);
  605. case cmsSigRgbData:
  606. return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
  607. cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
  608. cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
  609. cmsIsTag(hProfile, cmsSigRedTRCTag) &&
  610. cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
  611. cmsIsTag(hProfile, cmsSigBlueTRCTag));
  612. default:
  613. return FALSE;
  614. }
  615. }
  616. // Returns TRUE if the intent is implemented as CLUT
  617. cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
  618. {
  619. const cmsTagSignature* TagTable;
  620. // For devicelinks, the supported intent is that one stated in the header
  621. if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
  622. return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
  623. }
  624. switch (UsedDirection) {
  625. case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
  626. case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
  627. // For proofing, we need rel. colorimetric in output. Let's do some recursion
  628. case LCMS_USED_AS_PROOF:
  629. return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
  630. cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
  631. default:
  632. cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
  633. return FALSE;
  634. }
  635. return cmsIsTag(hProfile, TagTable[Intent]);
  636. }
  637. // Return info about supported intents
  638. cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
  639. cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
  640. {
  641. if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
  642. // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
  643. // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
  644. // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
  645. // the accuracy would be less than optimal in rel.col and v2 case.
  646. return cmsIsMatrixShaper(hProfile);
  647. }
  648. // ---------------------------------------------------------------------------------------------------------------
  649. // Read both, profile sequence description and profile sequence id if present. Then combine both to
  650. // create qa unique structure holding both. Shame on ICC to store things in such complicated way.
  651. cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
  652. {
  653. cmsSEQ* ProfileSeq;
  654. cmsSEQ* ProfileId;
  655. cmsSEQ* NewSeq;
  656. cmsUInt32Number i;
  657. // Take profile sequence description first
  658. ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
  659. // Take profile sequence ID
  660. ProfileId = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
  661. if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
  662. if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
  663. if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
  664. // We have to mix both together. For that they must agree
  665. if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
  666. NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
  667. // Ok, proceed to the mixing
  668. if (NewSeq != NULL) {
  669. for (i=0; i < ProfileSeq ->n; i++) {
  670. memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
  671. NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
  672. }
  673. }
  674. return NewSeq;
  675. }
  676. // Dump the contents of profile sequence in both tags (if v4 available)
  677. cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
  678. {
  679. if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
  680. if (cmsGetProfileVersion(hProfile) >= 4.0) {
  681. if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
  682. }
  683. return TRUE;
  684. }
  685. // Auxiliar, read and duplicate a MLU if found.
  686. static
  687. cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
  688. {
  689. cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
  690. if (mlu == NULL) return NULL;
  691. return cmsMLUdup(mlu);
  692. }
  693. // Create a sequence description out of an array of profiles
  694. cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
  695. {
  696. cmsUInt32Number i;
  697. cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
  698. if (seq == NULL) return NULL;
  699. for (i=0; i < nProfiles; i++) {
  700. cmsPSEQDESC* ps = &seq ->seq[i];
  701. cmsHPROFILE h = hProfiles[i];
  702. cmsTechnologySignature* techpt;
  703. cmsGetHeaderAttributes(h, &ps ->attributes);
  704. cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
  705. ps ->deviceMfg = cmsGetHeaderManufacturer(h);
  706. ps ->deviceModel = cmsGetHeaderModel(h);
  707. techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
  708. if (techpt == NULL)
  709. ps ->technology = (cmsTechnologySignature) 0;
  710. else
  711. ps ->technology = *techpt;
  712. ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);
  713. ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);
  714. ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
  715. }
  716. return seq;
  717. }
  718. // -------------------------------------------------------------------------------------------------------------------
  719. static
  720. const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
  721. {
  722. cmsTagSignature sig;
  723. switch (Info) {
  724. case cmsInfoDescription:
  725. sig = cmsSigProfileDescriptionTag;
  726. break;
  727. case cmsInfoManufacturer:
  728. sig = cmsSigDeviceMfgDescTag;
  729. break;
  730. case cmsInfoModel:
  731. sig = cmsSigDeviceModelDescTag;
  732. break;
  733. case cmsInfoCopyright:
  734. sig = cmsSigCopyrightTag;
  735. break;
  736. default: return NULL;
  737. }
  738. return (cmsMLU*) cmsReadTag(hProfile, sig);
  739. }
  740. cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
  741. const char LanguageCode[3], const char CountryCode[3],
  742. wchar_t* Buffer, cmsUInt32Number BufferSize)
  743. {
  744. const cmsMLU* mlu = GetInfo(hProfile, Info);
  745. if (mlu == NULL) return 0;
  746. return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
  747. }
  748. cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
  749. const char LanguageCode[3], const char CountryCode[3],
  750. char* Buffer, cmsUInt32Number BufferSize)
  751. {
  752. const cmsMLU* mlu = GetInfo(hProfile, Info);
  753. if (mlu == NULL) return 0;
  754. return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
  755. }