1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
//---------------------------------------------------------------------------------
//
// Little Color Management System
// Copyright (c) 1998-2020 Marti Maria Saguer
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//---------------------------------------------------------------------------------
//
#include "utils.h"
int Verbose = 0;
static char ProgramName[256] = "";
void FatalError(const char *frm, ...)
{
va_list args;
va_start(args, frm);
fprintf(stderr, "[%s fatal error]: ", ProgramName);
vfprintf(stderr, frm, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
// Show errors to the end user (unless quiet option)
static
void MyErrorLogHandler(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
{
if (Verbose >= 0)
fprintf(stderr, "[%s]: %s\n", ProgramName, Text);
UTILS_UNUSED_PARAMETER(ErrorCode);
UTILS_UNUSED_PARAMETER(ContextID);
}
void InitUtils(cmsContext ContextID, const char* PName)
{
strncpy(ProgramName, PName, sizeof(ProgramName));
ProgramName[sizeof(ProgramName)-1] = 0;
cmsSetLogErrorHandler(ContextID, MyErrorLogHandler);
}
// Virtual profiles are handled here.
cmsHPROFILE OpenStockProfile(cmsContext ContextID, const char* File)
{
if (!File)
return cmsCreate_sRGBProfile(ContextID);
if (cmsstrcasecmp(File, "*Lab2") == 0)
return cmsCreateLab2Profile(ContextID, NULL);
if (cmsstrcasecmp(File, "*Lab4") == 0)
return cmsCreateLab4Profile(ContextID, NULL);
if (cmsstrcasecmp(File, "*Lab") == 0)
return cmsCreateLab4Profile(ContextID, NULL);
if (cmsstrcasecmp(File, "*LabD65") == 0) {
cmsCIExyY D65xyY;
cmsWhitePointFromTemp(ContextID, &D65xyY, 6504);
return cmsCreateLab4Profile(ContextID, &D65xyY);
}
if (cmsstrcasecmp(File, "*XYZ") == 0)
return cmsCreateXYZProfile(ContextID);
if (cmsstrcasecmp(File, "*Gray22") == 0) {
cmsToneCurve* Curve = cmsBuildGamma(ContextID, 2.2);
cmsHPROFILE hProfile = cmsCreateGrayProfile(ContextID, cmsD50_xyY(ContextID), Curve);
cmsFreeToneCurve(ContextID, Curve);
return hProfile;
}
if (cmsstrcasecmp(File, "*Gray30") == 0) {
cmsToneCurve* Curve = cmsBuildGamma(ContextID, 3.0);
cmsHPROFILE hProfile = cmsCreateGrayProfile(ContextID, cmsD50_xyY(ContextID), Curve);
cmsFreeToneCurve(ContextID, Curve);
return hProfile;
}
if (cmsstrcasecmp(File, "*srgb") == 0)
return cmsCreate_sRGBProfile(ContextID);
if (cmsstrcasecmp(File, "*null") == 0)
return cmsCreateNULLProfile(ContextID);
if (cmsstrcasecmp(File, "*Lin2222") == 0) {
cmsToneCurve* Gamma = cmsBuildGamma(0, 2.2);
cmsToneCurve* Gamma4[4];
cmsHPROFILE hProfile;
Gamma4[0] = Gamma4[1] = Gamma4[2] = Gamma4[3] = Gamma;
hProfile = cmsCreateLinearizationDeviceLink(ContextID, cmsSigCmykData, Gamma4);
cmsFreeToneCurve(ContextID, Gamma);
return hProfile;
}
return cmsOpenProfileFromFile(ContextID, File, "r");
}
// Help on available built-ins
void PrintBuiltins(void)
{
fprintf(stderr, "\nBuilt-in profiles:\n\n");
fprintf(stderr, "\t*Lab2 -- D50-based v2 CIEL*a*b\n"
"\t*Lab4 -- D50-based v4 CIEL*a*b\n"
"\t*Lab -- D50-based v4 CIEL*a*b\n"
"\t*XYZ -- CIE XYZ (PCS)\n"
"\t*sRGB -- sRGB color space\n"
"\t*Gray22 - Monochrome of Gamma 2.2\n"
"\t*Gray30 - Monochrome of Gamma 3.0\n"
"\t*null - Monochrome black for all input\n"
"\t*Lin2222- CMYK linearization of gamma 2.2 on each channel\n");
}
// Auxiliary for printing information on profile
static
void PrintInfo(cmsContext ContextID, cmsHPROFILE h, cmsInfoType Info)
{
char* text;
int len;
len = cmsGetProfileInfoASCII(ContextID, h, Info, "en", "US", NULL, 0);
if (len == 0) return;
text = (char*) malloc(len * sizeof(char));
if (text == NULL) return;
cmsGetProfileInfoASCII(ContextID, h, Info, "en", "US", text, len);
if (strlen(text) > 0)
printf("%s\n", text);
free(text);
}
// Displays the colorant table
static
void PrintColorantTable(cmsContext ContextID, cmsHPROFILE hInput, cmsTagSignature Sig, const char* Title)
{
cmsNAMEDCOLORLIST* list;
int i, n;
if (cmsIsTag(ContextID, hInput, Sig)) {
printf("%s:\n", Title);
list = (cmsNAMEDCOLORLIST*) cmsReadTag(ContextID, hInput, Sig);
if (list == NULL) {
printf("(Unavailable)\n");
return;
}
n = cmsNamedColorCount(ContextID, list);
for (i=0; i < n; i++) {
char Name[cmsMAX_PATH];
cmsNamedColorInfo(ContextID, list, i, Name, NULL, NULL, NULL, NULL);
printf("\t%s\n", Name);
}
printf("\n");
}
}
void PrintProfileInformation(cmsContext ContextID, cmsHPROFILE hInput)
{
if (hInput == NULL) {
fprintf(stderr, "*Wrong or corrupted profile*\n");
return;
}
PrintInfo(ContextID, hInput, cmsInfoDescription);
PrintInfo(ContextID, hInput, cmsInfoManufacturer);
PrintInfo(ContextID, hInput, cmsInfoModel);
PrintInfo(ContextID, hInput, cmsInfoCopyright);
if (Verbose > 2) {
PrintColorantTable(ContextID, hInput, cmsSigColorantTableTag, "Input colorant table");
PrintColorantTable(ContextID, hInput, cmsSigColorantTableOutTag, "Input colorant out table");
}
printf("\n");
}
// -----------------------------------------------------------------------------
void PrintRenderingIntents(cmsContext ContextID)
{
cmsUInt32Number Codes[200];
char* Descriptions[200];
cmsUInt32Number n, i;
fprintf(stderr, "%ct<n> rendering intent:\n\n", SW);
n = cmsGetSupportedIntents(ContextID, 200, Codes, Descriptions);
for (i=0; i < n; i++) {
fprintf(stderr, "\t%u - %s\n", Codes[i], Descriptions[i]);
}
fprintf(stderr, "\n");
}
// ------------------------------------------------------------------------------
cmsBool SaveMemoryBlock(const cmsUInt8Number* Buffer, cmsUInt32Number dwLen, const char* Filename)
{
FILE* out = fopen(Filename, "wb");
if (out == NULL) {
FatalError("Cannot create '%s'", Filename);
return FALSE;
}
if (fwrite(Buffer, 1, dwLen, out) != dwLen) {
FatalError("Cannot write %ld bytes to %s", dwLen, Filename);
return FALSE;
}
if (fclose(out) != 0) {
FatalError("Error flushing file '%s'", Filename);
return FALSE;
}
return TRUE;
}
// ------------------------------------------------------------------------------
// Return a pixel type on depending on the number of channels
int PixelTypeFromChanCount(int ColorChannels)
{
switch (ColorChannels) {
case 1: return PT_GRAY;
case 2: return PT_MCH2;
case 3: return PT_MCH3;
case 4: return PT_CMYK;
case 5: return PT_MCH5;
case 6: return PT_MCH6;
case 7: return PT_MCH7;
case 8: return PT_MCH8;
case 9: return PT_MCH9;
case 10: return PT_MCH10;
case 11: return PT_MCH11;
case 12: return PT_MCH12;
case 13: return PT_MCH13;
case 14: return PT_MCH14;
case 15: return PT_MCH15;
default:
FatalError("What a weird separation of %d channels?!?!", ColorChannels);
return -1;
}
}
// ------------------------------------------------------------------------------
// Return number of channels of pixel type
int ChanCountFromPixelType(int ColorChannels)
{
switch (ColorChannels) {
case PT_GRAY: return 1;
case PT_RGB:
case PT_CMY:
case PT_Lab:
case PT_YUV:
case PT_YCbCr: return 3;
case PT_CMYK: return 4 ;
case PT_MCH2: return 2 ;
case PT_MCH3: return 3 ;
case PT_MCH4: return 4 ;
case PT_MCH5: return 5 ;
case PT_MCH6: return 6 ;
case PT_MCH7: return 7 ;
case PT_MCH8: return 8 ;
case PT_MCH9: return 9 ;
case PT_MCH10: return 10;
case PT_MCH11: return 11;
case PT_MCH12: return 12;
case PT_MCH13: return 12;
case PT_MCH14: return 14;
case PT_MCH15: return 15;
default:
FatalError("Unsupported color space of %d channels", ColorChannels);
return -1;
}
}
|