transicc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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. #include "utils.h"
  27. #ifndef _MSC_VER
  28. # include <unistd.h>
  29. #endif
  30. #ifdef CMS_IS_WINDOWS_
  31. # include <io.h>
  32. #endif
  33. #define MAX_INPUT_BUFFER 4096
  34. // Global options
  35. static cmsBool InHexa = FALSE;
  36. static cmsBool GamutCheck = FALSE;
  37. static cmsBool Width16 = FALSE;
  38. static cmsBool BlackPointCompensation = FALSE;
  39. static cmsBool lIsDeviceLink = FALSE;
  40. static cmsBool lQuantize = FALSE;
  41. static cmsBool lUnbounded = TRUE;
  42. static cmsBool lIsFloat = TRUE;
  43. static cmsUInt32Number Intent = INTENT_PERCEPTUAL;
  44. static cmsUInt32Number ProofingIntent = INTENT_PERCEPTUAL;
  45. static int PrecalcMode = 0;
  46. // --------------------------------------------------------------
  47. static char *cInProf = NULL;
  48. static char *cOutProf = NULL;
  49. static char *cProofing = NULL;
  50. static char *IncludePart = NULL;
  51. static cmsHANDLE hIT8in = NULL; // CGATS input
  52. static cmsHANDLE hIT8out = NULL; // CGATS output
  53. static char CGATSPatch[1024]; // Actual Patch Name
  54. static char CGATSoutFilename[cmsMAX_PATH];
  55. static int nMaxPatches;
  56. static cmsHTRANSFORM hTrans, hTransXYZ, hTransLab;
  57. static cmsBool InputNamedColor = FALSE;
  58. static cmsColorSpaceSignature InputColorSpace, OutputColorSpace;
  59. static cmsNAMEDCOLORLIST* InputColorant = NULL;
  60. static cmsNAMEDCOLORLIST* OutputColorant = NULL;
  61. static cmsFloat64Number InputRange, OutputRange;
  62. // isatty replacement
  63. #ifdef _MSC_VER
  64. #define xisatty(x) _isatty( _fileno( (x) ) )
  65. #else
  66. #define xisatty(x) isatty( fileno( (x) ) )
  67. #endif
  68. //---------------------------------------------------------------------------------------------------
  69. // Print usage to stderr
  70. static
  71. void Help(void)
  72. {
  73. fprintf(stderr, "usage: transicc [flags] [CGATS input] [CGATS output]\n\n");
  74. fprintf(stderr, "flags:\n\n");
  75. fprintf(stderr, "-v<0..3> - Verbosity level\n");
  76. fprintf(stderr, "-e[op] - Encoded representation of numbers\n");
  77. fprintf(stderr, "\t-w - use 16 bits\n");
  78. fprintf(stderr, "\t-x - Hexadecimal\n\n");
  79. fprintf(stderr, "-s - bounded mode (clip negatives and highlights)\n");
  80. fprintf(stderr, "-q - Quantize (round decimals)\n\n");
  81. fprintf(stderr, "-i<profile> - Input profile (defaults to sRGB)\n");
  82. fprintf(stderr, "-o<profile> - Output profile (defaults to sRGB)\n");
  83. fprintf(stderr, "-l<profile> - Transform by device-link profile\n");
  84. PrintBuiltins();
  85. PrintRenderingIntents();
  86. fprintf(stderr, "\n");
  87. fprintf(stderr, "-d<0..1> - Observer adaptation state (abs.col. only)\n\n");
  88. fprintf(stderr, "-b - Black point compensation\n");
  89. fprintf(stderr, "-c<0,1,2,3> Precalculates transform (0=Off, 1=Normal, 2=Hi-res, 3=LoRes)\n\n");
  90. fprintf(stderr, "-n - Terse output, intended for pipe usage\n");
  91. fprintf(stderr, "-p<profile> - Soft proof profile\n");
  92. fprintf(stderr, "-m<0,1,2,3> - Soft proof intent\n");
  93. fprintf(stderr, "-g - Marks out-of-gamut colors on softproof\n\n");
  94. fprintf(stderr, "This program is intended to be a demo of the Little CMS\n"
  95. "color engine. Both lcms and this program are open source.\n"
  96. "You can obtain both in source code at https://www.littlecms.com\n"
  97. "For suggestions, comments, bug reports etc. send mail to\n"
  98. "info@littlecms.com\n\n");
  99. }
  100. // The toggles stuff
  101. static
  102. void HandleSwitches(int argc, char *argv[])
  103. {
  104. int s;
  105. while ((s = xgetopt(argc, argv,
  106. "bBC:c:d:D:eEgGI:i:L:l:m:M:nNO:o:p:P:QqSsT:t:V:v:WwxX!:-:")) != EOF) {
  107. switch (s){
  108. case '-':
  109. if (strcmp(xoptarg, "help") == 0)
  110. {
  111. Help();
  112. exit(0);
  113. }
  114. else
  115. {
  116. FatalError("Unknown option - run without args to see valid ones.\n");
  117. }
  118. break;
  119. case '!':
  120. IncludePart = xoptarg;
  121. break;
  122. case 'b':
  123. case 'B':
  124. BlackPointCompensation = TRUE;
  125. break;
  126. case 'c':
  127. case 'C':
  128. PrecalcMode = atoi(xoptarg);
  129. if (PrecalcMode < 0 || PrecalcMode > 3)
  130. FatalError("Unknown precalc mode '%d'", PrecalcMode);
  131. break;
  132. case 'd':
  133. case 'D': {
  134. cmsFloat64Number ObserverAdaptationState = atof(xoptarg);
  135. if (ObserverAdaptationState < 0 ||
  136. ObserverAdaptationState > 1.0)
  137. FatalError("Adaptation states should be between 0 and 1");
  138. cmsSetAdaptationState(ObserverAdaptationState);
  139. }
  140. break;
  141. case 'e':
  142. case 'E':
  143. lIsFloat = FALSE;
  144. break;
  145. case 'g':
  146. case 'G':
  147. GamutCheck = TRUE;
  148. break;
  149. case 'i':
  150. case 'I':
  151. if (lIsDeviceLink)
  152. FatalError("icctrans: Device-link already specified");
  153. cInProf = xoptarg;
  154. break;
  155. case 'l':
  156. case 'L':
  157. cInProf = xoptarg;
  158. lIsDeviceLink = TRUE;
  159. break;
  160. // No extra intents for proofing
  161. case 'm':
  162. case 'M':
  163. ProofingIntent = atoi(xoptarg);
  164. if (ProofingIntent > 3)
  165. FatalError("Unknown Proofing Intent '%d'", ProofingIntent);
  166. break;
  167. // For compatibility
  168. case 'n':
  169. case 'N':
  170. Verbose = 0;
  171. break;
  172. // Output profile
  173. case 'o':
  174. case 'O':
  175. if (lIsDeviceLink)
  176. FatalError("icctrans: Device-link already specified");
  177. cOutProf = xoptarg;
  178. break;
  179. // Proofing profile
  180. case 'p':
  181. case 'P':
  182. cProofing = xoptarg;
  183. break;
  184. // Quantize (get rid of decimals)
  185. case 'q':
  186. case 'Q':
  187. lQuantize = TRUE;
  188. break;
  189. // Inhibit unbounded mode
  190. case 's':
  191. case 'S':
  192. lUnbounded = FALSE;
  193. break;
  194. // The intent
  195. case 't':
  196. case 'T':
  197. Intent = atoi(xoptarg);
  198. break;
  199. // Verbosity level
  200. case 'V':
  201. case 'v':
  202. Verbose = atoi(xoptarg);
  203. if (Verbose < 0 || Verbose > 3) {
  204. FatalError("Unknown verbosity level '%d'", Verbose);
  205. }
  206. break;
  207. // Wide (16 bits)
  208. case 'W':
  209. case 'w':
  210. Width16 = TRUE;
  211. break;
  212. // Hexadecimal
  213. case 'x':
  214. case 'X':
  215. InHexa = TRUE;
  216. break;
  217. default:
  218. FatalError("Unknown option - run without args to see valid ones.\n");
  219. }
  220. }
  221. // If output CGATS involved, switch to float
  222. if ((argc - xoptind) > 2) {
  223. lIsFloat = TRUE;
  224. }
  225. }
  226. static
  227. void SetRange(cmsFloat64Number range, cmsBool IsInput)
  228. {
  229. if (IsInput)
  230. InputRange = range;
  231. else
  232. OutputRange = range;
  233. }
  234. // Populate a named color list with usual component names.
  235. // I am using the first Colorant channel to store the range, but it works since
  236. // this space is not used anyway.
  237. static
  238. cmsNAMEDCOLORLIST* ComponentNames(cmsColorSpaceSignature space, cmsBool IsInput)
  239. {
  240. cmsNAMEDCOLORLIST* out;
  241. int i, n;
  242. char Buffer[cmsMAX_PATH];
  243. out = cmsAllocNamedColorList(0, 12, cmsMAXCHANNELS, "", "");
  244. if (out == NULL) return NULL;
  245. switch (space) {
  246. case cmsSigXYZData:
  247. SetRange(100, IsInput);
  248. cmsAppendNamedColor(out, "X", NULL, NULL);
  249. cmsAppendNamedColor(out, "Y", NULL, NULL);
  250. cmsAppendNamedColor(out, "Z", NULL, NULL);
  251. break;
  252. case cmsSigLabData:
  253. SetRange(1, IsInput);
  254. cmsAppendNamedColor(out, "L*", NULL, NULL);
  255. cmsAppendNamedColor(out, "a*", NULL, NULL);
  256. cmsAppendNamedColor(out, "b*", NULL, NULL);
  257. break;
  258. case cmsSigLuvData:
  259. SetRange(1, IsInput);
  260. cmsAppendNamedColor(out, "L", NULL, NULL);
  261. cmsAppendNamedColor(out, "u", NULL, NULL);
  262. cmsAppendNamedColor(out, "v", NULL, NULL);
  263. break;
  264. case cmsSigYCbCrData:
  265. SetRange(255, IsInput);
  266. cmsAppendNamedColor(out, "Y", NULL, NULL );
  267. cmsAppendNamedColor(out, "Cb", NULL, NULL);
  268. cmsAppendNamedColor(out, "Cr", NULL, NULL);
  269. break;
  270. case cmsSigYxyData:
  271. SetRange(1, IsInput);
  272. cmsAppendNamedColor(out, "Y", NULL, NULL);
  273. cmsAppendNamedColor(out, "x", NULL, NULL);
  274. cmsAppendNamedColor(out, "y", NULL, NULL);
  275. break;
  276. case cmsSigRgbData:
  277. SetRange(255, IsInput);
  278. cmsAppendNamedColor(out, "R", NULL, NULL);
  279. cmsAppendNamedColor(out, "G", NULL, NULL);
  280. cmsAppendNamedColor(out, "B", NULL, NULL);
  281. break;
  282. case cmsSigGrayData:
  283. SetRange(255, IsInput);
  284. cmsAppendNamedColor(out, "G", NULL, NULL);
  285. break;
  286. case cmsSigHsvData:
  287. SetRange(255, IsInput);
  288. cmsAppendNamedColor(out, "H", NULL, NULL);
  289. cmsAppendNamedColor(out, "s", NULL, NULL);
  290. cmsAppendNamedColor(out, "v", NULL, NULL);
  291. break;
  292. case cmsSigHlsData:
  293. SetRange(255, IsInput);
  294. cmsAppendNamedColor(out, "H", NULL, NULL);
  295. cmsAppendNamedColor(out, "l", NULL, NULL);
  296. cmsAppendNamedColor(out, "s", NULL, NULL);
  297. break;
  298. case cmsSigCmykData:
  299. SetRange(1, IsInput);
  300. cmsAppendNamedColor(out, "C", NULL, NULL);
  301. cmsAppendNamedColor(out, "M", NULL, NULL);
  302. cmsAppendNamedColor(out, "Y", NULL, NULL);
  303. cmsAppendNamedColor(out, "K", NULL, NULL);
  304. break;
  305. case cmsSigCmyData:
  306. SetRange(1, IsInput);
  307. cmsAppendNamedColor(out, "C", NULL, NULL);
  308. cmsAppendNamedColor(out, "M", NULL, NULL);
  309. cmsAppendNamedColor(out, "Y", NULL, NULL);
  310. break;
  311. default:
  312. SetRange(1, IsInput);
  313. n = cmsChannelsOfColorSpace(space);
  314. for (i=0; i < n; i++) {
  315. sprintf(Buffer, "Channel #%d", i + 1);
  316. cmsAppendNamedColor(out, Buffer, NULL, NULL);
  317. }
  318. }
  319. return out;
  320. }
  321. // Creates all needed color transforms
  322. static
  323. cmsBool OpenTransforms(void)
  324. {
  325. cmsHPROFILE hInput, hOutput, hProof;
  326. cmsUInt32Number dwIn, dwOut, dwFlags;
  327. cmsNAMEDCOLORLIST* List;
  328. int i;
  329. // We don't need cache
  330. dwFlags = cmsFLAGS_NOCACHE;
  331. if (lIsDeviceLink) {
  332. hInput = OpenStockProfile(0, cInProf);
  333. if (hInput == NULL) return FALSE;
  334. hOutput = NULL;
  335. hProof = NULL;
  336. if (cmsGetDeviceClass(hInput) == cmsSigNamedColorClass) {
  337. OutputColorSpace = cmsGetColorSpace(hInput);
  338. InputColorSpace = cmsGetPCS(hInput);
  339. }
  340. else {
  341. InputColorSpace = cmsGetColorSpace(hInput);
  342. OutputColorSpace = cmsGetPCS(hInput);
  343. }
  344. // Read colorant tables if present
  345. if (cmsIsTag(hInput, cmsSigColorantTableTag)) {
  346. List = cmsReadTag(hInput, cmsSigColorantTableTag);
  347. InputColorant = cmsDupNamedColorList(List);
  348. InputRange = 1;
  349. }
  350. else InputColorant = ComponentNames(InputColorSpace, TRUE);
  351. if (cmsIsTag(hInput, cmsSigColorantTableOutTag)){
  352. List = cmsReadTag(hInput, cmsSigColorantTableOutTag);
  353. OutputColorant = cmsDupNamedColorList(List);
  354. OutputRange = 1;
  355. }
  356. else OutputColorant = ComponentNames(OutputColorSpace, FALSE);
  357. }
  358. else {
  359. hInput = OpenStockProfile(0, cInProf);
  360. if (hInput == NULL) return FALSE;
  361. hOutput = OpenStockProfile(0, cOutProf);
  362. if (hOutput == NULL) return FALSE;
  363. hProof = NULL;
  364. if (cmsGetDeviceClass(hInput) == cmsSigLinkClass ||
  365. cmsGetDeviceClass(hOutput) == cmsSigLinkClass)
  366. FatalError("Use -l flag for devicelink profiles!\n");
  367. InputColorSpace = cmsGetColorSpace(hInput);
  368. OutputColorSpace = cmsGetColorSpace(hOutput);
  369. // Read colorant tables if present
  370. if (cmsIsTag(hInput, cmsSigColorantTableTag)) {
  371. List = cmsReadTag(hInput, cmsSigColorantTableTag);
  372. InputColorant = cmsDupNamedColorList(List);
  373. if (cmsNamedColorCount(InputColorant) <= 3)
  374. SetRange(255, TRUE);
  375. else
  376. SetRange(1, TRUE); // Inks are already divided by 100 in the formatter
  377. }
  378. else InputColorant = ComponentNames(InputColorSpace, TRUE);
  379. if (cmsIsTag(hOutput, cmsSigColorantTableTag)){
  380. List = cmsReadTag(hOutput, cmsSigColorantTableTag);
  381. OutputColorant = cmsDupNamedColorList(List);
  382. if (cmsNamedColorCount(OutputColorant) <= 3)
  383. SetRange(255, FALSE);
  384. else
  385. SetRange(1, FALSE); // Inks are already divided by 100 in the formatter
  386. }
  387. else OutputColorant = ComponentNames(OutputColorSpace, FALSE);
  388. if (cProofing != NULL) {
  389. hProof = OpenStockProfile(0, cProofing);
  390. if (hProof == NULL) return FALSE;
  391. dwFlags |= cmsFLAGS_SOFTPROOFING;
  392. }
  393. }
  394. // Print information on profiles
  395. if (Verbose > 2) {
  396. printf("Profile:\n");
  397. PrintProfileInformation(hInput);
  398. if (hOutput) {
  399. printf("Output profile:\n");
  400. PrintProfileInformation(hOutput);
  401. }
  402. if (hProof != NULL) {
  403. printf("Proofing profile:\n");
  404. PrintProfileInformation(hProof);
  405. }
  406. }
  407. // Input is always in floating point
  408. dwIn = cmsFormatterForColorspaceOfProfile(hInput, 0, TRUE);
  409. if (lIsDeviceLink) {
  410. dwOut = cmsFormatterForPCSOfProfile(hInput, lIsFloat ? 0 : 2, lIsFloat);
  411. }
  412. else {
  413. // 16 bits or floating point (only on output)
  414. dwOut = cmsFormatterForColorspaceOfProfile(hOutput, lIsFloat ? 0 : 2, lIsFloat);
  415. }
  416. // For named color, there is a specialized formatter
  417. if (cmsGetDeviceClass(hInput) == cmsSigNamedColorClass) {
  418. dwIn = TYPE_NAMED_COLOR_INDEX;
  419. InputNamedColor = TRUE;
  420. }
  421. // Precision mode
  422. switch (PrecalcMode) {
  423. case 0: dwFlags |= cmsFLAGS_NOOPTIMIZE; break;
  424. case 2: dwFlags |= cmsFLAGS_HIGHRESPRECALC; break;
  425. case 3: dwFlags |= cmsFLAGS_LOWRESPRECALC; break;
  426. case 1: break;
  427. default:
  428. FatalError("Unknown precalculation mode '%d'", PrecalcMode);
  429. }
  430. if (BlackPointCompensation)
  431. dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
  432. if (GamutCheck) {
  433. cmsUInt16Number Alarm[cmsMAXCHANNELS];
  434. if (hProof == NULL)
  435. FatalError("I need proofing profile -p for gamut checking!");
  436. for (i=0; i < cmsMAXCHANNELS; i++)
  437. Alarm[i] = 0xFFFF;
  438. cmsSetAlarmCodes(Alarm);
  439. dwFlags |= cmsFLAGS_GAMUTCHECK;
  440. }
  441. // The main transform
  442. hTrans = cmsCreateProofingTransform(hInput, dwIn, hOutput, dwOut, hProof, Intent, ProofingIntent, dwFlags);
  443. if (hProof) cmsCloseProfile(hProof);
  444. if (hTrans == NULL) return FALSE;
  445. // PCS Dump if requested
  446. hTransXYZ = NULL; hTransLab = NULL;
  447. if (hOutput && Verbose > 1) {
  448. cmsHPROFILE hXYZ = cmsCreateXYZProfile();
  449. cmsHPROFILE hLab = cmsCreateLab4Profile(NULL);
  450. hTransXYZ = cmsCreateTransform(hInput, dwIn, hXYZ, lIsFloat ? TYPE_XYZ_DBL : TYPE_XYZ_16, Intent, cmsFLAGS_NOCACHE);
  451. if (hTransXYZ == NULL) return FALSE;
  452. hTransLab = cmsCreateTransform(hInput, dwIn, hLab, lIsFloat? TYPE_Lab_DBL : TYPE_Lab_16, Intent, cmsFLAGS_NOCACHE);
  453. if (hTransLab == NULL) return FALSE;
  454. cmsCloseProfile(hXYZ);
  455. cmsCloseProfile(hLab);
  456. }
  457. if (hInput) cmsCloseProfile(hInput);
  458. if (hOutput) cmsCloseProfile(hOutput);
  459. return TRUE;
  460. }
  461. // Free open resources
  462. static
  463. void CloseTransforms(void)
  464. {
  465. if (InputColorant) cmsFreeNamedColorList(InputColorant);
  466. if (OutputColorant) cmsFreeNamedColorList(OutputColorant);
  467. if (hTrans) cmsDeleteTransform(hTrans);
  468. if (hTransLab) cmsDeleteTransform(hTransLab);
  469. if (hTransXYZ) cmsDeleteTransform(hTransXYZ);
  470. }
  471. // ---------------------------------------------------------------------------------------------------
  472. // Get input from user
  473. static
  474. void GetLine(char* Buffer, const char* frm, ...)
  475. {
  476. int res;
  477. va_list args;
  478. va_start(args, frm);
  479. do {
  480. if (xisatty(stdin))
  481. vfprintf(stderr, frm, args);
  482. res = scanf("%4095s", Buffer);
  483. if (res < 0 || toupper(Buffer[0]) == 'Q') { // Quit?
  484. CloseTransforms();
  485. if (xisatty(stdin))
  486. fprintf(stderr, "Done.\n");
  487. exit(0);
  488. }
  489. } while (res == 0);
  490. va_end(args);
  491. }
  492. // Print a value which is given in double floating point
  493. static
  494. void PrintFloatResults(cmsFloat64Number Value[])
  495. {
  496. cmsUInt32Number i, n;
  497. char ChannelName[cmsMAX_PATH];
  498. cmsFloat64Number v;
  499. n = cmsChannelsOfColorSpace(OutputColorSpace);
  500. for (i=0; i < n; i++) {
  501. if (OutputColorant != NULL) {
  502. cmsNamedColorInfo(OutputColorant, i, ChannelName, NULL, NULL, NULL, NULL);
  503. }
  504. else {
  505. OutputRange = 1;
  506. sprintf(ChannelName, "Channel #%u", i + 1);
  507. }
  508. v = (cmsFloat64Number) Value[i]* OutputRange;
  509. if (lQuantize)
  510. v = floor(v + 0.5);
  511. if (!lUnbounded) {
  512. if (v < 0)
  513. v = 0;
  514. if (v > OutputRange)
  515. v = OutputRange;
  516. }
  517. if (Verbose <= 0)
  518. printf("%.4f ", v);
  519. else
  520. printf("%s=%.4f ", ChannelName, v);
  521. }
  522. printf("\n");
  523. }
  524. // Get a named-color index
  525. static
  526. cmsUInt16Number GetIndex(void)
  527. {
  528. char Buffer[4096], Name[cmsMAX_PATH], Prefix[40], Suffix[40];
  529. int index, max;
  530. const cmsNAMEDCOLORLIST* NamedColorList;
  531. NamedColorList = cmsGetNamedColorList(hTrans);
  532. if (NamedColorList == NULL) return 0;
  533. max = cmsNamedColorCount(NamedColorList)-1;
  534. GetLine(Buffer, "Color index (0..%d)? ", max);
  535. index = atoi(Buffer);
  536. if (index > max)
  537. FatalError("Named color %d out of range!", index);
  538. cmsNamedColorInfo(NamedColorList, index, Name, Prefix, Suffix, NULL, NULL);
  539. printf("\n%s %s %s\n", Prefix, Name, Suffix);
  540. return (cmsUInt16Number) index;
  541. }
  542. // Read values from a text file or terminal
  543. static
  544. void TakeFloatValues(cmsFloat64Number Float[])
  545. {
  546. cmsUInt32Number i, n;
  547. char ChannelName[cmsMAX_PATH];
  548. char Buffer[4096];
  549. if (xisatty(stdin))
  550. fprintf(stderr, "\nEnter values, 'q' to quit\n");
  551. if (InputNamedColor) {
  552. // This is named color index, which is always cmsUInt16Number
  553. cmsUInt16Number index = GetIndex();
  554. memcpy(Float, &index, sizeof(cmsUInt16Number));
  555. return;
  556. }
  557. n = cmsChannelsOfColorSpace(InputColorSpace);
  558. for (i=0; i < n; i++) {
  559. if (InputColorant) {
  560. cmsNamedColorInfo(InputColorant, i, ChannelName, NULL, NULL, NULL, NULL);
  561. }
  562. else {
  563. InputRange = 1;
  564. sprintf(ChannelName, "Channel #%u", i+1);
  565. }
  566. GetLine(Buffer, "%s? ", ChannelName);
  567. Float[i] = (cmsFloat64Number) atof(Buffer) / InputRange;
  568. }
  569. if (xisatty(stdin))
  570. fprintf(stderr, "\n");
  571. }
  572. static
  573. void PrintPCSFloat(cmsFloat64Number Input[])
  574. {
  575. if (Verbose > 1 && hTransXYZ && hTransLab) {
  576. cmsCIEXYZ XYZ = { 0, 0, 0 };
  577. cmsCIELab Lab = { 0, 0, 0 };
  578. if (hTransXYZ) cmsDoTransform(hTransXYZ, Input, &XYZ, 1);
  579. if (hTransLab) cmsDoTransform(hTransLab, Input, &Lab, 1);
  580. printf("[PCS] Lab=(%.4f,%.4f,%.4f) XYZ=(%.4f,%.4f,%.4f)\n", Lab.L, Lab.a, Lab.b,
  581. XYZ.X * 100.0, XYZ.Y * 100.0, XYZ.Z * 100.0);
  582. }
  583. }
  584. // -----------------------------------------------------------------------------------------------
  585. static
  586. void PrintEncodedResults(cmsUInt16Number Encoded[])
  587. {
  588. cmsUInt32Number i, n;
  589. char ChannelName[cmsMAX_PATH];
  590. cmsUInt32Number v;
  591. n = cmsChannelsOfColorSpace(OutputColorSpace);
  592. for (i=0; i < n; i++) {
  593. if (OutputColorant != NULL) {
  594. cmsNamedColorInfo(OutputColorant, i, ChannelName, NULL, NULL, NULL, NULL);
  595. }
  596. else {
  597. sprintf(ChannelName, "Channel #%u", i + 1);
  598. }
  599. if (Verbose > 0)
  600. printf("%s=", ChannelName);
  601. v = Encoded[i];
  602. if (InHexa) {
  603. if (Width16)
  604. printf("0x%04X ", (int) floor(v + .5));
  605. else
  606. printf("0x%02X ", (int) floor(v / 257. + .5));
  607. } else {
  608. if (Width16)
  609. printf("%d ", (int) floor(v + .5));
  610. else
  611. printf("%d ", (int) floor(v / 257. + .5));
  612. }
  613. }
  614. printf("\n");
  615. }
  616. // Print XYZ/Lab values on verbose mode
  617. static
  618. void PrintPCSEncoded(cmsFloat64Number Input[])
  619. {
  620. if (Verbose > 1 && hTransXYZ && hTransLab) {
  621. cmsUInt16Number XYZ[3], Lab[3];
  622. if (hTransXYZ) cmsDoTransform(hTransXYZ, Input, XYZ, 1);
  623. if (hTransLab) cmsDoTransform(hTransLab, Input, Lab, 1);
  624. printf("[PCS] Lab=(0x%04X,0x%04X,0x%04X) XYZ=(0x%04X,0x%04X,0x%04X)\n", Lab[0], Lab[1], Lab[2],
  625. XYZ[0], XYZ[1], XYZ[2]);
  626. }
  627. }
  628. // --------------------------------------------------------------------------------------
  629. // Take a value from IT8 and scale it accordly to fill a cmsUInt16Number (0..FFFF)
  630. static
  631. cmsFloat64Number GetIT8Val(const char* Name, cmsFloat64Number Max)
  632. {
  633. const char* Val = cmsIT8GetData(hIT8in, CGATSPatch, Name);
  634. if (Val == NULL)
  635. FatalError("Field '%s' not found", Name);
  636. return atof(Val) / Max;
  637. }
  638. // Read input values from CGATS file.
  639. static
  640. void TakeCGATSValues(int nPatch, cmsFloat64Number Float[])
  641. {
  642. // At first take the name if SAMPLE_ID is present
  643. if (cmsIT8GetPatchName(hIT8in, nPatch, CGATSPatch) == NULL) {
  644. FatalError("Sorry, I need 'SAMPLE_ID' on input CGATS to operate.");
  645. }
  646. // Special handling for named color profiles.
  647. // Lookup the name in the names database (the transform)
  648. if (InputNamedColor) {
  649. const cmsNAMEDCOLORLIST* NamedColorList;
  650. int index;
  651. NamedColorList = cmsGetNamedColorList(hTrans);
  652. if (NamedColorList == NULL)
  653. FatalError("Malformed named color profile");
  654. index = cmsNamedColorIndex(NamedColorList, CGATSPatch);
  655. if (index < 0)
  656. FatalError("Named color '%s' not found in the profile", CGATSPatch);
  657. Float[0] = index;
  658. return;
  659. }
  660. // Color is not a spot color, proceed.
  661. switch (InputColorSpace) {
  662. // Encoding should follow CGATS specification.
  663. case cmsSigXYZData:
  664. Float[0] = cmsIT8GetDataDbl(hIT8in, CGATSPatch, "XYZ_X") / 100.0;
  665. Float[1] = cmsIT8GetDataDbl(hIT8in, CGATSPatch, "XYZ_Y") / 100.0;
  666. Float[2] = cmsIT8GetDataDbl(hIT8in, CGATSPatch, "XYZ_Z") / 100.0;
  667. break;
  668. case cmsSigLabData:
  669. Float[0] = cmsIT8GetDataDbl(hIT8in, CGATSPatch, "LAB_L");
  670. Float[1] = cmsIT8GetDataDbl(hIT8in, CGATSPatch, "LAB_A");
  671. Float[2] = cmsIT8GetDataDbl(hIT8in, CGATSPatch, "LAB_B");
  672. break;
  673. case cmsSigRgbData:
  674. Float[0] = GetIT8Val("RGB_R", 255.0);
  675. Float[1] = GetIT8Val("RGB_G", 255.0);
  676. Float[2] = GetIT8Val("RGB_B", 255.0);
  677. break;
  678. case cmsSigGrayData:
  679. Float[0] = GetIT8Val("GRAY", 255.0);
  680. break;
  681. case cmsSigCmykData:
  682. Float[0] = GetIT8Val("CMYK_C", 1.0);
  683. Float[1] = GetIT8Val("CMYK_M", 1.0);
  684. Float[2] = GetIT8Val("CMYK_Y", 1.0);
  685. Float[3] = GetIT8Val("CMYK_K", 1.0);
  686. break;
  687. case cmsSigCmyData:
  688. Float[0] = GetIT8Val("CMY_C", 1.0);
  689. Float[1] = GetIT8Val("CMY_M", 1.0);
  690. Float[2] = GetIT8Val("CMY_Y", 1.0);
  691. break;
  692. case cmsSig1colorData:
  693. case cmsSig2colorData:
  694. case cmsSig3colorData:
  695. case cmsSig4colorData:
  696. case cmsSig5colorData:
  697. case cmsSig6colorData:
  698. case cmsSig7colorData:
  699. case cmsSig8colorData:
  700. case cmsSig9colorData:
  701. case cmsSig10colorData:
  702. case cmsSig11colorData:
  703. case cmsSig12colorData:
  704. case cmsSig13colorData:
  705. case cmsSig14colorData:
  706. case cmsSig15colorData:
  707. {
  708. cmsUInt32Number i, n;
  709. n = cmsChannelsOfColorSpace(InputColorSpace);
  710. for (i=0; i < n; i++) {
  711. char Buffer[255];
  712. sprintf(Buffer, "%uCLR_%u", n, i+1);
  713. Float[i] = GetIT8Val(Buffer, 100.0);
  714. }
  715. }
  716. break;
  717. default:
  718. {
  719. cmsUInt32Number i, n;
  720. n = cmsChannelsOfColorSpace(InputColorSpace);
  721. for (i=0; i < n; i++) {
  722. char Buffer[255];
  723. sprintf(Buffer, "CHAN_%u", i+1);
  724. Float[i] = GetIT8Val(Buffer, 1.0);
  725. }
  726. }
  727. }
  728. }
  729. static
  730. void SetCGATSfld(const char* Col, cmsFloat64Number Val)
  731. {
  732. if (lQuantize)
  733. Val = floor(Val + 0.5);
  734. if (!cmsIT8SetDataDbl(hIT8out, CGATSPatch, Col, Val)) {
  735. FatalError("couldn't set '%s' on output cgats '%s'", Col, CGATSoutFilename);
  736. }
  737. }
  738. static
  739. void PutCGATSValues(cmsFloat64Number Float[])
  740. {
  741. cmsIT8SetData(hIT8out, CGATSPatch, "SAMPLE_ID", CGATSPatch);
  742. switch (OutputColorSpace) {
  743. // Encoding should follow CGATS specification.
  744. case cmsSigXYZData:
  745. SetCGATSfld("XYZ_X", Float[0] * 100.0);
  746. SetCGATSfld("XYZ_Y", Float[1] * 100.0);
  747. SetCGATSfld("XYZ_Z", Float[2] * 100.0);
  748. break;
  749. case cmsSigLabData:
  750. SetCGATSfld("LAB_L", Float[0]);
  751. SetCGATSfld("LAB_A", Float[1]);
  752. SetCGATSfld("LAB_B", Float[2]);
  753. break;
  754. case cmsSigRgbData:
  755. SetCGATSfld("RGB_R", Float[0] * 255.0);
  756. SetCGATSfld("RGB_G", Float[1] * 255.0);
  757. SetCGATSfld("RGB_B", Float[2] * 255.0);
  758. break;
  759. case cmsSigGrayData:
  760. SetCGATSfld("GRAY", Float[0] * 255.0);
  761. break;
  762. case cmsSigCmykData:
  763. SetCGATSfld("CMYK_C", Float[0]);
  764. SetCGATSfld("CMYK_M", Float[1]);
  765. SetCGATSfld("CMYK_Y", Float[2]);
  766. SetCGATSfld("CMYK_K", Float[3]);
  767. break;
  768. case cmsSigCmyData:
  769. SetCGATSfld("CMY_C", Float[0]);
  770. SetCGATSfld("CMY_M", Float[1]);
  771. SetCGATSfld("CMY_Y", Float[2]);
  772. break;
  773. case cmsSig1colorData:
  774. case cmsSig2colorData:
  775. case cmsSig3colorData:
  776. case cmsSig4colorData:
  777. case cmsSig5colorData:
  778. case cmsSig6colorData:
  779. case cmsSig7colorData:
  780. case cmsSig8colorData:
  781. case cmsSig9colorData:
  782. case cmsSig10colorData:
  783. case cmsSig11colorData:
  784. case cmsSig12colorData:
  785. case cmsSig13colorData:
  786. case cmsSig14colorData:
  787. case cmsSig15colorData:
  788. {
  789. cmsInt32Number i, n;
  790. n = cmsChannelsOfColorSpace(InputColorSpace);
  791. for (i=0; i < n; i++) {
  792. char Buffer[255];
  793. sprintf(Buffer, "%uCLR_%u", n, i+1);
  794. SetCGATSfld(Buffer, Float[i] * 100.0);
  795. }
  796. }
  797. break;
  798. default:
  799. {
  800. cmsInt32Number i, n;
  801. n = cmsChannelsOfColorSpace(InputColorSpace);
  802. for (i=0; i < n; i++) {
  803. char Buffer[255];
  804. sprintf(Buffer, "CHAN_%u", i+1);
  805. SetCGATSfld(Buffer, Float[i]);
  806. }
  807. }
  808. }
  809. }
  810. // Create data format
  811. static
  812. void SetOutputDataFormat(void)
  813. {
  814. cmsIT8DefineDblFormat(hIT8out, "%.4g");
  815. cmsIT8SetPropertyStr(hIT8out, "ORIGINATOR", "icctrans");
  816. if (IncludePart != NULL)
  817. cmsIT8SetPropertyStr(hIT8out, ".INCLUDE", IncludePart);
  818. cmsIT8SetComment(hIT8out, "Data follows");
  819. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_SETS", nMaxPatches);
  820. switch (OutputColorSpace) {
  821. // Encoding should follow CGATS specification.
  822. case cmsSigXYZData:
  823. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", 4);
  824. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  825. cmsIT8SetDataFormat(hIT8out, 1, "XYZ_X");
  826. cmsIT8SetDataFormat(hIT8out, 2, "XYZ_Y");
  827. cmsIT8SetDataFormat(hIT8out, 3, "XYZ_Z");
  828. break;
  829. case cmsSigLabData:
  830. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", 4);
  831. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  832. cmsIT8SetDataFormat(hIT8out, 1, "LAB_L");
  833. cmsIT8SetDataFormat(hIT8out, 2, "LAB_A");
  834. cmsIT8SetDataFormat(hIT8out, 3, "LAB_B");
  835. break;
  836. case cmsSigRgbData:
  837. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", 4);
  838. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  839. cmsIT8SetDataFormat(hIT8out, 1, "RGB_R");
  840. cmsIT8SetDataFormat(hIT8out, 2, "RGB_G");
  841. cmsIT8SetDataFormat(hIT8out, 3, "RGB_B");
  842. break;
  843. case cmsSigGrayData:
  844. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", 2);
  845. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  846. cmsIT8SetDataFormat(hIT8out, 1, "GRAY");
  847. break;
  848. case cmsSigCmykData:
  849. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", 5);
  850. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  851. cmsIT8SetDataFormat(hIT8out, 1, "CMYK_C");
  852. cmsIT8SetDataFormat(hIT8out, 2, "CMYK_M");
  853. cmsIT8SetDataFormat(hIT8out, 3, "CMYK_Y");
  854. cmsIT8SetDataFormat(hIT8out, 4, "CMYK_K");
  855. break;
  856. case cmsSigCmyData:
  857. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", 4);
  858. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  859. cmsIT8SetDataFormat(hIT8out, 1, "CMY_C");
  860. cmsIT8SetDataFormat(hIT8out, 2, "CMY_M");
  861. cmsIT8SetDataFormat(hIT8out, 3, "CMY_Y");
  862. break;
  863. case cmsSig1colorData:
  864. case cmsSig2colorData:
  865. case cmsSig3colorData:
  866. case cmsSig4colorData:
  867. case cmsSig5colorData:
  868. case cmsSig6colorData:
  869. case cmsSig7colorData:
  870. case cmsSig8colorData:
  871. case cmsSig9colorData:
  872. case cmsSig10colorData:
  873. case cmsSig11colorData:
  874. case cmsSig12colorData:
  875. case cmsSig13colorData:
  876. case cmsSig14colorData:
  877. case cmsSig15colorData:
  878. {
  879. int i, n;
  880. char Buffer[255];
  881. n = cmsChannelsOfColorSpace(OutputColorSpace);
  882. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", n+1);
  883. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  884. for (i=1; i <= n; i++) {
  885. sprintf(Buffer, "%dCLR_%d", n, i);
  886. cmsIT8SetDataFormat(hIT8out, i, Buffer);
  887. }
  888. }
  889. break;
  890. default: {
  891. int i, n;
  892. char Buffer[255];
  893. n = cmsChannelsOfColorSpace(OutputColorSpace);
  894. cmsIT8SetPropertyDbl(hIT8out, "NUMBER_OF_FIELDS", n+1);
  895. cmsIT8SetDataFormat(hIT8out, 0, "SAMPLE_ID");
  896. for (i=1; i <= n; i++) {
  897. sprintf(Buffer, "CHAN_%d", i);
  898. cmsIT8SetDataFormat(hIT8out, i, Buffer);
  899. }
  900. }
  901. }
  902. }
  903. // Open CGATS if specified
  904. static
  905. void OpenCGATSFiles(int argc, char *argv[])
  906. {
  907. int nParams = argc - xoptind;
  908. if (nParams >= 1) {
  909. hIT8in = cmsIT8LoadFromFile(0, argv[xoptind]);
  910. if (hIT8in == NULL)
  911. FatalError("'%s' is not recognized as a CGATS file", argv[xoptind]);
  912. nMaxPatches = (int) cmsIT8GetPropertyDbl(hIT8in, "NUMBER_OF_SETS");
  913. }
  914. if (nParams == 2) {
  915. hIT8out = cmsIT8Alloc(NULL);
  916. SetOutputDataFormat();
  917. strncpy(CGATSoutFilename, argv[xoptind+1], cmsMAX_PATH-1);
  918. }
  919. if (nParams > 2) FatalError("Too many CGATS files");
  920. }
  921. // The main sink
  922. int main(int argc, char *argv[])
  923. {
  924. cmsUInt16Number Output[cmsMAXCHANNELS];
  925. cmsFloat64Number OutputFloat[cmsMAXCHANNELS];
  926. cmsFloat64Number InputFloat[cmsMAXCHANNELS];
  927. int nPatch = 0;
  928. fprintf(stderr, "LittleCMS ColorSpace conversion calculator - 5.1 [LittleCMS %2.2f]\n", cmsGetEncodedCMMversion() / 1000.0);
  929. fprintf(stderr, "Copyright (c) 1998-2023 Marti Maria Saguer. See COPYING file for details.\n");
  930. fflush(stderr);
  931. InitUtils("transicc");
  932. Verbose = 1;
  933. if (argc == 1) {
  934. Help();
  935. return 0;
  936. }
  937. HandleSwitches(argc, argv);
  938. // Open profiles, create transforms
  939. if (!OpenTransforms()) return 1;
  940. // Open CGATS input if specified
  941. OpenCGATSFiles(argc, argv);
  942. // Main loop: read all values and convert them
  943. for(;;) {
  944. if (hIT8in != NULL) {
  945. if (nPatch >= nMaxPatches) break;
  946. TakeCGATSValues(nPatch++, InputFloat);
  947. } else {
  948. if (feof(stdin)) break;
  949. TakeFloatValues(InputFloat);
  950. }
  951. if (lIsFloat)
  952. cmsDoTransform(hTrans, InputFloat, OutputFloat, 1);
  953. else
  954. cmsDoTransform(hTrans, InputFloat, Output, 1);
  955. if (hIT8out != NULL) {
  956. PutCGATSValues(OutputFloat);
  957. }
  958. else {
  959. if (lIsFloat) {
  960. PrintFloatResults(OutputFloat); PrintPCSFloat(InputFloat);
  961. }
  962. else {
  963. PrintEncodedResults(Output); PrintPCSEncoded(InputFloat);
  964. }
  965. }
  966. }
  967. // Cleanup
  968. CloseTransforms();
  969. if (hIT8in)
  970. cmsIT8Free(hIT8in);
  971. if (hIT8out) {
  972. cmsIT8SaveToFile(hIT8out, CGATSoutFilename);
  973. cmsIT8Free(hIT8out);
  974. }
  975. // All is ok
  976. return 0;
  977. }