Code

From trunk
[inkscape.git] / src / color-profile.cpp
3 //#define DEBUG_LCMS
5 #include <glib/gstdio.h>
6 #include <sys/fcntl.h>
7 #include <gdkmm/color.h>
9 #ifdef DEBUG_LCMS
10 #include <gtk/gtkmessagedialog.h>
11 #endif // DEBUG_LCMS
13 #include <cstring>
14 #include <string>
15 #include "xml/repr.h"
16 #include "color-profile.h"
17 #include "color-profile-fns.h"
18 #include "attributes.h"
19 #include "inkscape.h"
20 #include "document.h"
21 #include "preferences.h"
23 #include "dom/uri.h"
24 #include "dom/util/digest.h"
26 using Inkscape::ColorProfile;
27 using Inkscape::ColorProfileClass;
29 namespace Inkscape
30 {
31 #if ENABLE_LCMS
32 static cmsHPROFILE colorprofile_get_system_profile_handle();
33 static cmsHPROFILE colorprofile_get_proof_profile_handle();
34 #endif // ENABLE_LCMS
35 }
37 #ifdef DEBUG_LCMS
38 extern guint update_in_progress;
39 #define DEBUG_MESSAGE(key, ...) \
40 {\
41     Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
42     bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
43     bool dumpD = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D");\
44     bool dumpD2 = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D2");\
45     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
46     if ( dump )\
47     {\
48         g_message( __VA_ARGS__ );\
49 \
50     }\
51     if ( dumpD )\
52     {\
53         GtkWidget *dialog = gtk_message_dialog_new(NULL,\
54                                                    GTK_DIALOG_DESTROY_WITH_PARENT, \
55                                                    GTK_MESSAGE_INFO,    \
56                                                    GTK_BUTTONS_OK,      \
57                                                    __VA_ARGS__          \
58                                                    );\
59         g_signal_connect_swapped(dialog, "response",\
60                                  G_CALLBACK(gtk_widget_destroy),        \
61                                  dialog);                               \
62         gtk_widget_show_all( dialog );\
63     }\
64 }
65 #endif // DEBUG_LCMS
67 static SPObjectClass *cprof_parent_class;
69 #if ENABLE_LCMS
71 cmsHPROFILE ColorProfile::_sRGBProf = 0;
73 cmsHPROFILE ColorProfile::getSRGBProfile() {
74     if ( !_sRGBProf ) {
75         _sRGBProf = cmsCreate_sRGBProfile();
76     }
77     return _sRGBProf;
78 }
80 #endif // ENABLE_LCMS
82 /**
83  * Register ColorProfile class and return its type.
84  */
85 GType Inkscape::colorprofile_get_type()
86 {
87     return ColorProfile::getType();
88 }
90 GType ColorProfile::getType()
91 {
92     static GType type = 0;
93     if (!type) {
94         GTypeInfo info = {
95             sizeof(ColorProfileClass),
96             NULL, NULL,
97             (GClassInitFunc) ColorProfile::classInit,
98             NULL, NULL,
99             sizeof(ColorProfile),
100             16,
101             (GInstanceInitFunc) ColorProfile::init,
102             NULL,   /* value_table */
103         };
104         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
105     }
106     return type;
109 /**
110  * ColorProfile vtable initialization.
111  */
112 void ColorProfile::classInit( ColorProfileClass *klass )
114     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
116     cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
118     sp_object_class->release = ColorProfile::release;
119     sp_object_class->build = ColorProfile::build;
120     sp_object_class->set = ColorProfile::set;
121     sp_object_class->write = ColorProfile::write;
124 /**
125  * Callback for ColorProfile object initialization.
126  */
127 void ColorProfile::init( ColorProfile *cprof )
129     cprof->href = 0;
130     cprof->local = 0;
131     cprof->name = 0;
132     cprof->intentStr = 0;
133     cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
134 #if ENABLE_LCMS
135     cprof->profHandle = 0;
136     cprof->_profileClass = icSigInputClass;
137     cprof->_profileSpace = icSigRgbData;
138     cprof->_transf = 0;
139     cprof->_revTransf = 0;
140 #endif // ENABLE_LCMS
143 /**
144  * Callback: free object
145  */
146 void ColorProfile::release( SPObject *object )
148     // Unregister ourselves
149     SPDocument* document = SP_OBJECT_DOCUMENT(object);
150     if ( document ) {
151         sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
152     }
154     ColorProfile *cprof = COLORPROFILE(object);
155     if ( cprof->href ) {
156         g_free( cprof->href );
157         cprof->href = 0;
158     }
160     if ( cprof->local ) {
161         g_free( cprof->local );
162         cprof->local = 0;
163     }
165     if ( cprof->name ) {
166         g_free( cprof->name );
167         cprof->name = 0;
168     }
170     if ( cprof->intentStr ) {
171         g_free( cprof->intentStr );
172         cprof->intentStr = 0;
173     }
175 #if ENABLE_LCMS
176     cprof->_clearProfile();
177 #endif // ENABLE_LCMS
180 #if ENABLE_LCMS
181 void ColorProfile::_clearProfile()
183     _profileSpace = icSigRgbData;
185     if ( _transf ) {
186         cmsDeleteTransform( _transf );
187         _transf = 0;
188     }
189     if ( _revTransf ) {
190         cmsDeleteTransform( _revTransf );
191         _revTransf = 0;
192     }
193     if ( profHandle ) {
194         cmsCloseProfile( profHandle );
195         profHandle = 0;
196     }
198 #endif // ENABLE_LCMS
200 /**
201  * Callback: set attributes from associated repr.
202  */
203 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
205     ColorProfile *cprof = COLORPROFILE(object);
206     g_assert(cprof->href == 0);
207     g_assert(cprof->local == 0);
208     g_assert(cprof->name == 0);
209     g_assert(cprof->intentStr == 0);
211     if (cprof_parent_class->build) {
212         (* cprof_parent_class->build)(object, document, repr);
213     }
214     sp_object_read_attr( object, "xlink:href" );
215     sp_object_read_attr( object, "local" );
216     sp_object_read_attr( object, "name" );
217     sp_object_read_attr( object, "rendering-intent" );
219     // Register
220     if ( document ) {
221         sp_document_add_resource( document, "iccprofile", object );
222     }
225 /**
226  * Callback: set attribute.
227  */
228 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
230     ColorProfile *cprof = COLORPROFILE(object);
232     switch (key) {
233         case SP_ATTR_XLINK_HREF:
234             if ( cprof->href ) {
235                 g_free( cprof->href );
236                 cprof->href = 0;
237             }
238             if ( value ) {
239                 cprof->href = g_strdup( value );
240                 if ( *cprof->href ) {
241 #if ENABLE_LCMS
242                     cmsErrorAction( LCMS_ERROR_SHOW );
244                     // TODO open filename and URIs properly
245                     //FILE* fp = fopen_utf8name( filename, "r" );
246                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
248                     // Try to open relative
249                     SPDocument *doc = SP_OBJECT_DOCUMENT(object);
250                     if (!doc) {
251                         doc = SP_ACTIVE_DOCUMENT;
252                         g_warning("object has no document.  using active");
253                     }
254                     //# 1.  Get complete URI of document
255                     gchar const *docbase = SP_DOCUMENT_URI( doc );
256                     if (!docbase)
257                         {
258                         g_warning("null docbase");
259                         docbase = "";
260                         }
261                     //g_message("docbase:%s\n", docbase);
262                     org::w3c::dom::URI docUri(docbase);
263                     //# 2. Get href of icc file.  we don't care if it's rel or abs
264                     org::w3c::dom::URI hrefUri(cprof->href);
265                     //# 3.  Resolve the href according the docBase.  This follows
266                     //      the w3c specs.  All absolute and relative issues are considered
267                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
268                     gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
269                     cprof->_clearProfile();
270                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
271                     if ( cprof->profHandle ) {
272                         cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
273                         cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
274                     }
275 #ifdef DEBUG_LCMS
276                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
277 #endif // DEBUG_LCMS
279 #endif // ENABLE_LCMS
280                 }
281             }
282             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
283             break;
285         case SP_ATTR_LOCAL:
286             if ( cprof->local ) {
287                 g_free( cprof->local );
288                 cprof->local = 0;
289             }
290             cprof->local = g_strdup( value );
291             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
292             break;
294         case SP_ATTR_NAME:
295             if ( cprof->name ) {
296                 g_free( cprof->name );
297                 cprof->name = 0;
298             }
299             cprof->name = g_strdup( value );
300 #ifdef DEBUG_LCMS
301             DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
302 #endif // DEBUG_LCMS
303             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
304             break;
306         case SP_ATTR_RENDERING_INTENT:
307             if ( cprof->intentStr ) {
308                 g_free( cprof->intentStr );
309                 cprof->intentStr = 0;
310             }
311             cprof->intentStr = g_strdup( value );
313             if ( value ) {
314                 if ( strcmp( value, "auto" ) == 0 ) {
315                     cprof->rendering_intent = RENDERING_INTENT_AUTO;
316                 } else if ( strcmp( value, "perceptual" ) == 0 ) {
317                     cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
318                 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
319                     cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
320                 } else if ( strcmp( value, "saturation" ) == 0 ) {
321                     cprof->rendering_intent = RENDERING_INTENT_SATURATION;
322                 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
323                     cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
324                 } else {
325                     cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
326                 }
327             } else {
328                 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
329             }
331             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
332             break;
334         default:
335             if (cprof_parent_class->set) {
336                 (* cprof_parent_class->set)(object, key, value);
337             }
338             break;
339     }
343 /**
344  * Callback: write attributes to associated repr.
345  */
346 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
348     ColorProfile *cprof = COLORPROFILE(object);
350     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
351         repr = xml_doc->createElement("svg:color-profile");
352     }
354     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
355         repr->setAttribute( "xlink:href", cprof->href );
356     }
358     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
359         repr->setAttribute( "local", cprof->local );
360     }
362     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
363         repr->setAttribute( "name", cprof->name );
364     }
366     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
367         repr->setAttribute( "rendering-intent", cprof->intentStr );
368     }
370     if (cprof_parent_class->write) {
371         (* cprof_parent_class->write)(object, xml_doc, repr, flags);
372     }
374     return repr;
378 #if ENABLE_LCMS
380 struct MapMap {
381     icColorSpaceSignature space;
382     DWORD inForm;
383 };
385 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
387     MapMap possible[] = {
388         {icSigXYZData,   TYPE_XYZ_16},
389         {icSigLabData,   TYPE_Lab_16},
390         //icSigLuvData
391         {icSigYCbCrData, TYPE_YCbCr_16},
392         {icSigYxyData,   TYPE_Yxy_16},
393         {icSigRgbData,   TYPE_RGB_16},
394         {icSigGrayData,  TYPE_GRAY_16},
395         {icSigHsvData,   TYPE_HSV_16},
396         {icSigHlsData,   TYPE_HLS_16},
397         {icSigCmykData,  TYPE_CMYK_16},
398         {icSigCmyData,   TYPE_CMY_16},
399     };
401     int index = 0;
402     for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
403         if ( possible[i].space == space ) {
404             index = i;
405             break;
406         }
407     }
409     return possible[index].inForm;
412 static int getLcmsIntent( guint svgIntent )
414     int intent = INTENT_PERCEPTUAL;
415     switch ( svgIntent ) {
416         case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
417             intent = INTENT_RELATIVE_COLORIMETRIC;
418             break;
419         case Inkscape::RENDERING_INTENT_SATURATION:
420             intent = INTENT_SATURATION;
421             break;
422         case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
423             intent = INTENT_ABSOLUTE_COLORIMETRIC;
424             break;
425         case Inkscape::RENDERING_INTENT_PERCEPTUAL:
426         case Inkscape::RENDERING_INTENT_UNKNOWN:
427         case Inkscape::RENDERING_INTENT_AUTO:
428         default:
429             intent = INTENT_PERCEPTUAL;
430     }
431     return intent;
434 static SPObject* bruteFind( SPDocument* document, gchar const* name )
436     SPObject* result = 0;
437     const GSList * current = sp_document_get_resource_list(document, "iccprofile");
438     while ( current && !result ) {
439         if ( IS_COLORPROFILE(current->data) ) {
440             ColorProfile* prof = COLORPROFILE(current->data);
441             if ( prof ) {
442                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
443                     result = SP_OBJECT(current->data);
444                     break;
445                 }
446             }
447         }
448         current = g_slist_next(current);
449     }
451     return result;
454 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
456     cmsHPROFILE prof = 0;
458     SPObject* thing = bruteFind( document, name );
459     if ( thing ) {
460         prof = COLORPROFILE(thing)->profHandle;
461     }
463     if ( intent ) {
464         *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
465     }
467 #ifdef DEBUG_LCMS
468     DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
469 #endif // DEBUG_LCMS
471     return prof;
474 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
476     if ( !_transf ) {
477         int intent = getLcmsIntent(rendering_intent);
478         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
479     }
480     return _transf;
483 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
485     if ( !_revTransf ) {
486         int intent = getLcmsIntent(rendering_intent);
487         _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
488     }
489     return _revTransf;
493 #include <io/sys.h>
495 class ProfileInfo
497 public:
498     ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
500     Glib::ustring const& getName() {return _name;}
501     Glib::ustring const& getPath() {return _path;}
502     icColorSpaceSignature getSpace() {return _profileSpace;}
503     icProfileClassSignature getClass() {return _profileClass;}
505 private:
506     Glib::ustring _path;
507     Glib::ustring _name;
508     icColorSpaceSignature _profileSpace;
509     icProfileClassSignature _profileClass;
510 };
513 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
515     _path = path;
516     _name = cmsTakeProductDesc(prof);
517     _profileSpace = cmsGetColorSpace( prof );
518     _profileClass = cmsGetDeviceClass( prof );
523 static std::vector<ProfileInfo> knownProfiles;
525 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
527     std::vector<Glib::ustring> result;
529     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
530         if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
531             result.push_back( it->getName() );
532         }
533     }
535     return result;
538 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
540     std::vector<Glib::ustring> result;
542     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
543         if ( it->getClass() == icSigOutputClass ) {
544             result.push_back( it->getName() );
545         }
546     }
548     return result;
551 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
553     Glib::ustring result;
555     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
556         if ( name == it->getName() ) {
557             result = it->getPath();
558             break;
559         }
560     }
562     return result;
565 static void findThings() {
566     std::list<gchar *> sources;
568     gchar* base = profile_path("XXX");
569     {
570         gchar* base2 = g_path_get_dirname(base);
571         g_free(base);
572         base = base2;
573         base2 = g_path_get_dirname(base);
574         g_free(base);
575         base = base2;
576     }
578     // first try user's local dir
579     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
580     sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
582     const gchar* const * dataDirs = g_get_system_data_dirs();
583     for ( int i = 0; dataDirs[i]; i++ ) {
584         sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
585     }
587     while (!sources.empty()) {
588         gchar *dirname = sources.front();
589         if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
590             GError *err = 0;
591             GDir *dir = g_dir_open(dirname, 0, &err);
593             if (dir) {
594                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
595                     gchar *filepath = g_build_filename(dirname, file, NULL);
598                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
599                         sources.push_back(g_strdup(filepath));
600                     } else {
601                         bool isIccFile = false;
602                         struct stat st;
603                         if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
604                             //0-3 == size
605                             //36-39 == 'acsp' 0x61637370
606                             int fd = g_open( filepath, O_RDONLY, S_IRWXU);
607                             if ( fd != -1 ) {
608                                 guchar scratch[40] = {0};
609                                 size_t len = sizeof(scratch);
611                                 //size_t left = 40;
612                                 ssize_t got = read(fd, scratch, len);
613                                 if ( got != -1 ) {
614                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
615                                     if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
616                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
617                                     }
618                                 }
620                                 close(fd);
621                             }
622                         }
624                         if ( isIccFile ) {
625                             cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
626                             if ( prof ) {
627                                 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
628                                 cmsCloseProfile( prof );
630                                 bool sameName = false;
631                                 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
632                                     if ( it->getName() == info.getName() ) {
633                                         sameName = true;
634                                         break;
635                                     }
636                                 }
638                                 if ( !sameName ) {
639                                     knownProfiles.push_back(info);
640                                 }
641                             }
642                         }
643                     }
645                     g_free(filepath);
646                 }
647             }
648         }
650         // toss the dirname
651         g_free(dirname);
652         sources.pop_front();
653     }
656 int errorHandlerCB(int ErrorCode, const char *ErrorText)
658     g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
660     return 1;
663 static bool gamutWarn = false;
664 static Gdk::Color lastGamutColor("#808080");
665 static bool lastBPC = false;
666 #if defined(cmsFLAGS_PRESERVEBLACK)
667 static bool lastPreserveBlack = false;
668 #endif // defined(cmsFLAGS_PRESERVEBLACK)
669 static int lastIntent = INTENT_PERCEPTUAL;
670 static int lastProofIntent = INTENT_PERCEPTUAL;
671 static cmsHTRANSFORM transf = 0;
673 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
675     static cmsHPROFILE theOne = 0;
676     static Glib::ustring lastURI;
678     static bool init = false;
679     if ( !init ) {
680         cmsSetErrorHandler(errorHandlerCB);
682         findThings();
683         init = true;
684     }
686     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
687     Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
689     if ( !uri.empty() ) {
690         if ( uri != lastURI ) {
691             lastURI.clear();
692             if ( theOne ) {
693                 cmsCloseProfile( theOne );
694             }
695             if ( transf ) {
696                 cmsDeleteTransform( transf );
697                 transf = 0;
698             }
699             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
700             if ( theOne ) {
701                 // a display profile must have the proper stuff
702                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
703                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
705                 if ( profClass != icSigDisplayClass ) {
706                     g_warning("Not a display profile");
707                     cmsCloseProfile( theOne );
708                     theOne = 0;
709                 } else if ( space != icSigRgbData ) {
710                     g_warning("Not an RGB profile");
711                     cmsCloseProfile( theOne );
712                     theOne = 0;
713                 } else {
714                     lastURI = uri;
715                 }
716             }
717         }
718     } else if ( theOne ) {
719         cmsCloseProfile( theOne );
720         theOne = 0;
721         lastURI.clear();
722         if ( transf ) {
723             cmsDeleteTransform( transf );
724             transf = 0;
725         }
726     }
728     return theOne;
732 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
734     static cmsHPROFILE theOne = 0;
735     static Glib::ustring lastURI;
737     static bool init = false;
738     if ( !init ) {
739         cmsSetErrorHandler(errorHandlerCB);
741         findThings();
742         init = true;
743     }
745     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
746     bool which = prefs->getBool( "/options/softproof/enable");
747     Glib::ustring uri = prefs->getString("/options/softproof/uri");
749     if ( which && !uri.empty() ) {
750         if ( lastURI != uri ) {
751             lastURI.clear();
752             if ( theOne ) {
753                 cmsCloseProfile( theOne );
754             }
755             if ( transf ) {
756                 cmsDeleteTransform( transf );
757                 transf = 0;
758             }
759             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
760             if ( theOne ) {
761                 // a display profile must have the proper stuff
762                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
763                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
765                 (void)space;
766                 (void)profClass;
767 /*
768                 if ( profClass != icSigDisplayClass ) {
769                     g_warning("Not a display profile");
770                     cmsCloseProfile( theOne );
771                     theOne = 0;
772                 } else if ( space != icSigRgbData ) {
773                     g_warning("Not an RGB profile");
774                     cmsCloseProfile( theOne );
775                     theOne = 0;
776                 } else {
777 */
778                     lastURI = uri;
779 /*
780                 }
781 */
782             }
783         }
784     } else if ( theOne ) {
785         cmsCloseProfile( theOne );
786         theOne = 0;
787         lastURI.clear();
788         if ( transf ) {
789             cmsDeleteTransform( transf );
790             transf = 0;
791         }
792     }
794     return theOne;
797 static void free_transforms();
799 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
801     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
802     bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
803     if ( fromDisplay ) {
804         if ( transf ) {
805             cmsDeleteTransform(transf);
806             transf = 0;
807         }
808         return 0;
809     }
811     bool warn = prefs->getBool( "/options/softproof/gamutwarn");
812     int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
813     int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
814     bool bpc = prefs->getBool( "/options/softproof/bpc");
815 #if defined(cmsFLAGS_PRESERVEBLACK)
816     bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
817 #endif //defined(cmsFLAGS_PRESERVEBLACK)
818     Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
819     Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
821     if ( (warn != gamutWarn)
822          || (lastIntent != intent)
823          || (lastProofIntent != proofIntent)
824          || (bpc != lastBPC)
825 #if defined(cmsFLAGS_PRESERVEBLACK)
826          || (preserveBlack != lastPreserveBlack)
827 #endif // defined(cmsFLAGS_PRESERVEBLACK)
828          || (gamutColor != lastGamutColor)
829         ) {
830         gamutWarn = warn;
831         free_transforms();
832         lastIntent = intent;
833         lastProofIntent = proofIntent;
834         lastBPC = bpc;
835 #if defined(cmsFLAGS_PRESERVEBLACK)
836         lastPreserveBlack = preserveBlack;
837 #endif // defined(cmsFLAGS_PRESERVEBLACK)
838         lastGamutColor = gamutColor;
839     }
841     // Fetch these now, as they might clear the transform as a side effect.
842     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
843     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
845     if ( !transf ) {
846         if ( hprof && proofProf ) {
847             DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
848             if ( gamutWarn ) {
849                 dwFlags |= cmsFLAGS_GAMUTCHECK;
850                 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
851             }
852             if ( bpc ) {
853                 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
854             }
855 #if defined(cmsFLAGS_PRESERVEBLACK)
856             if ( preserveBlack ) {
857                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
858             }
859 #endif // defined(cmsFLAGS_PRESERVEBLACK)
860             transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
861         } else if ( hprof ) {
862             transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
863         }
864     }
866     return transf;
870 class MemProfile {
871 public:
872     MemProfile();
873     ~MemProfile();
875     std::string id;
876     cmsHPROFILE hprof;
877     cmsHTRANSFORM transf;
878 };
880 MemProfile::MemProfile() :
881     id(),
882     hprof(0),
883     transf(0)
887 MemProfile::~MemProfile()
891 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
893 void free_transforms()
895     if ( transf ) {
896         cmsDeleteTransform(transf);
897         transf = 0;
898     }
900     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
901         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
902             if ( it2->transf ) {
903                 cmsDeleteTransform(it2->transf);
904                 it2->transf = 0;
905             }
906         }
907     }
910 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
912     Glib::ustring id;
914     if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
915         std::vector<MemProfile>& row = perMonitorProfiles[screen];
916         if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
917             MemProfile& item = row[monitor];
918             id = item.id;
919         }
920     }
922     return id;
925 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
927     Glib::ustring id;
929     while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
930         std::vector<MemProfile> tmp;
931         perMonitorProfiles.push_back(tmp);
932     }
933     std::vector<MemProfile>& row = perMonitorProfiles[screen];
934     while ( static_cast<int>(row.size()) <= monitor ) {
935         MemProfile tmp;
936         row.push_back(tmp);
937     }
938     MemProfile& item = row[monitor];
940     if ( item.hprof ) {
941         cmsCloseProfile( item.hprof );
942         item.hprof = 0;
943     }
944     id.clear();
946     if ( buf && bufLen ) {
947         id = Digest::hashHex(Digest::HASH_MD5,
948                    reinterpret_cast<unsigned char*>(buf), bufLen);
950         // Note: if this is not a valid profile, item.hprof will be set to null.
951         item.hprof = cmsOpenProfileFromMem(buf, bufLen);
952     }
953     item.id = id;
955     return id;
958 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
960     cmsHTRANSFORM result = 0;
961     if ( id.empty() ) {
962         return 0;
963     }
965     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
966     bool found = false;
967     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
968         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
969             if ( id == it2->id ) {
970                 MemProfile& item = *it2;
972                 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
973                 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
974                 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
975                 bool bpc = prefs->getBool( "/options/softproof/bpc");
976 #if defined(cmsFLAGS_PRESERVEBLACK)
977                 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
978 #endif //defined(cmsFLAGS_PRESERVEBLACK)
979                 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
980                 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
982                 if ( (warn != gamutWarn)
983                      || (lastIntent != intent)
984                      || (lastProofIntent != proofIntent)
985                      || (bpc != lastBPC)
986 #if defined(cmsFLAGS_PRESERVEBLACK)
987                      || (preserveBlack != lastPreserveBlack)
988 #endif // defined(cmsFLAGS_PRESERVEBLACK)
989                      || (gamutColor != lastGamutColor)
990                     ) {
991                     gamutWarn = warn;
992                     free_transforms();
993                     lastIntent = intent;
994                     lastProofIntent = proofIntent;
995                     lastBPC = bpc;
996 #if defined(cmsFLAGS_PRESERVEBLACK)
997                     lastPreserveBlack = preserveBlack;
998 #endif // defined(cmsFLAGS_PRESERVEBLACK)
999                     lastGamutColor = gamutColor;
1000                 }
1002                 // Fetch these now, as they might clear the transform as a side effect.
1003                 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1005                 if ( !item.transf ) {
1006                     if ( item.hprof && proofProf ) {
1007                         DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1008                         if ( gamutWarn ) {
1009                             dwFlags |= cmsFLAGS_GAMUTCHECK;
1010                             cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1011                         }
1012                         if ( bpc ) {
1013                             dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1014                         }
1015 #if defined(cmsFLAGS_PRESERVEBLACK)
1016                         if ( preserveBlack ) {
1017                             dwFlags |= cmsFLAGS_PRESERVEBLACK;
1018                         }
1019 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1020                         item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
1021                     } else if ( item.hprof ) {
1022                         item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
1023                     }
1024                 }
1026                 result = item.transf;
1027                 found = true;
1028             }
1029         }
1030     }
1032     return result;
1037 #endif // ENABLE_LCMS
1039 /*
1040   Local Variables:
1041   mode:c++
1042   c-file-style:"stroustrup"
1043   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1044   indent-tabs-mode:nil
1045   fill-column:99
1046   End:
1047 */
1048 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :