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 
23 // Creates a devicelink that decodes TIFF8 Lab files
24 
25 #include "lcms2mt.h"
26 #include <stdlib.h>
27 #include <math.h>
28 
29 static
DecodeAbTIFF(double ab)30 double DecodeAbTIFF(double ab)
31 {
32 	if (ab <= 128.)
33 		ab += 127.;
34 	else
35 		ab -= 127.;
36 
37 	return ab;
38 }
39 
40 static
CreateStep(void)41 cmsToneCurve* CreateStep(void)
42 {
43 	cmsToneCurve* Gamma;
44 	cmsUInt16Number* Table;
45 	int i;
46 	double a;
47 
48 	Table = calloc(4096, sizeof(cmsUInt16Number));
49 	if (Table == NULL) return NULL;
50 
51 	for (i=0; i < 4096; i++) {
52 
53 		a = (double) i * 255. / 4095.;
54 
55 		a = DecodeAbTIFF(a);
56 
57 		Table[i] = (cmsUInt16Number) floor(a * 257. + 0.5);
58 	}
59 
60 	Gamma = cmsBuildTabulatedToneCurve16(0, 4096, Table);
61 	free(Table);
62 
63 	return Gamma;
64 }
65 
66 
67 static
CreateLinear(void)68 cmsToneCurve* CreateLinear(void)
69 {
70 	cmsUInt16Number Linear[2] = { 0, 0xffff };
71 
72 	return cmsBuildTabulatedToneCurve16(0, 2, Linear);
73 }
74 
75 
76 
77 // Set the copyright and description
78 static
SetTextTags(cmsHPROFILE hProfile)79 cmsBool SetTextTags(cmsHPROFILE hProfile)
80 {
81     cmsMLU *DescriptionMLU, *CopyrightMLU;
82     cmsBool  rc = FALSE;
83 
84     DescriptionMLU  = cmsMLUalloc(0, 1);
85     CopyrightMLU    = cmsMLUalloc(0, 1);
86 
87     if (DescriptionMLU == NULL || CopyrightMLU == NULL) goto Error;
88 
89     if (!cmsMLUsetASCII(DescriptionMLU,  "en", "US", "Little cms Tiff8 CIELab")) goto Error;
90     if (!cmsMLUsetASCII(CopyrightMLU,    "en", "US", "Copyright (c) Marti Maria, 2010. All rights reserved.")) goto Error;
91 
92     if (!cmsWriteTag(hProfile, cmsSigProfileDescriptionTag,  DescriptionMLU)) goto Error;
93     if (!cmsWriteTag(hProfile, cmsSigCopyrightTag,           CopyrightMLU)) goto Error;
94 
95     rc = TRUE;
96 
97 Error:
98 
99     if (DescriptionMLU)
100         cmsMLUfree(DescriptionMLU);
101     if (CopyrightMLU)
102         cmsMLUfree(CopyrightMLU);
103     return rc;
104 }
105 
106 
main(int argc,char * argv[])107 int main(int argc, char *argv[])
108 {
109 	cmsHPROFILE hProfile;
110 	cmsPipeline *AToB0;
111 	cmsToneCurve* PreLinear[3];
112 	cmsToneCurve *Lin, *Step;
113 
114 	fprintf(stderr, "Creating lcmstiff8.icm...");
115 
116     remove("lcmstiff8.icm");
117 	hProfile = cmsOpenProfileFromFile("lcmstiff8.icm", "w");
118 
119 	// Create linearization
120 	Lin  = CreateLinear();
121 	Step = CreateStep();
122 
123 	PreLinear[0] = Lin;
124 	PreLinear[1] = Step;
125 	PreLinear[2] = Step;
126 
127     AToB0 = cmsPipelineAlloc(0, 3, 3);
128 
129 	cmsPipelineInsertStage(AToB0,
130 		cmsAT_BEGIN, cmsStageAllocToneCurves(0, 3, PreLinear));
131 
132 	cmsSetColorSpace(hProfile, cmsSigLabData);
133 	cmsSetPCS(hProfile, cmsSigLabData);
134 	cmsSetDeviceClass(hProfile, cmsSigLinkClass);
135 	cmsSetProfileVersion(hProfile, 4.2);
136 
137     cmsWriteTag(hProfile, cmsSigAToB0Tag, AToB0);
138 
139     SetTextTags(hProfile);
140 
141 	cmsCloseProfile(hProfile);
142 
143 	cmsFreeToneCurve(Lin);
144 	cmsFreeToneCurve(Step);
145 	cmsPipelineFree(AToB0);
146 
147 	fprintf(stderr, "Done.\n");
148 
149 	return 0;
150 }
151