vprf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. int Verbose = 0;
  28. static char ProgramName[256] = "";
  29. void FatalError(const char *frm, ...)
  30. {
  31. va_list args;
  32. va_start(args, frm);
  33. fprintf(stderr, "[%s fatal error]: ", ProgramName);
  34. vfprintf(stderr, frm, args);
  35. fprintf(stderr, "\n");
  36. va_end(args);
  37. exit(1);
  38. }
  39. // Show errors to the end user (unless quiet option)
  40. static
  41. void MyErrorLogHandler(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
  42. {
  43. if (Verbose >= 0)
  44. fprintf(stderr, "[%s]: %s\n", ProgramName, Text);
  45. UTILS_UNUSED_PARAMETER(ErrorCode);
  46. UTILS_UNUSED_PARAMETER(ContextID);
  47. }
  48. void InitUtils(const char* PName)
  49. {
  50. strncpy(ProgramName, PName, sizeof(ProgramName));
  51. ProgramName[sizeof(ProgramName)-1] = 0;
  52. cmsSetLogErrorHandler(MyErrorLogHandler);
  53. }
  54. // Virtual profiles are handled here.
  55. cmsHPROFILE OpenStockProfile(cmsContext ContextID, const char* File)
  56. {
  57. if (!File)
  58. return cmsCreate_sRGBProfileTHR(ContextID);
  59. if (cmsstrcasecmp(File, "*Lab2") == 0)
  60. return cmsCreateLab2ProfileTHR(ContextID, NULL);
  61. if (cmsstrcasecmp(File, "*Lab4") == 0)
  62. return cmsCreateLab4ProfileTHR(ContextID, NULL);
  63. if (cmsstrcasecmp(File, "*Lab") == 0)
  64. return cmsCreateLab4ProfileTHR(ContextID, NULL);
  65. if (cmsstrcasecmp(File, "*LabD65") == 0) {
  66. cmsCIExyY D65xyY;
  67. cmsWhitePointFromTemp( &D65xyY, 6504);
  68. return cmsCreateLab4ProfileTHR(ContextID, &D65xyY);
  69. }
  70. if (cmsstrcasecmp(File, "*XYZ") == 0)
  71. return cmsCreateXYZProfileTHR(ContextID);
  72. if (cmsstrcasecmp(File, "*Gray22") == 0) {
  73. cmsToneCurve* Curve = cmsBuildGamma(ContextID, 2.2);
  74. cmsHPROFILE hProfile = cmsCreateGrayProfileTHR(ContextID, cmsD50_xyY(), Curve);
  75. cmsFreeToneCurve(Curve);
  76. return hProfile;
  77. }
  78. if (cmsstrcasecmp(File, "*Gray30") == 0) {
  79. cmsToneCurve* Curve = cmsBuildGamma(ContextID, 3.0);
  80. cmsHPROFILE hProfile = cmsCreateGrayProfileTHR(ContextID, cmsD50_xyY(), Curve);
  81. cmsFreeToneCurve(Curve);
  82. return hProfile;
  83. }
  84. if (cmsstrcasecmp(File, "*srgb") == 0)
  85. return cmsCreate_sRGBProfileTHR(ContextID);
  86. if (cmsstrcasecmp(File, "*null") == 0)
  87. return cmsCreateNULLProfileTHR(ContextID);
  88. if (cmsstrcasecmp(File, "*Lin2222") == 0) {
  89. cmsToneCurve* Gamma = cmsBuildGamma(0, 2.2);
  90. cmsToneCurve* Gamma4[4];
  91. cmsHPROFILE hProfile;
  92. Gamma4[0] = Gamma4[1] = Gamma4[2] = Gamma4[3] = Gamma;
  93. hProfile = cmsCreateLinearizationDeviceLink(cmsSigCmykData, Gamma4);
  94. cmsFreeToneCurve(Gamma);
  95. return hProfile;
  96. }
  97. return cmsOpenProfileFromFileTHR(ContextID, File, "r");
  98. }
  99. // Help on available built-ins
  100. void PrintBuiltins(void)
  101. {
  102. fprintf(stderr, "\nBuilt-in profiles:\n\n");
  103. fprintf(stderr, "\t*Lab2 -- D50-based v2 CIEL*a*b\n"
  104. "\t*Lab4 -- D50-based v4 CIEL*a*b\n"
  105. "\t*Lab -- D50-based v4 CIEL*a*b\n"
  106. "\t*XYZ -- CIE XYZ (PCS)\n"
  107. "\t*sRGB -- sRGB color space\n"
  108. "\t*Gray22 - Monochrome of Gamma 2.2\n"
  109. "\t*Gray30 - Monochrome of Gamma 3.0\n"
  110. "\t*null - Monochrome black for all input\n"
  111. "\t*Lin2222- CMYK linearization of gamma 2.2 on each channel\n\n");
  112. }
  113. // Auxiliary for printing information on profile
  114. static
  115. void PrintInfo(cmsHPROFILE h, cmsInfoType Info)
  116. {
  117. char* text;
  118. int len;
  119. len = cmsGetProfileInfoASCII(h, Info, "en", "US", NULL, 0);
  120. if (len == 0) return;
  121. text = (char*) malloc(len * sizeof(char));
  122. if (text == NULL) return;
  123. cmsGetProfileInfoASCII(h, Info, "en", "US", text, len);
  124. if (strlen(text) > 0)
  125. printf("%s\n", text);
  126. free(text);
  127. }
  128. // Displays the colorant table
  129. static
  130. void PrintColorantTable(cmsHPROFILE hInput, cmsTagSignature Sig, const char* Title)
  131. {
  132. cmsNAMEDCOLORLIST* list;
  133. int i, n;
  134. if (cmsIsTag(hInput, Sig)) {
  135. printf("%s:\n", Title);
  136. list = (cmsNAMEDCOLORLIST*) cmsReadTag(hInput, Sig);
  137. if (list == NULL) {
  138. printf("(Unavailable)\n");
  139. return;
  140. }
  141. n = cmsNamedColorCount(list);
  142. for (i=0; i < n; i++) {
  143. char Name[cmsMAX_PATH];
  144. cmsNamedColorInfo(list, i, Name, NULL, NULL, NULL, NULL);
  145. printf("\t%s\n", Name);
  146. }
  147. printf("\n");
  148. }
  149. }
  150. void PrintProfileInformation(cmsHPROFILE hInput)
  151. {
  152. if (hInput == NULL) {
  153. fprintf(stderr, "*Wrong or corrupted profile*\n");
  154. return;
  155. }
  156. PrintInfo(hInput, cmsInfoDescription);
  157. PrintInfo(hInput, cmsInfoManufacturer);
  158. PrintInfo(hInput, cmsInfoModel);
  159. PrintInfo(hInput, cmsInfoCopyright);
  160. if (Verbose > 2) {
  161. PrintColorantTable(hInput, cmsSigColorantTableTag, "Input colorant table");
  162. PrintColorantTable(hInput, cmsSigColorantTableOutTag, "Input colorant out table");
  163. }
  164. printf("\n");
  165. }
  166. // -----------------------------------------------------------------------------
  167. void PrintRenderingIntents(void)
  168. {
  169. cmsUInt32Number Codes[200];
  170. char* Descriptions[200];
  171. cmsUInt32Number n, i;
  172. fprintf(stderr, "-t<n> rendering intent:\n\n");
  173. n = cmsGetSupportedIntents(200, Codes, Descriptions);
  174. for (i=0; i < n; i++) {
  175. fprintf(stderr, "\t%u - %s\n", Codes[i], Descriptions[i]);
  176. }
  177. fprintf(stderr, "\n");
  178. }
  179. // ------------------------------------------------------------------------------
  180. cmsBool SaveMemoryBlock(const cmsUInt8Number* Buffer, cmsUInt32Number dwLen, const char* Filename)
  181. {
  182. FILE* out = fopen(Filename, "wb");
  183. if (out == NULL) {
  184. FatalError("Cannot create '%s'", Filename);
  185. return FALSE;
  186. }
  187. if (fwrite(Buffer, 1, dwLen, out) != dwLen) {
  188. FatalError("Cannot write %ld bytes to %s", (long) dwLen, Filename);
  189. return FALSE;
  190. }
  191. if (fclose(out) != 0) {
  192. FatalError("Error flushing file '%s'", Filename);
  193. return FALSE;
  194. }
  195. return TRUE;
  196. }
  197. // ------------------------------------------------------------------------------
  198. // Return a pixel type on depending on the number of channels
  199. int PixelTypeFromChanCount(int ColorChannels)
  200. {
  201. switch (ColorChannels) {
  202. case 1: return PT_GRAY;
  203. case 2: return PT_MCH2;
  204. case 3: return PT_MCH3;
  205. case 4: return PT_CMYK;
  206. case 5: return PT_MCH5;
  207. case 6: return PT_MCH6;
  208. case 7: return PT_MCH7;
  209. case 8: return PT_MCH8;
  210. case 9: return PT_MCH9;
  211. case 10: return PT_MCH10;
  212. case 11: return PT_MCH11;
  213. case 12: return PT_MCH12;
  214. case 13: return PT_MCH13;
  215. case 14: return PT_MCH14;
  216. case 15: return PT_MCH15;
  217. default:
  218. FatalError("What a weird separation of %d channels?!?!", ColorChannels);
  219. return -1;
  220. }
  221. }
  222. // ------------------------------------------------------------------------------
  223. // Return number of channels of pixel type
  224. int ChanCountFromPixelType(int ColorChannels)
  225. {
  226. switch (ColorChannels) {
  227. case PT_GRAY: return 1;
  228. case PT_RGB:
  229. case PT_CMY:
  230. case PT_Lab:
  231. case PT_YUV:
  232. case PT_YCbCr: return 3;
  233. case PT_CMYK: return 4 ;
  234. case PT_MCH2: return 2 ;
  235. case PT_MCH3: return 3 ;
  236. case PT_MCH4: return 4 ;
  237. case PT_MCH5: return 5 ;
  238. case PT_MCH6: return 6 ;
  239. case PT_MCH7: return 7 ;
  240. case PT_MCH8: return 8 ;
  241. case PT_MCH9: return 9 ;
  242. case PT_MCH10: return 10;
  243. case PT_MCH11: return 11;
  244. case PT_MCH12: return 12;
  245. case PT_MCH13: return 12;
  246. case PT_MCH14: return 14;
  247. case PT_MCH15: return 15;
  248. default:
  249. FatalError("Unsupported color space of %d channels", ColorChannels);
  250. return -1;
  251. }
  252. }