mktiff8.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Little cms
  3. // Copyright (C) 1998-2010 Marti Maria
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the Software
  10. // is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  17. // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. // Creates a devicelink that decodes TIFF8 Lab files
  23. #include "lcms2.h"
  24. #include <stdlib.h>
  25. #include <math.h>
  26. static
  27. double DecodeAbTIFF(double ab)
  28. {
  29. if (ab <= 128.)
  30. ab += 127.;
  31. else
  32. ab -= 127.;
  33. return ab;
  34. }
  35. static
  36. cmsToneCurve* CreateStep(void)
  37. {
  38. cmsToneCurve* Gamma;
  39. cmsUInt16Number* Table;
  40. int i;
  41. double a;
  42. Table = calloc(4096, sizeof(cmsUInt16Number));
  43. if (Table == NULL) return NULL;
  44. for (i=0; i < 4096; i++) {
  45. a = (double) i * 255. / 4095.;
  46. a = DecodeAbTIFF(a);
  47. Table[i] = (cmsUInt16Number) floor(a * 257. + 0.5);
  48. }
  49. Gamma = cmsBuildTabulatedToneCurve16(0, 4096, Table);
  50. free(Table);
  51. return Gamma;
  52. }
  53. static
  54. cmsToneCurve* CreateLinear(void)
  55. {
  56. cmsUInt16Number Linear[2] = { 0, 0xffff };
  57. return cmsBuildTabulatedToneCurve16(0, 2, Linear);
  58. }
  59. // Set the copyright and description
  60. static
  61. cmsBool SetTextTags(cmsHPROFILE hProfile)
  62. {
  63. cmsMLU *DescriptionMLU, *CopyrightMLU;
  64. cmsBool rc = FALSE;
  65. DescriptionMLU = cmsMLUalloc(0, 1);
  66. CopyrightMLU = cmsMLUalloc(0, 1);
  67. if (DescriptionMLU == NULL || CopyrightMLU == NULL) goto Error;
  68. if (!cmsMLUsetASCII(DescriptionMLU, "en", "US", "Little cms Tiff8 CIELab")) goto Error;
  69. if (!cmsMLUsetASCII(CopyrightMLU, "en", "US", "Copyright (c) Marti Maria, 2010. All rights reserved.")) goto Error;
  70. if (!cmsWriteTag(hProfile, cmsSigProfileDescriptionTag, DescriptionMLU)) goto Error;
  71. if (!cmsWriteTag(hProfile, cmsSigCopyrightTag, CopyrightMLU)) goto Error;
  72. rc = TRUE;
  73. Error:
  74. if (DescriptionMLU)
  75. cmsMLUfree(DescriptionMLU);
  76. if (CopyrightMLU)
  77. cmsMLUfree(CopyrightMLU);
  78. return rc;
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. cmsHPROFILE hProfile;
  83. cmsPipeline *AToB0;
  84. cmsToneCurve* PreLinear[3];
  85. cmsToneCurve *Lin, *Step;
  86. fprintf(stderr, "Creating lcmstiff8.icm...");
  87. remove("lcmstiff8.icm");
  88. hProfile = cmsOpenProfileFromFile("lcmstiff8.icm", "w");
  89. // Create linearization
  90. Lin = CreateLinear();
  91. Step = CreateStep();
  92. PreLinear[0] = Lin;
  93. PreLinear[1] = Step;
  94. PreLinear[2] = Step;
  95. AToB0 = cmsPipelineAlloc(0, 3, 3);
  96. cmsPipelineInsertStage(AToB0,
  97. cmsAT_BEGIN, cmsStageAllocToneCurves(0, 3, PreLinear));
  98. cmsSetColorSpace(hProfile, cmsSigLabData);
  99. cmsSetPCS(hProfile, cmsSigLabData);
  100. cmsSetDeviceClass(hProfile, cmsSigLinkClass);
  101. cmsSetProfileVersion(hProfile, 4.2);
  102. cmsWriteTag(hProfile, cmsSigAToB0Tag, AToB0);
  103. SetTextTags(hProfile);
  104. cmsCloseProfile(hProfile);
  105. cmsFreeToneCurve(Lin);
  106. cmsFreeToneCurve(Step);
  107. cmsPipelineFree(AToB0);
  108. fprintf(stderr, "Done.\n");
  109. return 0;
  110. }