Code

Display CMS adjustment per-desktop view
[inkscape.git] / src / color-profile.cpp
3 #include "xml/repr.h"
4 #include "color-profile.h"
5 #include "color-profile-fns.h"
6 #include "attributes.h"
7 #include "inkscape.h"
8 #include "document.h"
9 #include "prefs-utils.h"
11 #include "dom/uri.h"
13 //#define DEBUG_LCMS
15 #include <glib/gstdio.h>
16 #include <sys/fcntl.h>
17 #include <gdkmm/color.h>
19 #ifdef DEBUG_LCMS
20 #include <gtk/gtkmessagedialog.h>
21 #endif // DEBUG_LCMS
23 using Inkscape::ColorProfile;
24 using Inkscape::ColorProfileClass;
26 namespace Inkscape
27 {
28 #if ENABLE_LCMS
29 static cmsHPROFILE colorprofile_get_system_profile_handle();
30 static cmsHPROFILE colorprofile_get_proof_profile_handle();
31 #endif // ENABLE_LCMS
32 }
34 #ifdef DEBUG_LCMS
35 extern guint update_in_progress;
36 #define DEBUG_MESSAGE(key, ...) \
37 {\
38     gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
39     gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
40     gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
41     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
42     if ( dump )\
43     {\
44         g_message( __VA_ARGS__ );\
45 \
46     }\
47     if ( dumpD )\
48     {\
49         GtkWidget *dialog = gtk_message_dialog_new(NULL,\
50                                                    GTK_DIALOG_DESTROY_WITH_PARENT, \
51                                                    GTK_MESSAGE_INFO,    \
52                                                    GTK_BUTTONS_OK,      \
53                                                    __VA_ARGS__          \
54                                                    );\
55         g_signal_connect_swapped(dialog, "response",\
56                                  G_CALLBACK(gtk_widget_destroy),        \
57                                  dialog);                               \
58         gtk_widget_show_all( dialog );\
59     }\
60 }
61 #endif // DEBUG_LCMS
63 static SPObjectClass *cprof_parent_class;
65 #if ENABLE_LCMS
67 cmsHPROFILE ColorProfile::_sRGBProf = 0;
69 cmsHPROFILE ColorProfile::getSRGBProfile() {
70     if ( !_sRGBProf ) {
71         _sRGBProf = cmsCreate_sRGBProfile();
72     }
73     return _sRGBProf;
74 }
76 #endif // ENABLE_LCMS
78 /**
79  * Register ColorProfile class and return its type.
80  */
81 GType Inkscape::colorprofile_get_type()
82 {
83     return ColorProfile::getType();
84 }
86 GType ColorProfile::getType()
87 {
88     static GType type = 0;
89     if (!type) {
90         GTypeInfo info = {
91             sizeof(ColorProfileClass),
92             NULL, NULL,
93             (GClassInitFunc) ColorProfile::classInit,
94             NULL, NULL,
95             sizeof(ColorProfile),
96             16,
97             (GInstanceInitFunc) ColorProfile::init,
98             NULL,   /* value_table */
99         };
100         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
101     }
102     return type;
105 /**
106  * ColorProfile vtable initialization.
107  */
108 void ColorProfile::classInit( ColorProfileClass *klass )
110     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
112     cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
114     sp_object_class->release = ColorProfile::release;
115     sp_object_class->build = ColorProfile::build;
116     sp_object_class->set = ColorProfile::set;
117     sp_object_class->write = ColorProfile::write;
120 /**
121  * Callback for ColorProfile object initialization.
122  */
123 void ColorProfile::init( ColorProfile *cprof )
125     cprof->href = 0;
126     cprof->local = 0;
127     cprof->name = 0;
128     cprof->intentStr = 0;
129     cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
130 #if ENABLE_LCMS
131     cprof->profHandle = 0;
132     cprof->_profileClass = icSigInputClass;
133     cprof->_profileSpace = icSigRgbData;
134     cprof->_transf = 0;
135     cprof->_revTransf = 0;
136 #endif // ENABLE_LCMS
139 /**
140  * Callback: free object
141  */
142 void ColorProfile::release( SPObject *object )
144     // Unregister ourselves
145     SPDocument* document = SP_OBJECT_DOCUMENT(object);
146     if ( document ) {
147         sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
148     }
150     ColorProfile *cprof = COLORPROFILE(object);
151     if ( cprof->href ) {
152         g_free( cprof->href );
153         cprof->href = 0;
154     }
156     if ( cprof->local ) {
157         g_free( cprof->local );
158         cprof->local = 0;
159     }
161     if ( cprof->name ) {
162         g_free( cprof->name );
163         cprof->name = 0;
164     }
166     if ( cprof->intentStr ) {
167         g_free( cprof->intentStr );
168         cprof->intentStr = 0;
169     }
171 #if ENABLE_LCMS
172     cprof->_clearProfile();
173 #endif // ENABLE_LCMS
176 #if ENABLE_LCMS
177 void ColorProfile::_clearProfile()
179     _profileSpace = icSigRgbData;
181     if ( _transf ) {
182         cmsDeleteTransform( _transf );
183         _transf = 0;
184     }
185     if ( _revTransf ) {
186         cmsDeleteTransform( _revTransf );
187         _revTransf = 0;
188     }
189     if ( profHandle ) {
190         cmsCloseProfile( profHandle );
191         profHandle = 0;
192     }
194 #endif // ENABLE_LCMS
196 /**
197  * Callback: set attributes from associated repr.
198  */
199 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
201     ColorProfile *cprof = COLORPROFILE(object);
202     g_assert(cprof->href == 0);
203     g_assert(cprof->local == 0);
204     g_assert(cprof->name == 0);
205     g_assert(cprof->intentStr == 0);
207     if (cprof_parent_class->build) {
208         (* cprof_parent_class->build)(object, document, repr);
209     }
210     sp_object_read_attr( object, "xlink:href" );
211     sp_object_read_attr( object, "local" );
212     sp_object_read_attr( object, "name" );
213     sp_object_read_attr( object, "rendering-intent" );
215     // Register
216     if ( document ) {
217         sp_document_add_resource( document, "iccprofile", object );
218     }
221 /**
222  * Callback: set attribute.
223  */
224 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
226     ColorProfile *cprof = COLORPROFILE(object);
228     switch (key) {
229         case SP_ATTR_XLINK_HREF:
230             if ( cprof->href ) {
231                 g_free( cprof->href );
232                 cprof->href = 0;
233             }
234             if ( value ) {
235                 cprof->href = g_strdup( value );
236                 if ( *cprof->href ) {
237 #if ENABLE_LCMS
238                     cmsErrorAction( LCMS_ERROR_SHOW );
240                     // TODO open filename and URIs properly
241                     //FILE* fp = fopen_utf8name( filename, "r" );
242                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
244                     // Try to open relative
245                     SPDocument *doc = SP_OBJECT_DOCUMENT(object);
246                     if (!doc) {
247                         doc = SP_ACTIVE_DOCUMENT;
248                         g_warning("object has no document.  using active");
249                     }
250                     //# 1.  Get complete URI of document
251                     gchar const *docbase = SP_DOCUMENT_URI( doc );
252                     if (!docbase)
253                         {
254                         g_warning("null docbase");
255                         docbase = "";
256                         }
257                     //g_message("docbase:%s\n", docbase);
258                     org::w3c::dom::URI docUri(docbase);
259                     //# 2. Get href of icc file.  we don't care if it's rel or abs
260                     org::w3c::dom::URI hrefUri(cprof->href);
261                     //# 3.  Resolve the href according the docBase.  This follows
262                     //      the w3c specs.  All absolute and relative issues are considered
263                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
264                     gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
265                     cprof->_clearProfile();
266                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
267                     if ( cprof->profHandle ) {
268                         cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
269                         cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
270                     }
271 #ifdef DEBUG_LCMS
272                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
273 #endif // DEBUG_LCMS
275 #endif // ENABLE_LCMS
276                 }
277             }
278             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
279             break;
281         case SP_ATTR_LOCAL:
282             if ( cprof->local ) {
283                 g_free( cprof->local );
284                 cprof->local = 0;
285             }
286             cprof->local = g_strdup( value );
287             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
288             break;
290         case SP_ATTR_NAME:
291             if ( cprof->name ) {
292                 g_free( cprof->name );
293                 cprof->name = 0;
294             }
295             cprof->name = g_strdup( value );
296 #ifdef DEBUG_LCMS
297             DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
298 #endif // DEBUG_LCMS
299             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
300             break;
302         case SP_ATTR_RENDERING_INTENT:
303             if ( cprof->intentStr ) {
304                 g_free( cprof->intentStr );
305                 cprof->intentStr = 0;
306             }
307             cprof->intentStr = g_strdup( value );
309             if ( value ) {
310                 if ( strcmp( value, "auto" ) == 0 ) {
311                     cprof->rendering_intent = RENDERING_INTENT_AUTO;
312                 } else if ( strcmp( value, "perceptual" ) == 0 ) {
313                     cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
314                 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
315                     cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
316                 } else if ( strcmp( value, "saturation" ) == 0 ) {
317                     cprof->rendering_intent = RENDERING_INTENT_SATURATION;
318                 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
319                     cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
320                 } else {
321                     cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
322                 }
323             } else {
324                 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
325             }
327             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
328             break;
330         default:
331             if (cprof_parent_class->set) {
332                 (* cprof_parent_class->set)(object, key, value);
333             }
334             break;
335     }
339 /**
340  * Callback: write attributes to associated repr.
341  */
342 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
344     ColorProfile *cprof = COLORPROFILE(object);
346     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
347         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
348         repr = xml_doc->createElement("svg:color-profile");
349     }
351     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
352         repr->setAttribute( "xlink:href", cprof->href );
353     }
355     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
356         repr->setAttribute( "local", cprof->local );
357     }
359     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
360         repr->setAttribute( "name", cprof->name );
361     }
363     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
364         repr->setAttribute( "rendering-intent", cprof->intentStr );
365     }
367     if (cprof_parent_class->write) {
368         (* cprof_parent_class->write)(object, repr, flags);
369     }
371     return repr;
375 #if ENABLE_LCMS
377 struct MapMap {
378     icColorSpaceSignature space;
379     DWORD inForm;
380 };
382 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
384     MapMap possible[] = {
385         {icSigXYZData,   TYPE_XYZ_16},
386         {icSigLabData,   TYPE_Lab_16},
387         //icSigLuvData
388         {icSigYCbCrData, TYPE_YCbCr_16},
389         {icSigYxyData,   TYPE_Yxy_16},
390         {icSigRgbData,   TYPE_RGB_16},
391         {icSigGrayData,  TYPE_GRAY_16},
392         {icSigHsvData,   TYPE_HSV_16},
393         {icSigHlsData,   TYPE_HLS_16},
394         {icSigCmykData,  TYPE_CMYK_16},
395         {icSigCmyData,   TYPE_CMY_16},
396     };
398     int index = 0;
399     for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
400         if ( possible[i].space == space ) {
401             index = i;
402             break;
403         }
404     }
406     return possible[index].inForm;
409 static int getLcmsIntent( guint svgIntent )
411     int intent = INTENT_PERCEPTUAL;
412     switch ( svgIntent ) {
413         case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
414             intent = INTENT_RELATIVE_COLORIMETRIC;
415             break;
416         case Inkscape::RENDERING_INTENT_SATURATION:
417             intent = INTENT_SATURATION;
418             break;
419         case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
420             intent = INTENT_ABSOLUTE_COLORIMETRIC;
421             break;
422         case Inkscape::RENDERING_INTENT_PERCEPTUAL:
423         case Inkscape::RENDERING_INTENT_UNKNOWN:
424         case Inkscape::RENDERING_INTENT_AUTO:
425         default:
426             intent = INTENT_PERCEPTUAL;
427     }
428     return intent;
431 static SPObject* bruteFind( SPDocument* document, gchar const* name )
433     SPObject* result = 0;
434     const GSList * current = sp_document_get_resource_list(document, "iccprofile");
435     while ( current && !result ) {
436         if ( IS_COLORPROFILE(current->data) ) {
437             ColorProfile* prof = COLORPROFILE(current->data);
438             if ( prof ) {
439                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
440                     result = SP_OBJECT(current->data);
441                     break;
442                 }
443             }
444         }
445         current = g_slist_next(current);
446     }
448     return result;
451 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
453     cmsHPROFILE prof = 0;
455     SPObject* thing = bruteFind( document, name );
456     if ( thing ) {
457         prof = COLORPROFILE(thing)->profHandle;
458     }
460     if ( intent ) {
461         *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
462     }
464 #ifdef DEBUG_LCMS
465     DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
466 #endif // DEBUG_LCMS
468     return prof;
471 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
473     if ( !_transf ) {
474         int intent = getLcmsIntent(rendering_intent);
475         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
476     }
477     return _transf;
480 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
482     if ( !_revTransf ) {
483         int intent = getLcmsIntent(rendering_intent);
484         _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
485     }
486     return _revTransf;
490 #include <io/sys.h>
492 class ProfileInfo
494 public:
495     ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
497     Glib::ustring const& getName() {return _name;}
498     Glib::ustring const& getPath() {return _path;}
499     icColorSpaceSignature getSpace() {return _profileSpace;}
500     icProfileClassSignature getClass() {return _profileClass;}
502 private:
503     Glib::ustring _path;
504     Glib::ustring _name;
505     icColorSpaceSignature _profileSpace;
506     icProfileClassSignature _profileClass;
507 };
510 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
512     _path = path;
513     _name = cmsTakeProductDesc(prof);
514     _profileSpace = cmsGetColorSpace( prof );
515     _profileClass = cmsGetDeviceClass( prof );
520 static std::vector<ProfileInfo> knownProfiles;
522 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
524     std::vector<Glib::ustring> result;
526     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
527         if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
528             result.push_back( it->getName() );
529         }
530     }
532     return result;
535 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
537     std::vector<Glib::ustring> result;
539     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
540         if ( it->getClass() == icSigOutputClass ) {
541             result.push_back( it->getName() );
542         }
543     }
545     return result;
548 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
550     Glib::ustring result;
552     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
553         if ( name == it->getName() ) {
554             result = it->getPath();
555             break;
556         }
557     }
559     return result;
562 static void findThings() {
563     std::list<gchar *> sources;
565     gchar* base = profile_path("XXX");
566     {
567         gchar* base2 = g_path_get_dirname(base);
568         g_free(base);
569         base = base2;
570         base2 = g_path_get_dirname(base);
571         g_free(base);
572         base = base2;
573     }
575     // first try user's local dir
576     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
577     sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
579     const gchar* const * dataDirs = g_get_system_data_dirs();
580     for ( int i = 0; dataDirs[i]; i++ ) {
581         sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
582     }
584     while (!sources.empty()) {
585         gchar *dirname = sources.front();
586         if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
587             GError *err = 0;
588             GDir *dir = g_dir_open(dirname, 0, &err);
590             if (dir) {
591                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
592                     gchar *filepath = g_build_filename(dirname, file, NULL);
595                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
596                         sources.push_back(g_strdup(filepath));
597                     } else {
598                         bool isIccFile = false;
599                         struct stat st;
600                         if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
601                             //0-3 == size
602                             //36-39 == 'acsp' 0x61637370
603                             int fd = g_open( filepath, O_RDONLY, S_IRWXU);
604                             if ( fd != -1 ) {
605                                 guchar scratch[40] = {0};
606                                 size_t len = sizeof(scratch);
608                                 //size_t left = 40;
609                                 ssize_t got = read(fd, scratch, len);
610                                 if ( got != -1 ) {
611                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
612                                     if ( calcSize > 128 && calcSize <= st.st_size ) {
613                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
614                                     }
615                                 }
617                                 close(fd);
618                             }
619                         }
621                         if ( isIccFile ) {
622                             cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
623                             if ( prof ) {
624                                 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
625                                 cmsCloseProfile( prof );
627                                 bool sameName = false;
628                                 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
629                                     if ( it->getName() == info.getName() ) {
630                                         sameName = true;
631                                         break;
632                                     }
633                                 }
635                                 if ( !sameName ) {
636                                     knownProfiles.push_back(info);
637                                 }
638                             }
639                         }
640                     }
642                     g_free(filepath);
643                 }
644             }
645         }
647         // toss the dirname
648         g_free(dirname);
649         sources.pop_front();
650     }
653 int errorHandlerCB(int ErrorCode, const char *ErrorText)
655     g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
657     return 1;
660 static bool gamutWarn = false;
661 static Gdk::Color lastGamutColor("#808080");
662 static bool lastBPC = false;
663 #if defined(cmsFLAGS_PRESERVEBLACK)
664 static bool lastPreserveBlack = false;
665 #endif // defined(cmsFLAGS_PRESERVEBLACK)
666 static int lastIntent = INTENT_PERCEPTUAL;
667 static int lastProofIntent = INTENT_PERCEPTUAL;
668 static cmsHTRANSFORM transf = 0;
670 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
672     static cmsHPROFILE theOne = 0;
673     static std::string lastURI;
675     static bool init = false;
676     if ( !init ) {
677         cmsSetErrorHandler(errorHandlerCB);
679         findThings();
680         init = true;
681     }
683     gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
685     if ( uri && *uri ) {
686         if ( lastURI != std::string(uri) ) {
687             lastURI.clear();
688             if ( theOne ) {
689                 cmsCloseProfile( theOne );
690             }
691             if ( transf ) {
692                 cmsDeleteTransform( transf );
693                 transf = 0;
694             }
695             theOne = cmsOpenProfileFromFile( uri, "r" );
696             if ( theOne ) {
697                 // a display profile must have the proper stuff
698                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
699                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
701                 if ( profClass != icSigDisplayClass ) {
702                     g_warning("Not a display profile");
703                     cmsCloseProfile( theOne );
704                     theOne = 0;
705                 } else if ( space != icSigRgbData ) {
706                     g_warning("Not an RGB profile");
707                     cmsCloseProfile( theOne );
708                     theOne = 0;
709                 } else {
710                     lastURI = uri;
711                 }
712             }
713         }
714     } else if ( theOne ) {
715         cmsCloseProfile( theOne );
716         theOne = 0;
717         lastURI.clear();
718         if ( transf ) {
719             cmsDeleteTransform( transf );
720             transf = 0;
721         }
722     }
724     return theOne;
728 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
730     static cmsHPROFILE theOne = 0;
731     static std::string lastURI;
733     static bool init = false;
734     if ( !init ) {
735         cmsSetErrorHandler(errorHandlerCB);
737         findThings();
738         init = true;
739     }
741     long long int which = prefs_get_int_attribute_limited( "options.softproof", "enable", 0, 0, 1 );
742     gchar const * uri = prefs_get_string_attribute("options.softproof", "uri");
744     if ( which && uri && *uri ) {
745         if ( lastURI != std::string(uri) ) {
746             lastURI.clear();
747             if ( theOne ) {
748                 cmsCloseProfile( theOne );
749             }
750             if ( transf ) {
751                 cmsDeleteTransform( transf );
752                 transf = 0;
753             }
754             theOne = cmsOpenProfileFromFile( uri, "r" );
755             if ( theOne ) {
756                 // a display profile must have the proper stuff
757                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
758                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
760                 (void)space;
761                 (void)profClass;
762 /*
763                 if ( profClass != icSigDisplayClass ) {
764                     g_warning("Not a display profile");
765                     cmsCloseProfile( theOne );
766                     theOne = 0;
767                 } else if ( space != icSigRgbData ) {
768                     g_warning("Not an RGB profile");
769                     cmsCloseProfile( theOne );
770                     theOne = 0;
771                 } else {
772 */
773                     lastURI = uri;
774 /*
775                 }
776 */
777             }
778         }
779     } else if ( theOne ) {
780         cmsCloseProfile( theOne );
781         theOne = 0;
782         lastURI.clear();
783         if ( transf ) {
784             cmsDeleteTransform( transf );
785             transf = 0;
786         }
787     }
789     return theOne;
792 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
794     bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
795     int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
796     int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
797     bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
798 #if defined(cmsFLAGS_PRESERVEBLACK)
799     bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
800 #endif //defined(cmsFLAGS_PRESERVEBLACK)
801     gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
802     Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
804     if ( (warn != gamutWarn)
805          || (lastIntent != intent)
806          || (lastProofIntent != proofIntent)
807          || (bpc != lastBPC)
808 #if defined(cmsFLAGS_PRESERVEBLACK)
809          || (preserveBlack != lastPreserveBlack)
810 #endif // defined(cmsFLAGS_PRESERVEBLACK)
811          || (gamutColor != lastGamutColor)
812         ) {
813         gamutWarn = warn;
814         if ( transf ) {
815             cmsDeleteTransform(transf);
816             transf = 0;
817         }
818         lastIntent = intent;
819         lastProofIntent = proofIntent;
820         lastBPC = bpc;
821 #if defined(cmsFLAGS_PRESERVEBLACK)
822         lastPreserveBlack = preserveBlack;
823 #endif // defined(cmsFLAGS_PRESERVEBLACK)
824         lastGamutColor = gamutColor;
825     }
827     // Fetch these now, as they might clear the transform as a side effect.
828     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
829     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
831     if ( !transf ) {
832         if ( hprof && proofProf ) {
833             DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
834             if ( gamutWarn ) {
835                 dwFlags |= cmsFLAGS_GAMUTCHECK;
836                 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
837             }
838             if ( bpc ) {
839                 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
840             }
841 #if defined(cmsFLAGS_PRESERVEBLACK)
842             if ( preserveBlack ) {
843                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
844             }
845 #endif // defined(cmsFLAGS_PRESERVEBLACK)
846             transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
847         } else if ( hprof ) {
848             transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
849         }
850     }
852     return transf;
855 #endif // ENABLE_LCMS
857 /*
858   Local Variables:
859   mode:c++
860   c-file-style:"stroustrup"
861   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
862   indent-tabs-mode:nil
863   fill-column:99
864   End:
865 */
866 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :