psicc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include "utils.h"
  26. // ------------------------------------------------------------------------
  27. static char *cInProf = NULL;
  28. static char *cOutProf = NULL;
  29. static int Intent = INTENT_PERCEPTUAL;
  30. static FILE* OutFile;
  31. static int BlackPointCompensation = FALSE;
  32. static int Undecorated = FALSE;
  33. static int PrecalcMode = 1;
  34. static int NumOfGridPoints = 0;
  35. static
  36. void Help(void)
  37. {
  38. fprintf(stderr, "usage: psicc [flags] [<Output file>]\n\n");
  39. fprintf(stderr, "flags:\n\n");
  40. fprintf(stderr, "-i<profile> - Input profile: Generates Color Space Array (CSA)\n");
  41. fprintf(stderr, "-o<profile> - Output profile: Generates Color Rendering Dictionary(CRD)\n");
  42. fprintf(stderr, "-t<0,1,2,3> - Intent (0=Perceptual, 1=Colorimetric, 2=Saturation, 3=Absolute)\n");
  43. fprintf(stderr, "-b - Black point compensation (CRD only)\n");
  44. fprintf(stderr, "-u - Do NOT generate resource name on CRD\n");
  45. fprintf(stderr, "-c<0,1,2> - Precision (0=LowRes, 1=Normal (default), 2=Hi-res) (CRD only)\n");
  46. fprintf(stderr, "-n<gridpoints> - Alternate way to set precision, number of CLUT points (CRD only)\n");
  47. fprintf(stderr, "\n");
  48. fprintf(stderr, "If no output file is specified, output goes to stdout.\n\n");
  49. fprintf(stderr, "This program is intended to be a demo of the little cms\n"
  50. "engine. Both lcms and this program are freeware. You can\n"
  51. "obtain both in source code at https://www.littlecms.com\n"
  52. "For suggestions, comments, bug reports etc. send mail to\n"
  53. "info@littlecms.com\n\n");
  54. exit(0);
  55. }
  56. // The toggles stuff
  57. static
  58. void HandleSwitches(int argc, char *argv[])
  59. {
  60. int s;
  61. while ((s = xgetopt(argc,argv,"uUbBI:i:O:o:T:t:c:C:n:N:-:")) != EOF) {
  62. switch (s)
  63. {
  64. case '-':
  65. if (strcmp(xoptarg, "help") == 0)
  66. {
  67. Help();
  68. }
  69. else
  70. {
  71. FatalError("Unknown option - run without args to see valid ones.\n");
  72. }
  73. break;
  74. case 'i':
  75. case 'I':
  76. cInProf = xoptarg;
  77. break;
  78. case 'o':
  79. case 'O':
  80. cOutProf = xoptarg;
  81. break;
  82. case 'b':
  83. case 'B': BlackPointCompensation =TRUE;
  84. break;
  85. case 't':
  86. case 'T':
  87. Intent = atoi(xoptarg);
  88. if (Intent > 3) Intent = 3;
  89. if (Intent < 0) Intent = 0;
  90. break;
  91. case 'U':
  92. case 'u':
  93. Undecorated = TRUE;
  94. break;
  95. case 'c':
  96. case 'C':
  97. PrecalcMode = atoi(xoptarg);
  98. if (PrecalcMode < 0 || PrecalcMode > 2)
  99. FatalError("ERROR: Unknown precalc mode '%d'", PrecalcMode);
  100. break;
  101. case 'n':
  102. case 'N':
  103. if (PrecalcMode != 1)
  104. FatalError("Precalc mode already specified");
  105. NumOfGridPoints = atoi(xoptarg);
  106. break;
  107. default:
  108. FatalError("Unknown option - run without args to see valid ones.\n");
  109. }
  110. }
  111. }
  112. static
  113. void GenerateCSA(void)
  114. {
  115. cmsHPROFILE hProfile = OpenStockProfile(0, cInProf);
  116. size_t n;
  117. char* Buffer;
  118. if (hProfile == NULL) return;
  119. n = cmsGetPostScriptCSA(0, hProfile, Intent, 0, NULL, 0);
  120. if (n == 0) return;
  121. Buffer = (char*) malloc(n + 1);
  122. if (Buffer != NULL) {
  123. cmsGetPostScriptCSA(0, hProfile, Intent, 0, Buffer, (cmsUInt32Number) n);
  124. Buffer[n] = 0;
  125. fprintf(OutFile, "%s", Buffer);
  126. free(Buffer);
  127. }
  128. cmsCloseProfile(hProfile);
  129. }
  130. static
  131. void GenerateCRD(void)
  132. {
  133. cmsHPROFILE hProfile = OpenStockProfile(0, cOutProf);
  134. size_t n;
  135. char* Buffer;
  136. cmsUInt32Number dwFlags = 0;
  137. if (hProfile == NULL) return;
  138. if (BlackPointCompensation) dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
  139. if (Undecorated) dwFlags |= cmsFLAGS_NODEFAULTRESOURCEDEF;
  140. switch (PrecalcMode) {
  141. case 0: dwFlags |= cmsFLAGS_LOWRESPRECALC; break;
  142. case 2: dwFlags |= cmsFLAGS_HIGHRESPRECALC; break;
  143. case 1:
  144. if (NumOfGridPoints > 0)
  145. dwFlags |= cmsFLAGS_GRIDPOINTS(NumOfGridPoints);
  146. break;
  147. default: FatalError("ERROR: Unknown precalculation mode '%d'", PrecalcMode);
  148. }
  149. n = cmsGetPostScriptCRD(0, hProfile, Intent, dwFlags, NULL, 0);
  150. if (n == 0) return;
  151. Buffer = (char*) malloc(n + 1);
  152. if (Buffer == NULL) return;
  153. cmsGetPostScriptCRD(0, hProfile, Intent, dwFlags, Buffer, (cmsUInt32Number) n);
  154. Buffer[n] = 0;
  155. fprintf(OutFile, "%s", Buffer);
  156. free(Buffer);
  157. cmsCloseProfile(hProfile);
  158. }
  159. int main(int argc, char *argv[])
  160. {
  161. int nargs;
  162. fprintf(stderr, "Little CMS ICC PostScript generator - v2.1 [LittleCMS %2.2f]\n", cmsGetEncodedCMMversion() / 1000.0);
  163. fprintf(stderr, "Copyright (c) 1998-2023 Marti Maria Saguer. See COPYING file for details.\n");
  164. fflush(stderr);
  165. // Initialize
  166. InitUtils("psicc");
  167. HandleSwitches(argc, argv);
  168. nargs = (argc - xoptind);
  169. if (nargs != 0 && nargs != 1)
  170. Help();
  171. if (cInProf == NULL && cOutProf == NULL)
  172. Help();
  173. if (nargs == 0)
  174. OutFile = stdout;
  175. else
  176. OutFile = fopen(argv[xoptind], "wt");
  177. if (cInProf != NULL)
  178. GenerateCSA();
  179. if (cOutProf != NULL)
  180. GenerateCRD();
  181. if (nargs == 1) {
  182. fclose(OutFile);
  183. }
  184. return 0;
  185. }