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