itufax.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Little cms
  3. // Copyright (C) 1998-2003 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. #include "lcms.h"
  23. // This is a sample on how to build a profile for decoding ITU T.42/Fax JPEG
  24. // streams. The profile has an additional ability in the input direction of
  25. // gamut compress values between 85 < a < -85 and -75 < b < 125. This conforms
  26. // the default range for ITU/T.42 -- See RFC 2301, section 6.2.3 for details
  27. // L* = [0, 100]
  28. // a* = [-85, 85]
  29. // b* = [-75, 125]
  30. // These functions does convert the encoding of ITUFAX to floating point
  31. static
  32. void ITU2Lab(WORD In[3], LPcmsCIELab Lab)
  33. {
  34. Lab -> L = (double) In[0] / 655.35;
  35. Lab -> a = (double) 170.* (In[1] - 32768.) / 65535.;
  36. Lab -> b = (double) 200.* (In[2] - 24576.) / 65535.;
  37. }
  38. static
  39. void Lab2ITU(LPcmsCIELab Lab, WORD Out[3])
  40. {
  41. Out[0] = (WORD) floor((double) (Lab -> L / 100.)* 65535. + 0.5);
  42. Out[1] = (WORD) floor((double) (Lab -> a / 170.)* 65535. + 32768. + 0.5);
  43. Out[2] = (WORD) floor((double) (Lab -> b / 200.)* 65535. + 24576. + 0.5);
  44. }
  45. // These are the samplers-- They are passed as callbacks to cmsSample3DGrid()
  46. // then, cmsSample3DGrid() will sweel whole Lab gamut calling these functions
  47. // once for each node. In[] will contain the Lab PCS value to convert to ITUFAX
  48. // on InputDirection, or the ITUFAX value to convert to Lab in OutputDirection
  49. // You can change the number of sample points if desired, the algorithm will
  50. // remain same. 33 points gives good accuracy, but you can reduce to 22 or less
  51. // is space is critical
  52. #define GRID_POINTS 33
  53. static
  54. int InputDirection(register WORD In[], register WORD Out[], register LPVOID Cargo)
  55. {
  56. cmsCIELab Lab;
  57. cmsLabEncoded2Float(&Lab, In);
  58. cmsClampLab(&Lab, 85, -85, 125, -75); // This function does the necessary gamut remapping
  59. Lab2ITU(&Lab, Out);
  60. return TRUE;
  61. }
  62. static
  63. int OutputDirection(register WORD In[], register WORD Out[], register LPVOID Cargo)
  64. {
  65. cmsCIELab Lab;
  66. ITU2Lab(In, &Lab);
  67. cmsFloat2LabEncoded(Out, &Lab);
  68. return TRUE;
  69. }
  70. // The main entry point. Just create a profile an populate it with required tags.
  71. // note that cmsOpenProfileFromFile("itufax.icm", "w") will NOT delete the file
  72. // if already exists. This is for obvious safety reasons.
  73. int main(int argc, char *argv[])
  74. {
  75. LPLUT AToB0, BToA0;
  76. cmsHPROFILE hProfile;
  77. fprintf(stderr, "Creating itufax.icm...");
  78. unlink("itufax.icm");
  79. hProfile = cmsOpenProfileFromFile("itufax.icm", "w");
  80. AToB0 = cmsAllocLUT();
  81. BToA0 = cmsAllocLUT();
  82. cmsAlloc3DGrid(AToB0, GRID_POINTS, 3, 3);
  83. cmsAlloc3DGrid(BToA0, GRID_POINTS, 3, 3);
  84. cmsSample3DGrid(AToB0, InputDirection, NULL, 0);
  85. cmsSample3DGrid(BToA0, OutputDirection, NULL, 0);
  86. cmsAddTag(hProfile, icSigAToB0Tag, AToB0);
  87. cmsAddTag(hProfile, icSigBToA0Tag, BToA0);
  88. cmsSetColorSpace(hProfile, icSigLabData);
  89. cmsSetPCS(hProfile, icSigLabData);
  90. cmsSetDeviceClass(hProfile, icSigColorSpaceClass);
  91. cmsAddTag(hProfile, icSigProfileDescriptionTag, "ITU T.42/Fax JPEG CIEL*a*b*");
  92. cmsAddTag(hProfile, icSigCopyrightTag, "No Copyright, use freely.");
  93. cmsAddTag(hProfile, icSigDeviceMfgDescTag, "Little cms");
  94. cmsAddTag(hProfile, icSigDeviceModelDescTag, "ITU T.42/Fax JPEG CIEL*a*b*");
  95. cmsCloseProfile(hProfile);
  96. cmsFreeLUT(AToB0);
  97. cmsFreeLUT(BToA0);
  98. fprintf(stderr, "Done.\n");
  99. return 0;
  100. }