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