Code

enabling all icc-color related debug messages
[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_SCISLAC(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 }
67 #define DEBUG_MESSAGE(key, ...)\
68 {\
69     g_message( __VA_ARGS__ );\
70 }
72 #endif // DEBUG_LCMS
74 static SPObjectClass *cprof_parent_class;
76 #if ENABLE_LCMS
78 cmsHPROFILE ColorProfile::_sRGBProf = 0;
80 cmsHPROFILE ColorProfile::getSRGBProfile() {
81     if ( !_sRGBProf ) {
82         _sRGBProf = cmsCreate_sRGBProfile();
83     }
84     return _sRGBProf;
85 }
87 #endif // ENABLE_LCMS
89 /**
90  * Register ColorProfile class and return its type.
91  */
92 GType Inkscape::colorprofile_get_type()
93 {
94     return ColorProfile::getType();
95 }
97 GType ColorProfile::getType()
98 {
99     static GType type = 0;
100     if (!type) {
101         GTypeInfo info = {
102             sizeof(ColorProfileClass),
103             NULL, NULL,
104             (GClassInitFunc) ColorProfile::classInit,
105             NULL, NULL,
106             sizeof(ColorProfile),
107             16,
108             (GInstanceInitFunc) ColorProfile::init,
109             NULL,   /* value_table */
110         };
111         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
112     }
113     return type;
116 /**
117  * ColorProfile vtable initialization.
118  */
119 void ColorProfile::classInit( ColorProfileClass *klass )
121     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
123     cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
125     sp_object_class->release = ColorProfile::release;
126     sp_object_class->build = ColorProfile::build;
127     sp_object_class->set = ColorProfile::set;
128     sp_object_class->write = ColorProfile::write;
131 /**
132  * Callback for ColorProfile object initialization.
133  */
134 void ColorProfile::init( ColorProfile *cprof )
136     cprof->href = 0;
137     cprof->local = 0;
138     cprof->name = 0;
139     cprof->intentStr = 0;
140     cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
141 #if ENABLE_LCMS
142     cprof->profHandle = 0;
143     cprof->_profileClass = icSigInputClass;
144     cprof->_profileSpace = icSigRgbData;
145     cprof->_transf = 0;
146     cprof->_revTransf = 0;
147 #endif // ENABLE_LCMS
150 /**
151  * Callback: free object
152  */
153 void ColorProfile::release( SPObject *object )
155     // Unregister ourselves
156     SPDocument* document = SP_OBJECT_DOCUMENT(object);
157     if ( document ) {
158         sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
159     }
161     ColorProfile *cprof = COLORPROFILE(object);
162     if ( cprof->href ) {
163         g_free( cprof->href );
164         cprof->href = 0;
165     }
167     if ( cprof->local ) {
168         g_free( cprof->local );
169         cprof->local = 0;
170     }
172     if ( cprof->name ) {
173         g_free( cprof->name );
174         cprof->name = 0;
175     }
177     if ( cprof->intentStr ) {
178         g_free( cprof->intentStr );
179         cprof->intentStr = 0;
180     }
182 #if ENABLE_LCMS
183     cprof->_clearProfile();
184 #endif // ENABLE_LCMS
187 #if ENABLE_LCMS
188 void ColorProfile::_clearProfile()
190     _profileSpace = icSigRgbData;
192     if ( _transf ) {
193         cmsDeleteTransform( _transf );
194         _transf = 0;
195     }
196     if ( _revTransf ) {
197         cmsDeleteTransform( _revTransf );
198         _revTransf = 0;
199     }
200     if ( profHandle ) {
201         cmsCloseProfile( profHandle );
202         profHandle = 0;
203     }
205 #endif // ENABLE_LCMS
207 /**
208  * Callback: set attributes from associated repr.
209  */
210 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
212     ColorProfile *cprof = COLORPROFILE(object);
213     g_assert(cprof->href == 0);
214     g_assert(cprof->local == 0);
215     g_assert(cprof->name == 0);
216     g_assert(cprof->intentStr == 0);
218     if (cprof_parent_class->build) {
219         (* cprof_parent_class->build)(object, document, repr);
220     }
221     sp_object_read_attr( object, "xlink:href" );
222     sp_object_read_attr( object, "local" );
223     sp_object_read_attr( object, "name" );
224     sp_object_read_attr( object, "rendering-intent" );
226     // Register
227     if ( document ) {
228         sp_document_add_resource( document, "iccprofile", object );
229     }
232 /**
233  * Callback: set attribute.
234  */
235 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
237     ColorProfile *cprof = COLORPROFILE(object);
239     switch (key) {
240         case SP_ATTR_XLINK_HREF:
241             if ( cprof->href ) {
242                 g_free( cprof->href );
243                 cprof->href = 0;
244             }
245             if ( value ) {
246                 cprof->href = g_strdup( value );
247                 if ( *cprof->href ) {
248 #if ENABLE_LCMS
249                     cmsErrorAction( LCMS_ERROR_SHOW );
251                     // TODO open filename and URIs properly
252                     //FILE* fp = fopen_utf8name( filename, "r" );
253                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
255                     // Try to open relative
256                     SPDocument *doc = SP_OBJECT_DOCUMENT(object);
257                     if (!doc) {
258                         doc = SP_ACTIVE_DOCUMENT;
259                         g_warning("object has no document.  using active");
260                     }
261                     //# 1.  Get complete URI of document
262                     gchar const *docbase = SP_DOCUMENT_URI( doc );
263                     if (!docbase)
264                     {
265                         // Normal for files that have not yet been saved.
266                         docbase = "";
267                     }
268                     //g_message("docbase:%s\n", docbase);
269                     org::w3c::dom::URI docUri(docbase);
270                     //# 2. Get href of icc file.  we don't care if it's rel or abs
271                     org::w3c::dom::URI hrefUri(cprof->href);
272                     //# 3.  Resolve the href according the docBase.  This follows
273                     //      the w3c specs.  All absolute and relative issues are considered
274                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
275                     gchar* fullname = g_strdup((gchar *)cprofUri.getNativePath().c_str());
276                     cprof->_clearProfile();
277                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
278                     if ( cprof->profHandle ) {
279                         cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
280                         cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
281                     }
282 #ifdef DEBUG_LCMS
283                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
284 #endif // DEBUG_LCMS
285                     g_free(fullname);
286 #endif // ENABLE_LCMS
287                 }
288             }
289             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
290             break;
292         case SP_ATTR_LOCAL:
293             if ( cprof->local ) {
294                 g_free( cprof->local );
295                 cprof->local = 0;
296             }
297             cprof->local = g_strdup( value );
298             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
299             break;
301         case SP_ATTR_NAME:
302             if ( cprof->name ) {
303                 g_free( cprof->name );
304                 cprof->name = 0;
305             }
306             cprof->name = g_strdup( value );
307 #ifdef DEBUG_LCMS
308             DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
309 #endif // DEBUG_LCMS
310             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
311             break;
313         case SP_ATTR_RENDERING_INTENT:
314             if ( cprof->intentStr ) {
315                 g_free( cprof->intentStr );
316                 cprof->intentStr = 0;
317             }
318             cprof->intentStr = g_strdup( value );
320             if ( value ) {
321                 if ( strcmp( value, "auto" ) == 0 ) {
322                     cprof->rendering_intent = RENDERING_INTENT_AUTO;
323                 } else if ( strcmp( value, "perceptual" ) == 0 ) {
324                     cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
325                 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
326                     cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
327                 } else if ( strcmp( value, "saturation" ) == 0 ) {
328                     cprof->rendering_intent = RENDERING_INTENT_SATURATION;
329                 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
330                     cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
331                 } else {
332                     cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
333                 }
334             } else {
335                 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
336             }
338             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
339             break;
341         default:
342             if (cprof_parent_class->set) {
343                 (* cprof_parent_class->set)(object, key, value);
344             }
345             break;
346     }
350 /**
351  * Callback: write attributes to associated repr.
352  */
353 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
355     ColorProfile *cprof = COLORPROFILE(object);
357     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
358         repr = xml_doc->createElement("svg:color-profile");
359     }
361     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
362         repr->setAttribute( "xlink:href", cprof->href );
363     }
365     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
366         repr->setAttribute( "local", cprof->local );
367     }
369     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
370         repr->setAttribute( "name", cprof->name );
371     }
373     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
374         repr->setAttribute( "rendering-intent", cprof->intentStr );
375     }
377     if (cprof_parent_class->write) {
378         (* cprof_parent_class->write)(object, xml_doc, repr, flags);
379     }
381     return repr;
385 #if ENABLE_LCMS
387 struct MapMap {
388     icColorSpaceSignature space;
389     DWORD inForm;
390 };
392 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
394     MapMap possible[] = {
395         {icSigXYZData,   TYPE_XYZ_16},
396         {icSigLabData,   TYPE_Lab_16},
397         //icSigLuvData
398         {icSigYCbCrData, TYPE_YCbCr_16},
399         {icSigYxyData,   TYPE_Yxy_16},
400         {icSigRgbData,   TYPE_RGB_16},
401         {icSigGrayData,  TYPE_GRAY_16},
402         {icSigHsvData,   TYPE_HSV_16},
403         {icSigHlsData,   TYPE_HLS_16},
404         {icSigCmykData,  TYPE_CMYK_16},
405         {icSigCmyData,   TYPE_CMY_16},
406     };
408     int index = 0;
409     for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
410         if ( possible[i].space == space ) {
411             index = i;
412             break;
413         }
414     }
416     return possible[index].inForm;
419 static int getLcmsIntent( guint svgIntent )
421     int intent = INTENT_PERCEPTUAL;
422     switch ( svgIntent ) {
423         case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
424             intent = INTENT_RELATIVE_COLORIMETRIC;
425             break;
426         case Inkscape::RENDERING_INTENT_SATURATION:
427             intent = INTENT_SATURATION;
428             break;
429         case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
430             intent = INTENT_ABSOLUTE_COLORIMETRIC;
431             break;
432         case Inkscape::RENDERING_INTENT_PERCEPTUAL:
433         case Inkscape::RENDERING_INTENT_UNKNOWN:
434         case Inkscape::RENDERING_INTENT_AUTO:
435         default:
436             intent = INTENT_PERCEPTUAL;
437     }
438     return intent;
441 static SPObject* bruteFind( SPDocument* document, gchar const* name )
443     SPObject* result = 0;
444     const GSList * current = sp_document_get_resource_list(document, "iccprofile");
445     while ( current && !result ) {
446         if ( IS_COLORPROFILE(current->data) ) {
447             ColorProfile* prof = COLORPROFILE(current->data);
448             if ( prof ) {
449                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
450                     result = SP_OBJECT(current->data);
451                     break;
452                 }
453             }
454         }
455         current = g_slist_next(current);
456     }
458     return result;
461 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
463     cmsHPROFILE prof = 0;
465     SPObject* thing = bruteFind( document, name );
466     if ( thing ) {
467         prof = COLORPROFILE(thing)->profHandle;
468     }
470     if ( intent ) {
471         *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
472     }
474 #ifdef DEBUG_LCMS
475     DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
476 #endif // DEBUG_LCMS
478     return prof;
481 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
483     if ( !_transf ) {
484         int intent = getLcmsIntent(rendering_intent);
485         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
486     }
487     return _transf;
490 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
492     if ( !_revTransf ) {
493         int intent = getLcmsIntent(rendering_intent);
494         _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
495     }
496     return _revTransf;
500 #include <io/sys.h>
502 class ProfileInfo
504 public:
505     ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
507     Glib::ustring const& getName() {return _name;}
508     Glib::ustring const& getPath() {return _path;}
509     icColorSpaceSignature getSpace() {return _profileSpace;}
510     icProfileClassSignature getClass() {return _profileClass;}
512 private:
513     Glib::ustring _path;
514     Glib::ustring _name;
515     icColorSpaceSignature _profileSpace;
516     icProfileClassSignature _profileClass;
517 };
520 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
522     _path = path;
523     _name = cmsTakeProductDesc(prof);
524     _profileSpace = cmsGetColorSpace( prof );
525     _profileClass = cmsGetDeviceClass( prof );
530 static std::vector<ProfileInfo> knownProfiles;
532 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
534     std::vector<Glib::ustring> result;
536     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
537         if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
538             result.push_back( it->getName() );
539         }
540     }
542     return result;
545 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
547     std::vector<Glib::ustring> result;
549     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
550         if ( it->getClass() == icSigOutputClass ) {
551             result.push_back( it->getName() );
552         }
553     }
555     return result;
558 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
560     Glib::ustring result;
562     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
563         if ( name == it->getName() ) {
564             result = it->getPath();
565             break;
566         }
567     }
569     return result;
571 #endif // ENABLE_LCMS
573 std::list<Glib::ustring> ColorProfile::getProfileDirs() {
574     std::list<Glib::ustring> sources;
576     gchar* base = profile_path("XXX");
577     {
578         gchar* base2 = g_path_get_dirname(base);
579         g_free(base);
580         base = base2;
581         base2 = g_path_get_dirname(base);
582         g_free(base);
583         base = base2;
584     }
586     // first try user's local dir
587     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
588     sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
590     const gchar* const * dataDirs = g_get_system_data_dirs();
591     for ( int i = 0; dataDirs[i]; i++ ) {
592         sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
593     }
595     return sources;
598 #if ENABLE_LCMS
599 static void findThings() {
600     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
602     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
603         if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
604             GError *err = 0;
605             GDir *dir = g_dir_open(it->c_str(), 0, &err);
607             if (dir) {
608                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
609                     gchar *filepath = g_build_filename(it->c_str(), file, NULL);
612                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
613                         sources.push_back(g_strdup(filepath));
614                     } else {
615                         bool isIccFile = false;
616                         struct stat st;
617                         if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
618                             //0-3 == size
619                             //36-39 == 'acsp' 0x61637370
620                             int fd = g_open( filepath, O_RDONLY, S_IRWXU);
621                             if ( fd != -1 ) {
622                                 guchar scratch[40] = {0};
623                                 size_t len = sizeof(scratch);
625                                 //size_t left = 40;
626                                 ssize_t got = read(fd, scratch, len);
627                                 if ( got != -1 ) {
628                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
629                                     if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
630                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
631                                     }
632                                 }
634                                 close(fd);
635                             }
636                         }
638                         if ( isIccFile ) {
639                             cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
640                             if ( prof ) {
641                                 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
642                                 cmsCloseProfile( prof );
644                                 bool sameName = false;
645                                 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
646                                     if ( it->getName() == info.getName() ) {
647                                         sameName = true;
648                                         break;
649                                     }
650                                 }
652                                 if ( !sameName ) {
653                                     knownProfiles.push_back(info);
654                                 }
655                             }
656                         }
657                     }
659                     g_free(filepath);
660                 }
661             }
662         }
663     }
666 int errorHandlerCB(int ErrorCode, const char *ErrorText)
668     g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
670     return 1;
673 static bool gamutWarn = false;
674 static Gdk::Color lastGamutColor("#808080");
675 static bool lastBPC = false;
676 #if defined(cmsFLAGS_PRESERVEBLACK)
677 static bool lastPreserveBlack = false;
678 #endif // defined(cmsFLAGS_PRESERVEBLACK)
679 static int lastIntent = INTENT_PERCEPTUAL;
680 static int lastProofIntent = INTENT_PERCEPTUAL;
681 static cmsHTRANSFORM transf = 0;
683 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
685     static cmsHPROFILE theOne = 0;
686     static Glib::ustring lastURI;
688     static bool init = false;
689     if ( !init ) {
690         cmsSetErrorHandler(errorHandlerCB);
692         findThings();
693         init = true;
694     }
696     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
697     Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
699     if ( !uri.empty() ) {
700         if ( uri != lastURI ) {
701             lastURI.clear();
702             if ( theOne ) {
703                 cmsCloseProfile( theOne );
704             }
705             if ( transf ) {
706                 cmsDeleteTransform( transf );
707                 transf = 0;
708             }
709             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
710             if ( theOne ) {
711                 // a display profile must have the proper stuff
712                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
713                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
715                 if ( profClass != icSigDisplayClass ) {
716                     g_warning("Not a display profile");
717                     cmsCloseProfile( theOne );
718                     theOne = 0;
719                 } else if ( space != icSigRgbData ) {
720                     g_warning("Not an RGB profile");
721                     cmsCloseProfile( theOne );
722                     theOne = 0;
723                 } else {
724                     lastURI = uri;
725                 }
726             }
727         }
728     } else if ( theOne ) {
729         cmsCloseProfile( theOne );
730         theOne = 0;
731         lastURI.clear();
732         if ( transf ) {
733             cmsDeleteTransform( transf );
734             transf = 0;
735         }
736     }
738     return theOne;
742 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
744     static cmsHPROFILE theOne = 0;
745     static Glib::ustring lastURI;
747     static bool init = false;
748     if ( !init ) {
749         cmsSetErrorHandler(errorHandlerCB);
751         findThings();
752         init = true;
753     }
755     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
756     bool which = prefs->getBool( "/options/softproof/enable");
757     Glib::ustring uri = prefs->getString("/options/softproof/uri");
759     if ( which && !uri.empty() ) {
760         if ( lastURI != uri ) {
761             lastURI.clear();
762             if ( theOne ) {
763                 cmsCloseProfile( theOne );
764             }
765             if ( transf ) {
766                 cmsDeleteTransform( transf );
767                 transf = 0;
768             }
769             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
770             if ( theOne ) {
771                 // a display profile must have the proper stuff
772                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
773                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
775                 (void)space;
776                 (void)profClass;
777 /*
778                 if ( profClass != icSigDisplayClass ) {
779                     g_warning("Not a display profile");
780                     cmsCloseProfile( theOne );
781                     theOne = 0;
782                 } else if ( space != icSigRgbData ) {
783                     g_warning("Not an RGB profile");
784                     cmsCloseProfile( theOne );
785                     theOne = 0;
786                 } else {
787 */
788                     lastURI = uri;
789 /*
790                 }
791 */
792             }
793         }
794     } else if ( theOne ) {
795         cmsCloseProfile( theOne );
796         theOne = 0;
797         lastURI.clear();
798         if ( transf ) {
799             cmsDeleteTransform( transf );
800             transf = 0;
801         }
802     }
804     return theOne;
807 static void free_transforms();
809 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
811     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
812     bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
813     if ( fromDisplay ) {
814         if ( transf ) {
815             cmsDeleteTransform(transf);
816             transf = 0;
817         }
818         return 0;
819     }
821     bool warn = prefs->getBool( "/options/softproof/gamutwarn");
822     int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
823     int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
824     bool bpc = prefs->getBool( "/options/softproof/bpc");
825 #if defined(cmsFLAGS_PRESERVEBLACK)
826     bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
827 #endif //defined(cmsFLAGS_PRESERVEBLACK)
828     Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
829     Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
831     if ( (warn != gamutWarn)
832          || (lastIntent != intent)
833          || (lastProofIntent != proofIntent)
834          || (bpc != lastBPC)
835 #if defined(cmsFLAGS_PRESERVEBLACK)
836          || (preserveBlack != lastPreserveBlack)
837 #endif // defined(cmsFLAGS_PRESERVEBLACK)
838          || (gamutColor != lastGamutColor)
839         ) {
840         gamutWarn = warn;
841         free_transforms();
842         lastIntent = intent;
843         lastProofIntent = proofIntent;
844         lastBPC = bpc;
845 #if defined(cmsFLAGS_PRESERVEBLACK)
846         lastPreserveBlack = preserveBlack;
847 #endif // defined(cmsFLAGS_PRESERVEBLACK)
848         lastGamutColor = gamutColor;
849     }
851     // Fetch these now, as they might clear the transform as a side effect.
852     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
853     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
855     if ( !transf ) {
856         if ( hprof && proofProf ) {
857             DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
858             if ( gamutWarn ) {
859                 dwFlags |= cmsFLAGS_GAMUTCHECK;
860                 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
861             }
862             if ( bpc ) {
863                 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
864             }
865 #if defined(cmsFLAGS_PRESERVEBLACK)
866             if ( preserveBlack ) {
867                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
868             }
869 #endif // defined(cmsFLAGS_PRESERVEBLACK)
870             transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
871         } else if ( hprof ) {
872             transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, intent, 0 );
873         }
874     }
876     return transf;
880 class MemProfile {
881 public:
882     MemProfile();
883     ~MemProfile();
885     std::string id;
886     cmsHPROFILE hprof;
887     cmsHTRANSFORM transf;
888 };
890 MemProfile::MemProfile() :
891     id(),
892     hprof(0),
893     transf(0)
897 MemProfile::~MemProfile()
901 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
903 void free_transforms()
905     if ( transf ) {
906         cmsDeleteTransform(transf);
907         transf = 0;
908     }
910     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
911         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
912             if ( it2->transf ) {
913                 cmsDeleteTransform(it2->transf);
914                 it2->transf = 0;
915             }
916         }
917     }
920 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
922     Glib::ustring id;
924     if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
925         std::vector<MemProfile>& row = perMonitorProfiles[screen];
926         if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
927             MemProfile& item = row[monitor];
928             id = item.id;
929         }
930     }
932     return id;
935 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
937     Glib::ustring id;
939     while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
940         std::vector<MemProfile> tmp;
941         perMonitorProfiles.push_back(tmp);
942     }
943     std::vector<MemProfile>& row = perMonitorProfiles[screen];
944     while ( static_cast<int>(row.size()) <= monitor ) {
945         MemProfile tmp;
946         row.push_back(tmp);
947     }
948     MemProfile& item = row[monitor];
950     if ( item.hprof ) {
951         cmsCloseProfile( item.hprof );
952         item.hprof = 0;
953     }
954     id.clear();
956     if ( buf && bufLen ) {
957         id = Digest::hashHex(Digest::HASH_MD5,
958                    reinterpret_cast<unsigned char*>(buf), bufLen);
960         // Note: if this is not a valid profile, item.hprof will be set to null.
961         item.hprof = cmsOpenProfileFromMem(buf, bufLen);
962     }
963     item.id = id;
965     return id;
968 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
970     cmsHTRANSFORM result = 0;
971     if ( id.empty() ) {
972         return 0;
973     }
975     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
976     bool found = false;
977     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
978         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
979             if ( id == it2->id ) {
980                 MemProfile& item = *it2;
982                 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
983                 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
984                 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
985                 bool bpc = prefs->getBool( "/options/softproof/bpc");
986 #if defined(cmsFLAGS_PRESERVEBLACK)
987                 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
988 #endif //defined(cmsFLAGS_PRESERVEBLACK)
989                 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
990                 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
992                 if ( (warn != gamutWarn)
993                      || (lastIntent != intent)
994                      || (lastProofIntent != proofIntent)
995                      || (bpc != lastBPC)
996 #if defined(cmsFLAGS_PRESERVEBLACK)
997                      || (preserveBlack != lastPreserveBlack)
998 #endif // defined(cmsFLAGS_PRESERVEBLACK)
999                      || (gamutColor != lastGamutColor)
1000                     ) {
1001                     gamutWarn = warn;
1002                     free_transforms();
1003                     lastIntent = intent;
1004                     lastProofIntent = proofIntent;
1005                     lastBPC = bpc;
1006 #if defined(cmsFLAGS_PRESERVEBLACK)
1007                     lastPreserveBlack = preserveBlack;
1008 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1009                     lastGamutColor = gamutColor;
1010                 }
1012                 // Fetch these now, as they might clear the transform as a side effect.
1013                 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1015                 if ( !item.transf ) {
1016                     if ( item.hprof && proofProf ) {
1017                         DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1018                         if ( gamutWarn ) {
1019                             dwFlags |= cmsFLAGS_GAMUTCHECK;
1020                             cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1021                         }
1022                         if ( bpc ) {
1023                             dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1024                         }
1025 #if defined(cmsFLAGS_PRESERVEBLACK)
1026                         if ( preserveBlack ) {
1027                             dwFlags |= cmsFLAGS_PRESERVEBLACK;
1028                         }
1029 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1030                         item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
1031                     } else if ( item.hprof ) {
1032                         item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, intent, 0 );
1033                     }
1034                 }
1036                 result = item.transf;
1037                 found = true;
1038             }
1039         }
1040     }
1042     return result;
1047 #endif // ENABLE_LCMS
1049 /*
1050   Local Variables:
1051   mode:c++
1052   c-file-style:"stroustrup"
1053   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1054   indent-tabs-mode:nil
1055   fill-column:99
1056   End:
1057 */
1058 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :