1 /*************************************************************************
2  *
3  *  OpenOffice.org - a multi-platform office productivity suite
4  *
5 
6   Derived by beppec56@openoffice.org from various examples
7   in SampleICC library, the original copyright retained.
8 
9   Copyright:  � see below
10 */
11 
12 /*
13  * The ICC Software License, Version 0.1
14  *
15  *
16  * Copyright (c) 2003-2006 The International Color Consortium. All rights
17  * reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  *
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *
31  * 3. The end-user documentation included with the redistribution,
32  *    if any, must include the following acknowledgment:
33  *       "This product includes software developed by the
34  *        The International Color Consortium (www.color.org)"
35  *    Alternately, this acknowledgment may appear in the software itself,
36  *    if and wherever such third-party acknowledgments normally appear.
37  *
38  * 4. The names "ICC" and "The International Color Consortium" must
39  *    not be used to imply that the ICC organization endorses or
40  *    promotes products derived from this software without prior
41  *    written permission. For written permission, please see
42  *    <http://www.color.org/>.
43  *
44  *
45  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNATIONAL COLOR CONSORTIUM OR
49  * ITS CONTRIBUTING MEMBERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  * ====================================================================
58  *
59  * This software consists of voluntary contributions made by many
60  * individuals on behalf of the The International Color Consortium.
61  *
62  *
63  * Membership in the ICC is encouraged when this software is used for
64  * commercial purposes.
65  *
66  *
67  * For more information on The International Color Consortium, please
68  * see <http://www.color.org/>.
69  *
70  *
71  */
72 
73 #include <math.h>
74 #include <iostream>
75 #include <fstream>
76 using namespace std;
77 
78 #include "IccUtil.h"
79 #include "IccProfile.h"
80 
81 #include "Vetters.h"
82 #include "CAT.h"
83 #include "CLUT.h"
84 
85 const char * const icc_file_name = "sRGB-IEC61966-2.1.icc";
86 const char * const hxx_file_name = "sRGB-IEC61966-2.1.hxx";
87 const char * const this_file_name_and_location =" * icc/source/create_sRGB_profile/create_sRGB_profile.cpp";
88 
89 const char* const description = "sRGB IEC61966-2.1";
90 //const char* const devicemanufact = "IEC http://www.iec.ch"; not used, device manufactured by OOo seems funny...
91 const char* const devicemodel = "IEC 61966-2.1 Default RGB colour space - sRGB";
92 const char* const copyright = "The Contents of this file are made available subject to the terms of GNU Lesser General Public License version 3";
93 
94 // the creation date is fixed, corresponds to the last time this file has been changed
95 // NOTE: change this date values whenever the data inside the profile are changed.
96 const int  data_last_changed_year =     2007;
97 const int  data_last_changed_month =    12;
98 const int  data_last_day =              12;
99 const int  data_last_changed_hour =     18;
100 const int  data_last_changed_minute =   32;
101 
102 // the following string array it's the standard OOo header format
103 const char * const TheHeader1[] =
104 {
105     "/*************************************************************************",
106     " *",
107     " *  OpenOffice.org - a multi-platform office productivity suite",
108     " *",
109     " *  sRGB-IEC61966-2.1.hxx",
110     " *",
111     " *  creator: create_sRGB_profile",
112     NULL
113 };
114 
115 const char * const TheHeader2[] =
116 {
117     " *",
118     " * Copyright 2011 Apache OpenOffice.org.",
119     " *",
120     " * Licensed under the Apache License, Version 2.0 (the \"License\");",
121     " * you may not use this file except in compliance with the License.",
122     " * You may obtain a copy of the License at",
123     " *",
124     " *     http://www.apache.org/licenses/LICENSE-2.0",
125     " *",
126     " * Unless required by applicable law or agreed to in writing, software",
127     " * distributed under the License is distributed on an \"AS IS\" BASIS,",
128     " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.",
129     " * See the License for the specific language governing permissions and",
130     " * limitations under the License.",
131     " *",
132     " ************************************************************************/",
133     "",
134     "#ifndef INCLUDED_ICC_SRGB_IEC61966_2_1_H",
135     "#define INCLUDED_ICC_SRGB_IEC61966_2_1_H",
136     "",
137     "/***********************************************************************",
138     " * NOTE:",
139     " * this file is automatically generated by running the program",
140     " * obtained building:",
141     this_file_name_and_location,
142     " * contained in module icc",
143     " * modify that program if you need to change something.",
144     " ***********************************************************************/",
145     NULL // last string, a null
146 };
147 
148 const char * const TheTail[] =
149 {
150     "#endif /* INCLUDED_ICC_SRGB_IEC61966_2_1_H */",
151     NULL
152 };
153 
154 icFloatNumber computeIEC_RGBtoXYZ( icFloatNumber indata )
155 {
156     double retval = 0.0;
157     if(indata < 0.04045)
158         retval = indata/12.92;
159     else // apply the other conversion
160         retval = pow( (indata + 0.055)/1.055 , 2.4);
161 
162     return retval;
163 }
164 
165 icFloatNumber computeIEC_XYZtoRGB( icFloatNumber indata )
166 {
167     icFloatNumber retval = 0.0;
168     if(indata < 0.0031308)
169         retval = indata*12.92;
170     else // apply the other conversion
171         retval =  1.055*pow( indata , icFloatNumber(1.0/2.4)) - 0.055;
172 
173     //  cout << retval << endl;
174     return retval;
175 }
176 
177 void dumpTag(FILE *outfile, CIccProfile *pIcc, icTagSignature sig)
178 {
179     CIccTag *pTag = pIcc->FindTag(sig);
180     char buf[64];
181     CIccInfo Fmt;
182 
183     std::string contents;
184 
185     if (pTag)
186     {
187         fprintf(outfile, "\nContents of %s tag (%s)\n", Fmt.GetTagSigName(sig), icGetSig(buf, sig));
188         fprintf(outfile,"Type:   ");
189 
190         if (pTag->IsArrayType())  fprintf(outfile, "Array of ");
191 
192         fprintf(outfile, "%s\n", Fmt.GetTagTypeSigName(pTag->GetType()));
193         pTag->Describe(contents);
194         fwrite(contents.c_str(), contents.length(), 1, outfile);
195     }
196     else
197         fprintf(outfile, "Tag (%s) not found in profile\n", icGetSig(buf, sig));
198 }
199 
200 void dumpProfile(FILE *outfile, const char * profileName)
201 {
202     CIccProfile *pIcc;
203     std::string sReport;
204     icValidateStatus nStatus;
205 
206     pIcc = OpenIccProfile(profileName);
207 
208     if (!pIcc)
209         printf("Unable to open '%s'\n", profileName);
210     else
211     {
212         icHeader *pHdr = &pIcc->m_Header;
213         CIccInfo Fmt;
214         char buf[64];
215 
216         fprintf(outfile,"Profile:          '%s'\n", profileName);
217         if(Fmt.IsProfileIDCalculated(&pHdr->profileID))
218             fprintf(outfile,"Profile ID:        %s\n", Fmt.GetProfileID(&pHdr->profileID));
219         else
220             fprintf(outfile,"Profile ID:       Profile ID not calculated.\n");
221         fprintf(outfile,"Size:             %ld(0x%lx) bytes\n", pHdr->size, pHdr->size);
222 
223         fprintf(outfile,"\nHeader\n");
224         fprintf(outfile,"------\n");
225         fprintf(outfile,"Attributes:       %s\n", Fmt.GetDeviceAttrName(pHdr->attributes));
226         fprintf(outfile,"Cmm:              %s\n", Fmt.GetCmmSigName((icCmmSignature)(pHdr->cmmId)));
227         fprintf(outfile,"Creation Date:    %d/%d/%d  %02u:%02u:%02u\n",
228                 pHdr->date.month, pHdr->date.day, pHdr->date.year,
229                 pHdr->date.hours, pHdr->date.minutes, pHdr->date.seconds);
230         fprintf(outfile,"Creator:          %s\n", icGetSig(buf, pHdr->creator));
231         fprintf(outfile,"Data Color Space: %s\n", Fmt.GetColorSpaceSigName(pHdr->colorSpace));
232         fprintf(outfile,"Flags             %s\n", Fmt.GetProfileFlagsName(pHdr->flags));
233         fprintf(outfile,"PCS Color Space:  %s\n", Fmt.GetColorSpaceSigName(pHdr->pcs));
234         fprintf(outfile,"Platform:         %s\n", Fmt.GetPlatformSigName(pHdr->platform));
235         fprintf(outfile,"Rendering Intent: %s\n", Fmt.GetRenderingIntentName((icRenderingIntent)(pHdr->renderingIntent)));
236         fprintf(outfile,"Type:             %s\n", Fmt.GetProfileClassSigName(pHdr->deviceClass));
237         fprintf(outfile,"Version:          %s\n", Fmt.GetVersionName(pHdr->version));
238         fprintf(outfile,"Illuminant:       X=%.4lf, Y=%.4lf, Z=%.4lf\n",
239                 icFtoD(pHdr->illuminant.X),
240                 icFtoD(pHdr->illuminant.Y),
241                 icFtoD(pHdr->illuminant.Z));
242 
243         fprintf(outfile,"\nProfile Tags\n");
244         fprintf(outfile,"------------\n");
245 
246         fprintf(outfile,"%25s    ID    %8s\t%8s\n", "Tag", "Offset", "Size");
247         fprintf(outfile,"%25s  ------  %8s\t%8s\n", "----", "------", "----");
248 
249         int n;
250         TagEntryList::iterator i;
251 
252         for (n=0, i=pIcc->m_Tags->begin(); i!=pIcc->m_Tags->end(); i++, n++)
253         {
254             fprintf(outfile,"%25s  %s  %8ld\t%8ld\n", Fmt.GetTagSigName(i->TagInfo.sig),
255                     icGetSig(buf, i->TagInfo.sig, false),
256                     i->TagInfo.offset, i->TagInfo.size);
257         }
258 
259         for (n=0, i=pIcc->m_Tags->begin(); i!=pIcc->m_Tags->end(); i++, n++)
260             dumpTag(outfile, pIcc, i->TagInfo.sig);
261     }
262     delete pIcc;
263 }
264 
265 int main(int argc, char* argv[])
266 {
267     const char* myName = path_tail(argv[0]);
268 
269     try
270     {
271         const char* const out_file_pathname = icc_file_name;
272 
273         CIccProfile profile;
274         profile.InitHeader();
275 
276         profile.m_Header.date.year = data_last_changed_year;
277         profile.m_Header.date.month = data_last_changed_month;
278         profile.m_Header.date.day = data_last_day;
279         profile.m_Header.date.hours = data_last_changed_hour;
280         profile.m_Header.date.minutes = data_last_changed_minute;
281         profile.m_Header.date.seconds = 0;
282 
283         profile.m_Header.deviceClass = icSigDisplayClass;
284         profile.m_Header.colorSpace = icSigRgbData;
285         profile.m_Header.pcs = icSigXYZData;
286         profile.m_Header.platform = icSigUnkownPlatform;
287         profile.m_Header.attributes = static_cast<icUInt64Number>(icReflective);
288         profile.m_Header.renderingIntent = icPerceptual;
289 
290         profile.m_Header.cmmId = 0x6E6F6E65; /* 'none' */
291         profile.m_Header.model = 0x73524742;//sRGB
292 
293         profile.m_Header.version=icVersionNumberV2_1;
294 
295         // Required tags for a three-component matrix-based display profile, as laid
296         // out by specification ICC.1:1998-09 (clause 6.3)  are:
297         //
298         //   copyrightTag
299         //   profileDescriptionTag
300         //   redMatrixColumnTag
301         //   greenMatrixColumnTag
302         //   blueMatrixColumnTag
303         //   redTRCTag
304         //   greenTRCTag
305         //   blueTRCTag
306         //   mediaWhitePointTag
307 
308         // the other tags:
309         //
310         // technologyTag
311         // deviceModelTag
312         // deviceMfgDescTag
313         // mediaBlackPointTag
314         // viewingCondDescTag
315         // viewingConditionsTag
316         // luminanceTag
317         // measurementTag
318         //
319         // are optionals, added for completeness
320 
321         // the element below are sorted in the same order as
322         // the list above, but the LUT table,
323         // embedded at the end of the profile
324 
325         // copyrightTag
326         CIccTagText* copyrightTag = new CIccTagText;
327         copyrightTag->SetText(copyright);
328         profile.AttachTag(icSigCopyrightTag, copyrightTag);
329 
330         // profileDescriptionTag
331         CIccTagTextDescription* descriptionTag = new CIccTagTextDescription;
332         descriptionTag->SetText(description);
333         profile.AttachTag(icSigProfileDescriptionTag, descriptionTag);
334 
335         CIccTagXYZ* redMatrixColumnTag = new CIccTagXYZ;
336         //values from raccomandation of ICC for sRGB, D50 referenced characterisation data
337         //should be: 0.4361, 0.2225, 0.0139 according to application notes,
338         // the 'X' value below is the one commonly in use on a very
339         // diffused sRGB profile
340         (*redMatrixColumnTag)[0].X = icDtoF(0.4361);
341         (*redMatrixColumnTag)[0].Y = icDtoF(0.2225);
342         (*redMatrixColumnTag)[0].Z = icDtoF(0.0139);
343         profile.AttachTag(icSigRedMatrixColumnTag, redMatrixColumnTag);
344 
345         CIccTagXYZ* greenMatrixColumnTag = new CIccTagXYZ;
346         //values from raccomandation of ICC for sRGB, D50 referenced characterisation data
347         (*greenMatrixColumnTag)[0].X = icDtoF(0.3851);
348         (*greenMatrixColumnTag)[0].Y = icDtoF(0.7169);
349         (*greenMatrixColumnTag)[0].Z = icDtoF(0.0971);
350         profile.AttachTag(icSigGreenMatrixColumnTag, greenMatrixColumnTag);
351 
352         CIccTagXYZ* blueMatrixColumnTag = new CIccTagXYZ;
353         //values from raccomandation of ICC for sRGB, D50 referenced characterisation data
354         //should be: 0.1431, 0.0606, 0.7139 according to application notes,
355         // the 'Z' value below is the one commonly in use on a very
356         // diffused sRGB profile
357         (*blueMatrixColumnTag)[0].X = icDtoF(0.1431);
358         (*blueMatrixColumnTag)[0].Y = icDtoF(0.0606);
359         (*blueMatrixColumnTag)[0].Z = icDtoF(0.7141);
360         profile.AttachTag(icSigBlueMatrixColumnTag, blueMatrixColumnTag);
361 
362         // mediaWhitePointTag
363         CIccTagXYZ* whitePointTag = new CIccTagXYZ;
364         (*whitePointTag)[0].X = icDtoF(0.9505);
365         (*whitePointTag)[0].Y = icDtoF(1.0);
366         (*whitePointTag)[0].Z = icDtoF(1.0891);
367         profile.AttachTag(icSigMediaWhitePointTag, whitePointTag);
368 
369         //device signature (technologytag)
370         CIccTagSignature* deviceSign = new CIccTagSignature;
371         deviceSign->SetValue( icSigCRTDisplay );
372         profile.AttachTag( icSigTechnologyTag, deviceSign );
373 
374         //device model tag
375         CIccTagTextDescription* deviceModelTag = new CIccTagTextDescription;
376         deviceModelTag->SetText("IEC 61966-2.1 Default RGB colour space - sRGB");
377         profile.AttachTag( icSigDeviceModelDescTag, deviceModelTag);
378 
379         // deviceMfgDescTag
380         CIccTagTextDescription* deviceMfgTag = new CIccTagTextDescription;
381         deviceMfgTag->SetText("IEC http://www.iec.ch");
382         profile.AttachTag( icSigDeviceMfgDescTag, deviceMfgTag);
383 
384         // mediaBlackPointTag
385         CIccTagXYZ* blackPointTag = new CIccTagXYZ;
386         (*blackPointTag)[0].X =
387         (*blackPointTag)[0].Y =
388         (*blackPointTag)[0].Z = icDtoF(0.0);
389         profile.AttachTag(icSigMediaBlackPointTag, blackPointTag);
390 
391         // viewingCondDescTag
392         CIccTagTextDescription* viewingCondDescTag = new CIccTagTextDescription;
393         viewingCondDescTag->SetText("Reference viewing condition according to IEC 61966-2.1");
394         profile.AttachTag( icSigViewingCondDescTag, viewingCondDescTag );
395 
396         // viewingConditionsTag
397         CIccTagViewingConditions* viewingConditionsTag = new  CIccTagViewingConditions;
398         // Illuminant tristimulus value
399         (*viewingConditionsTag).m_XYZIllum.X = icDtoF(19.6445);
400         (*viewingConditionsTag).m_XYZIllum.Y = icDtoF(20.3718);
401         (*viewingConditionsTag).m_XYZIllum.Z = icDtoF(16.8089);
402         // surround tristimulus value
403         (*viewingConditionsTag).m_XYZSurround.X = icDtoF(3.9289);
404         (*viewingConditionsTag).m_XYZSurround.Y = icDtoF(4.0744);
405         (*viewingConditionsTag).m_XYZSurround.Z = icDtoF(3.3618);
406         (*viewingConditionsTag).m_illumType = icIlluminantD50;
407         profile.AttachTag( icSigViewingConditionsType, viewingConditionsTag );
408 
409         // luminanceTag
410         CIccTagXYZ* luminanceTag = new CIccTagXYZ;
411         (*luminanceTag)[0].X = icDtoF(76.0365);
412         (*luminanceTag)[0].Y = icDtoF(80.0);
413         (*luminanceTag)[0].Z = icDtoF(87.1246);
414         profile.AttachTag(icSigLuminanceTag, luminanceTag);
415 
416         // measurementTag
417         CIccTagMeasurement* measurementTag = new  CIccTagMeasurement;
418         (*measurementTag).m_Data.stdObserver = icStdObs1931TwoDegrees;
419         (*measurementTag).m_Data.backing.X =
420         (*measurementTag).m_Data.backing.Y =
421         (*measurementTag).m_Data.backing.Z = icDtoF(0.0);
422         (*measurementTag).m_Data.geometry = icGeometryUnknown;
423         // the flare is 1%, but the library doesn't seem all right with this
424         // see specification ICC.1:1998-09, clause 6.5.8, table 55 fot the right
425         // format of the data value
426         (*measurementTag).m_Data.flare = static_cast< icMeasurementFlare > ( icDtoUF( 0.01 ) );//means 1%
427         (*measurementTag).m_Data.illuminant = icIlluminantD65;
428         profile.AttachTag(icSigMeasurementTag, measurementTag );
429 
430         // compute the LUT curves, they are equal for all three colors
431         // so only one LUT is computed and stored
432         int N = 1024; // number of points in LUTs
433         CIccTagCurve* colorTRCTag = new CIccTagCurve(N);
434         // apply conversion from RGB to XYZ, stepping the RGB value linearly from 0 to 100%
435         // 1024 steps are computed
436         for (int i = 0; i < N; ++i)
437             (*colorTRCTag)[i] = computeIEC_RGBtoXYZ( (icFloatNumber)i/(N-1));
438 
439         profile.AttachTag(icSigRedTRCTag, colorTRCTag);
440         profile.AttachTag(icSigGreenTRCTag, colorTRCTag);
441         profile.AttachTag(icSigBlueTRCTag, colorTRCTag);
442 
443         //Verify things
444         string validationReport;
445         icValidateStatus validationStatus = profile.Validate(validationReport);
446 
447         switch (validationStatus)
448         {
449         case icValidateOK:
450             break;
451 
452         case icValidateWarning:
453             clog << "Profile validation warning" << endl
454                  << validationReport;
455             break;
456 
457         case icValidateNonCompliant:
458             clog << "Profile non compliancy" << endl
459                  << validationReport;
460             break;
461 
462         case icValidateCriticalError:
463         default:
464             clog << "Profile Error" << endl
465                  << validationReport;
466         }
467 
468         // Out it goes
469         CIccFileIO out;
470         out.Open(out_file_pathname, "wb+");
471         profile.Write(&out);
472         out.Close();
473 
474         FILE *headerfile = fopen(hxx_file_name,"w");
475 
476         //print OpenOffice standard file header
477         const char *the_string;
478 
479         int idx = 0;
480 
481         while((the_string = TheHeader1[idx++]) != NULL )
482             fprintf(headerfile,"%s\n",the_string);
483 // print the creation date (today)
484 // print the date of sRGB ICC profile data
485         fprintf(headerfile," *  the date of last change to sRGB ICC profile data is:\n *  %4d/%02d/%02d - %02d:%02d\n",
486                 data_last_changed_year, data_last_changed_month,
487                 data_last_day, data_last_changed_hour,data_last_changed_minute );
488 
489         idx = 0;
490 
491         while((the_string = TheHeader2[idx++]) != NULL )
492             fprintf(headerfile,"%s\n",the_string);
493 
494         {
495 // spit out the data structure (an array of unsigned char)
496             FILE *infile;
497 
498             int achar, number = 1;
499 
500             infile = fopen(out_file_pathname,"rb");
501 
502             fseek(infile,0,SEEK_END);
503             long int thesize= ftell(infile);
504             fseek(infile,0,SEEK_SET);
505 
506             fprintf(headerfile,"\nsal_uInt8 nsRGB_ICC_profile[%d]=\n{\n    ",thesize);
507 
508             do
509             {
510                 achar = fgetc(infile);
511                 if(achar == EOF)
512                     break;
513                 fprintf(headerfile,"0x%02x",achar);
514                 if(number % 12 == 0)
515                     fprintf(headerfile,",\n    ");
516                 else
517                     fprintf(headerfile,", ");
518                 number++;
519             } while(achar != EOF );
520             fprintf(headerfile,"\n};\n\n");
521 
522             fclose(infile);
523         }
524         // append the file contents, in human readable form, as comment in the header
525         // get the functions from iccDump
526 
527         fprintf(headerfile,"/*****************\n\n");
528 
529         fprintf(headerfile,"This ICC profile contains the following data:\n\n");
530 
531         dumpProfile(headerfile, out_file_pathname );
532 
533         fprintf(headerfile,"\n*****************/\n");
534         //now append the tail
535         idx = 0;
536         while((the_string = TheTail[idx++]) != NULL )
537             fprintf(headerfile,"%s\n",the_string);
538 
539         fclose(headerfile);
540 
541         return EXIT_SUCCESS;
542     }
543     catch (const std::exception& e)
544     {
545         cout << myName << ": error: " << e.what() << endl;
546         return EXIT_FAILURE;
547     }
548 }
549