1 /*************************************************************************
2  *
3  *  Apache OpenOffice - 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 = "Licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the License.";
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 =     2011;
97 const int  data_last_changed_month =    10;
98 const int  data_last_day =              16;
99 const int  data_last_changed_hour =     18;
100 const int  data_last_changed_minute =   55;
101 
102 // the following string array it's the standard OOo header format
103 const char * const TheHeader1[] =
104 {
105     "/*************************************************************************",
106     " *",
107     " *  Apache OpenOffice - 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 2012, 2011 Apache OpenOffice.",
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 
computeIEC_RGBtoXYZ(icFloatNumber indata)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 
computeIEC_XYZtoRGB(icFloatNumber indata)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 
dumpTag(FILE * outfile,CIccProfile * pIcc,icTagSignature sig)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 
dumpProfile(FILE * outfile,const char * profileName)200 void dumpProfile(FILE *outfile, const char * profileName)
201 {
202     CIccProfile *pIcc;
203     icValidateStatus nStatus;
204 
205     pIcc = OpenIccProfile(profileName);
206 
207     if (!pIcc)
208         printf("Unable to open '%s'\n", profileName);
209     else
210     {
211         icHeader *pHdr = &pIcc->m_Header;
212         CIccInfo Fmt;
213         char buf[64];
214 
215         fprintf(outfile,"Profile:          '%s'\n", profileName);
216         if(Fmt.IsProfileIDCalculated(&pHdr->profileID))
217             fprintf(outfile,"Profile ID:        %s\n", Fmt.GetProfileID(&pHdr->profileID));
218         else
219             fprintf(outfile,"Profile ID:       Profile ID not calculated.\n");
220         fprintf(outfile,"Size:             %ld(0x%lx) bytes\n", pHdr->size, pHdr->size);
221 
222         fprintf(outfile,"\nHeader\n");
223         fprintf(outfile,"------\n");
224         fprintf(outfile,"Attributes:       %s\n", Fmt.GetDeviceAttrName(pHdr->attributes));
225         fprintf(outfile,"Cmm:              %s\n", Fmt.GetCmmSigName((icCmmSignature)(pHdr->cmmId)));
226         fprintf(outfile,"Creation Date:    %d/%d/%d  %02u:%02u:%02u\n",
227                 pHdr->date.month, pHdr->date.day, pHdr->date.year,
228                 pHdr->date.hours, pHdr->date.minutes, pHdr->date.seconds);
229         fprintf(outfile,"Creator:          %s\n", icGetSig(buf, pHdr->creator));
230         fprintf(outfile,"Data Color Space: %s\n", Fmt.GetColorSpaceSigName(pHdr->colorSpace));
231         fprintf(outfile,"Flags             %s\n", Fmt.GetProfileFlagsName(pHdr->flags));
232         fprintf(outfile,"PCS Color Space:  %s\n", Fmt.GetColorSpaceSigName(pHdr->pcs));
233         fprintf(outfile,"Platform:         %s\n", Fmt.GetPlatformSigName(pHdr->platform));
234         fprintf(outfile,"Rendering Intent: %s\n", Fmt.GetRenderingIntentName((icRenderingIntent)(pHdr->renderingIntent)));
235         fprintf(outfile,"Type:             %s\n", Fmt.GetProfileClassSigName(pHdr->deviceClass));
236         fprintf(outfile,"Version:          %s\n", Fmt.GetVersionName(pHdr->version));
237         fprintf(outfile,"Illuminant:       X=%.4lf, Y=%.4lf, Z=%.4lf\n",
238                 icFtoD(pHdr->illuminant.X),
239                 icFtoD(pHdr->illuminant.Y),
240                 icFtoD(pHdr->illuminant.Z));
241 
242         fprintf(outfile,"\nProfile Tags\n");
243         fprintf(outfile,"------------\n");
244 
245         fprintf(outfile,"%25s    ID    %8s\t%8s\n", "Tag", "Offset", "Size");
246         fprintf(outfile,"%25s  ------  %8s\t%8s\n", "----", "------", "----");
247 
248         int n;
249         TagEntryList::iterator i;
250 
251         for (n=0, i=pIcc->m_Tags->begin(); i!=pIcc->m_Tags->end(); i++, n++)
252         {
253             fprintf(outfile,"%25s  %s  %8ld\t%8ld\n", Fmt.GetTagSigName(i->TagInfo.sig),
254                     icGetSig(buf, i->TagInfo.sig, false),
255                     i->TagInfo.offset, i->TagInfo.size);
256         }
257 
258         for (n=0, i=pIcc->m_Tags->begin(); i!=pIcc->m_Tags->end(); i++, n++)
259             dumpTag(outfile, pIcc, i->TagInfo.sig);
260     }
261     delete pIcc;
262 }
263 
main(int argc,char * argv[])264 int main(int argc, char* argv[])
265 {
266     const char* myName = path_tail(argv[0]);
267 
268     try
269     {
270         const char* const out_file_pathname = icc_file_name;
271 
272         CIccProfile profile;
273         profile.InitHeader();
274 
275         profile.m_Header.date.year = data_last_changed_year;
276         profile.m_Header.date.month = data_last_changed_month;
277         profile.m_Header.date.day = data_last_day;
278         profile.m_Header.date.hours = data_last_changed_hour;
279         profile.m_Header.date.minutes = data_last_changed_minute;
280         profile.m_Header.date.seconds = 0;
281 
282         profile.m_Header.deviceClass = icSigDisplayClass;
283         profile.m_Header.colorSpace = icSigRgbData;
284         profile.m_Header.pcs = icSigXYZData;
285         profile.m_Header.platform = icSigUnkownPlatform;
286         profile.m_Header.attributes = static_cast<icUInt64Number>(icReflective);
287         profile.m_Header.renderingIntent = icPerceptual;
288 
289         profile.m_Header.cmmId = 0x6E6F6E65; /* 'none' */
290         profile.m_Header.model = 0x73524742;//sRGB
291 
292         profile.m_Header.version=icVersionNumberV2_1;
293 
294         // Required tags for a three-component matrix-based display profile, as laid
295         // out by specification ICC.1:1998-09 (clause 6.3)  are:
296         //
297         //   copyrightTag
298         //   profileDescriptionTag
299         //   redMatrixColumnTag
300         //   greenMatrixColumnTag
301         //   blueMatrixColumnTag
302         //   redTRCTag
303         //   greenTRCTag
304         //   blueTRCTag
305         //   mediaWhitePointTag
306 
307         // the other tags:
308         //
309         // technologyTag
310         // deviceModelTag
311         // deviceMfgDescTag
312         // mediaBlackPointTag
313         // viewingCondDescTag
314         // viewingConditionsTag
315         // luminanceTag
316         // measurementTag
317         //
318         // are optionals, added for completeness
319 
320         // the element below are sorted in the same order as
321         // the list above, but the LUT table,
322         // embedded at the end of the profile
323 
324         // copyrightTag
325         CIccTagText* copyrightTag = new CIccTagText;
326         copyrightTag->SetText(copyright);
327         profile.AttachTag(icSigCopyrightTag, copyrightTag);
328 
329         // profileDescriptionTag
330         CIccTagTextDescription* descriptionTag = new CIccTagTextDescription;
331         descriptionTag->SetText(description);
332         profile.AttachTag(icSigProfileDescriptionTag, descriptionTag);
333 
334         CIccTagXYZ* redMatrixColumnTag = new CIccTagXYZ;
335         //values from raccomandation of ICC for sRGB, D50 referenced characterisation data
336         //should be: 0.4361, 0.2225, 0.0139 according to application notes,
337         // the 'X' value below is the one commonly in use on a very
338         // diffused sRGB profile
339         (*redMatrixColumnTag)[0].X = icDtoF(0.4361);
340         (*redMatrixColumnTag)[0].Y = icDtoF(0.2225);
341         (*redMatrixColumnTag)[0].Z = icDtoF(0.0139);
342         profile.AttachTag(icSigRedMatrixColumnTag, redMatrixColumnTag);
343 
344         CIccTagXYZ* greenMatrixColumnTag = new CIccTagXYZ;
345         //values from raccomandation of ICC for sRGB, D50 referenced characterisation data
346         (*greenMatrixColumnTag)[0].X = icDtoF(0.3851);
347         (*greenMatrixColumnTag)[0].Y = icDtoF(0.7169);
348         (*greenMatrixColumnTag)[0].Z = icDtoF(0.0971);
349         profile.AttachTag(icSigGreenMatrixColumnTag, greenMatrixColumnTag);
350 
351         CIccTagXYZ* blueMatrixColumnTag = new CIccTagXYZ;
352         //values from raccomandation of ICC for sRGB, D50 referenced characterisation data
353         //should be: 0.1431, 0.0606, 0.7139 according to application notes,
354         // the 'Z' value below is the one commonly in use on a very
355         // diffused sRGB profile
356         (*blueMatrixColumnTag)[0].X = icDtoF(0.1431);
357         (*blueMatrixColumnTag)[0].Y = icDtoF(0.0606);
358         (*blueMatrixColumnTag)[0].Z = icDtoF(0.7141);
359         profile.AttachTag(icSigBlueMatrixColumnTag, blueMatrixColumnTag);
360 
361         // mediaWhitePointTag
362         CIccTagXYZ* whitePointTag = new CIccTagXYZ;
363         (*whitePointTag)[0].X = icDtoF(0.9505);
364         (*whitePointTag)[0].Y = icDtoF(1.0);
365         (*whitePointTag)[0].Z = icDtoF(1.0891);
366         profile.AttachTag(icSigMediaWhitePointTag, whitePointTag);
367 
368         //device signature (technologytag)
369         CIccTagSignature* deviceSign = new CIccTagSignature;
370         deviceSign->SetValue( icSigCRTDisplay );
371         profile.AttachTag( icSigTechnologyTag, deviceSign );
372 
373         //device model tag
374         CIccTagTextDescription* deviceModelTag = new CIccTagTextDescription;
375         deviceModelTag->SetText("IEC 61966-2.1 Default RGB colour space - sRGB");
376         profile.AttachTag( icSigDeviceModelDescTag, deviceModelTag);
377 
378         // deviceMfgDescTag
379         CIccTagTextDescription* deviceMfgTag = new CIccTagTextDescription;
380         deviceMfgTag->SetText("IEC http://www.iec.ch");
381         profile.AttachTag( icSigDeviceMfgDescTag, deviceMfgTag);
382 
383         // mediaBlackPointTag
384         CIccTagXYZ* blackPointTag = new CIccTagXYZ;
385         (*blackPointTag)[0].X =
386         (*blackPointTag)[0].Y =
387         (*blackPointTag)[0].Z = icDtoF(0.0);
388         profile.AttachTag(icSigMediaBlackPointTag, blackPointTag);
389 
390         // viewingCondDescTag
391         CIccTagTextDescription* viewingCondDescTag = new CIccTagTextDescription;
392         viewingCondDescTag->SetText("Reference viewing condition according to IEC 61966-2.1");
393         profile.AttachTag( icSigViewingCondDescTag, viewingCondDescTag );
394 
395         // viewingConditionsTag
396         CIccTagViewingConditions* viewingConditionsTag = new  CIccTagViewingConditions;
397         // Illuminant tristimulus value
398         (*viewingConditionsTag).m_XYZIllum.X = icDtoF(19.6445);
399         (*viewingConditionsTag).m_XYZIllum.Y = icDtoF(20.3718);
400         (*viewingConditionsTag).m_XYZIllum.Z = icDtoF(16.8089);
401         // surround tristimulus value
402         (*viewingConditionsTag).m_XYZSurround.X = icDtoF(3.9289);
403         (*viewingConditionsTag).m_XYZSurround.Y = icDtoF(4.0744);
404         (*viewingConditionsTag).m_XYZSurround.Z = icDtoF(3.3618);
405         (*viewingConditionsTag).m_illumType = icIlluminantD50;
406         profile.AttachTag( icSigViewingConditionsType, viewingConditionsTag );
407 
408         // luminanceTag
409         CIccTagXYZ* luminanceTag = new CIccTagXYZ;
410         (*luminanceTag)[0].X = icDtoF(76.0365);
411         (*luminanceTag)[0].Y = icDtoF(80.0);
412         (*luminanceTag)[0].Z = icDtoF(87.1246);
413         profile.AttachTag(icSigLuminanceTag, luminanceTag);
414 
415         // measurementTag
416         CIccTagMeasurement* measurementTag = new  CIccTagMeasurement;
417         (*measurementTag).m_Data.stdObserver = icStdObs1931TwoDegrees;
418         (*measurementTag).m_Data.backing.X =
419         (*measurementTag).m_Data.backing.Y =
420         (*measurementTag).m_Data.backing.Z = icDtoF(0.0);
421         (*measurementTag).m_Data.geometry = icGeometryUnknown;
422         // the flare is 1%, but the library doesn't seem all right with this
423         // see specification ICC.1:1998-09, clause 6.5.8, table 55 fot the right
424         // format of the data value
425         (*measurementTag).m_Data.flare = static_cast< icMeasurementFlare > ( icDtoUF( 0.01 ) );//means 1%
426         (*measurementTag).m_Data.illuminant = icIlluminantD65;
427         profile.AttachTag(icSigMeasurementTag, measurementTag );
428 
429         // compute the LUT curves, they are equal for all three colors
430         // so only one LUT is computed and stored
431         int N = 1024; // number of points in LUTs
432         CIccTagCurve* colorTRCTag = new CIccTagCurve(N);
433         // apply conversion from RGB to XYZ, stepping the RGB value linearly from 0 to 100%
434         // 1024 steps are computed
435         for (int i = 0; i < N; ++i)
436             (*colorTRCTag)[i] = computeIEC_RGBtoXYZ( (icFloatNumber)i/(N-1));
437 
438         profile.AttachTag(icSigRedTRCTag, colorTRCTag);
439         profile.AttachTag(icSigGreenTRCTag, colorTRCTag);
440         profile.AttachTag(icSigBlueTRCTag, colorTRCTag);
441 
442         //Verify things
443         string validationReport;
444         icValidateStatus validationStatus = profile.Validate(validationReport);
445 
446         switch (validationStatus)
447         {
448         case icValidateOK:
449             break;
450 
451         case icValidateWarning:
452             clog << "Profile validation warning" << endl
453                  << validationReport;
454             break;
455 
456         case icValidateNonCompliant:
457             clog << "Profile non compliancy" << endl
458                  << validationReport;
459             break;
460 
461         case icValidateCriticalError:
462         default:
463             clog << "Profile Error" << endl
464                  << validationReport;
465         }
466 
467         // Out it goes
468         CIccFileIO out;
469         out.Open(out_file_pathname, "wb+");
470         profile.Write(&out);
471         out.Close();
472 
473         FILE *headerfile = fopen(hxx_file_name,"w");
474 
475         //print OpenOffice standard file header
476         const char *the_string;
477 
478         int idx = 0;
479 
480         while((the_string = TheHeader1[idx++]) != NULL )
481             fprintf(headerfile,"%s\n",the_string);
482 // print the creation date (today)
483 // print the date of sRGB ICC profile data
484         fprintf(headerfile," *  the date of last change to sRGB ICC profile data is:\n *  %4d/%02d/%02d - %02d:%02d\n",
485                 data_last_changed_year, data_last_changed_month,
486                 data_last_day, data_last_changed_hour,data_last_changed_minute );
487 
488         idx = 0;
489 
490         while((the_string = TheHeader2[idx++]) != NULL )
491             fprintf(headerfile,"%s\n",the_string);
492 
493         {
494 // spit out the data structure (an array of unsigned char)
495             FILE *infile;
496 
497             int achar, number = 1;
498 
499             infile = fopen(out_file_pathname,"rb");
500 
501             fseek(infile,0,SEEK_END);
502             long int thesize= ftell(infile);
503             fseek(infile,0,SEEK_SET);
504 
505             fprintf(headerfile,"\nsal_uInt8 nsRGB_ICC_profile[%d]=\n{\n    ",thesize);
506 
507             do
508             {
509                 achar = fgetc(infile);
510                 if(achar == EOF)
511                     break;
512                 fprintf(headerfile,"0x%02x",achar);
513                 if(number % 12 == 0)
514                     fprintf(headerfile,",\n    ");
515                 else
516                     fprintf(headerfile,", ");
517                 number++;
518             } while(achar != EOF );
519             fprintf(headerfile,"\n};\n\n");
520 
521             fclose(infile);
522         }
523         // append the file contents, in human readable form, as comment in the header
524         // get the functions from iccDump
525 
526         fprintf(headerfile,"/*****************\n\n");
527 
528         fprintf(headerfile,"This ICC profile contains the following data:\n\n");
529 
530         dumpProfile(headerfile, out_file_pathname );
531 
532         fprintf(headerfile,"\n*****************/\n");
533         //now append the tail
534         idx = 0;
535         while((the_string = TheTail[idx++]) != NULL )
536             fprintf(headerfile,"%s\n",the_string);
537 
538         fclose(headerfile);
539 
540         return EXIT_SUCCESS;
541     }
542     catch (const std::exception& e)
543     {
544         cout << myName << ": error: " << e.what() << endl;
545         return EXIT_FAILURE;
546     }
547 }
548