Code

Fix compile when lcms is disabled
[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;
564 #endif // ENABLE_LCMS
566 std::list<Glib::ustring> ColorProfile::getProfileDirs() {
567     std::list<Glib::ustring> sources;
569     gchar* base = profile_path("XXX");
570     {
571         gchar* base2 = g_path_get_dirname(base);
572         g_free(base);
573         base = base2;
574         base2 = g_path_get_dirname(base);
575         g_free(base);
576         base = base2;
577     }
579     // first try user's local dir
580     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
581     sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
583     const gchar* const * dataDirs = g_get_system_data_dirs();
584     for ( int i = 0; dataDirs[i]; i++ ) {
585         sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
586     }
588     return sources;
591 #if ENABLE_LCMS
592 static void findThings() {
593     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
595     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
596         if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
597             GError *err = 0;
598             GDir *dir = g_dir_open(it->c_str(), 0, &err);
600             if (dir) {
601                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
602                     gchar *filepath = g_build_filename(it->c_str(), file, NULL);
605                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
606                         sources.push_back(g_strdup(filepath));
607                     } else {
608                         bool isIccFile = false;
609                         struct stat st;
610                         if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
611                             //0-3 == size
612                             //36-39 == 'acsp' 0x61637370
613                             int fd = g_open( filepath, O_RDONLY, S_IRWXU);
614                             if ( fd != -1 ) {
615                                 guchar scratch[40] = {0};
616                                 size_t len = sizeof(scratch);
618                                 //size_t left = 40;
619                                 ssize_t got = read(fd, scratch, len);
620                                 if ( got != -1 ) {
621                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
622                                     if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
623                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
624                                     }
625                                 }
627                                 close(fd);
628                             }
629                         }
631                         if ( isIccFile ) {
632                             cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
633                             if ( prof ) {
634                                 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
635                                 cmsCloseProfile( prof );
637                                 bool sameName = false;
638                                 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
639                                     if ( it->getName() == info.getName() ) {
640                                         sameName = true;
641                                         break;
642                                     }
643                                 }
645                                 if ( !sameName ) {
646                                     knownProfiles.push_back(info);
647                                 }
648                             }
649                         }
650                     }
652                     g_free(filepath);
653                 }
654             }
655         }
656     }
659 int errorHandlerCB(int ErrorCode, const char *ErrorText)
661     g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
663     return 1;
666 static bool gamutWarn = false;
667 static Gdk::Color lastGamutColor("#808080");
668 static bool lastBPC = false;
669 #if defined(cmsFLAGS_PRESERVEBLACK)
670 static bool lastPreserveBlack = false;
671 #endif // defined(cmsFLAGS_PRESERVEBLACK)
672 static int lastIntent = INTENT_PERCEPTUAL;
673 static int lastProofIntent = INTENT_PERCEPTUAL;
674 static cmsHTRANSFORM transf = 0;
676 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
678     static cmsHPROFILE theOne = 0;
679     static Glib::ustring lastURI;
681     static bool init = false;
682     if ( !init ) {
683         cmsSetErrorHandler(errorHandlerCB);
685         findThings();
686         init = true;
687     }
689     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
690     Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
692     if ( !uri.empty() ) {
693         if ( uri != lastURI ) {
694             lastURI.clear();
695             if ( theOne ) {
696                 cmsCloseProfile( theOne );
697             }
698             if ( transf ) {
699                 cmsDeleteTransform( transf );
700                 transf = 0;
701             }
702             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
703             if ( theOne ) {
704                 // a display profile must have the proper stuff
705                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
706                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
708                 if ( profClass != icSigDisplayClass ) {
709                     g_warning("Not a display profile");
710                     cmsCloseProfile( theOne );
711                     theOne = 0;
712                 } else if ( space != icSigRgbData ) {
713                     g_warning("Not an RGB profile");
714                     cmsCloseProfile( theOne );
715                     theOne = 0;
716                 } else {
717                     lastURI = uri;
718                 }
719             }
720         }
721     } else if ( theOne ) {
722         cmsCloseProfile( theOne );
723         theOne = 0;
724         lastURI.clear();
725         if ( transf ) {
726             cmsDeleteTransform( transf );
727             transf = 0;
728         }
729     }
731     return theOne;
735 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
737     static cmsHPROFILE theOne = 0;
738     static Glib::ustring lastURI;
740     static bool init = false;
741     if ( !init ) {
742         cmsSetErrorHandler(errorHandlerCB);
744         findThings();
745         init = true;
746     }
748     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
749     bool which = prefs->getBool( "/options/softproof/enable");
750     Glib::ustring uri = prefs->getString("/options/softproof/uri");
752     if ( which && !uri.empty() ) {
753         if ( lastURI != uri ) {
754             lastURI.clear();
755             if ( theOne ) {
756                 cmsCloseProfile( theOne );
757             }
758             if ( transf ) {
759                 cmsDeleteTransform( transf );
760                 transf = 0;
761             }
762             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
763             if ( theOne ) {
764                 // a display profile must have the proper stuff
765                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
766                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
768                 (void)space;
769                 (void)profClass;
770 /*
771                 if ( profClass != icSigDisplayClass ) {
772                     g_warning("Not a display profile");
773                     cmsCloseProfile( theOne );
774                     theOne = 0;
775                 } else if ( space != icSigRgbData ) {
776                     g_warning("Not an RGB profile");
777                     cmsCloseProfile( theOne );
778                     theOne = 0;
779                 } else {
780 */
781                     lastURI = uri;
782 /*
783                 }
784 */
785             }
786         }
787     } else if ( theOne ) {
788         cmsCloseProfile( theOne );
789         theOne = 0;
790         lastURI.clear();
791         if ( transf ) {
792             cmsDeleteTransform( transf );
793             transf = 0;
794         }
795     }
797     return theOne;
800 static void free_transforms();
802 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
804     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
805     bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
806     if ( fromDisplay ) {
807         if ( transf ) {
808             cmsDeleteTransform(transf);
809             transf = 0;
810         }
811         return 0;
812     }
814     bool warn = prefs->getBool( "/options/softproof/gamutwarn");
815     int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
816     int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
817     bool bpc = prefs->getBool( "/options/softproof/bpc");
818 #if defined(cmsFLAGS_PRESERVEBLACK)
819     bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
820 #endif //defined(cmsFLAGS_PRESERVEBLACK)
821     Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
822     Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
824     if ( (warn != gamutWarn)
825          || (lastIntent != intent)
826          || (lastProofIntent != proofIntent)
827          || (bpc != lastBPC)
828 #if defined(cmsFLAGS_PRESERVEBLACK)
829          || (preserveBlack != lastPreserveBlack)
830 #endif // defined(cmsFLAGS_PRESERVEBLACK)
831          || (gamutColor != lastGamutColor)
832         ) {
833         gamutWarn = warn;
834         free_transforms();
835         lastIntent = intent;
836         lastProofIntent = proofIntent;
837         lastBPC = bpc;
838 #if defined(cmsFLAGS_PRESERVEBLACK)
839         lastPreserveBlack = preserveBlack;
840 #endif // defined(cmsFLAGS_PRESERVEBLACK)
841         lastGamutColor = gamutColor;
842     }
844     // Fetch these now, as they might clear the transform as a side effect.
845     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
846     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
848     if ( !transf ) {
849         if ( hprof && proofProf ) {
850             DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
851             if ( gamutWarn ) {
852                 dwFlags |= cmsFLAGS_GAMUTCHECK;
853                 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
854             }
855             if ( bpc ) {
856                 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
857             }
858 #if defined(cmsFLAGS_PRESERVEBLACK)
859             if ( preserveBlack ) {
860                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
861             }
862 #endif // defined(cmsFLAGS_PRESERVEBLACK)
863             transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
864         } else if ( hprof ) {
865             transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
866         }
867     }
869     return transf;
873 class MemProfile {
874 public:
875     MemProfile();
876     ~MemProfile();
878     std::string id;
879     cmsHPROFILE hprof;
880     cmsHTRANSFORM transf;
881 };
883 MemProfile::MemProfile() :
884     id(),
885     hprof(0),
886     transf(0)
890 MemProfile::~MemProfile()
894 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
896 void free_transforms()
898     if ( transf ) {
899         cmsDeleteTransform(transf);
900         transf = 0;
901     }
903     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
904         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
905             if ( it2->transf ) {
906                 cmsDeleteTransform(it2->transf);
907                 it2->transf = 0;
908             }
909         }
910     }
913 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
915     Glib::ustring id;
917     if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
918         std::vector<MemProfile>& row = perMonitorProfiles[screen];
919         if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
920             MemProfile& item = row[monitor];
921             id = item.id;
922         }
923     }
925     return id;
928 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
930     Glib::ustring id;
932     while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
933         std::vector<MemProfile> tmp;
934         perMonitorProfiles.push_back(tmp);
935     }
936     std::vector<MemProfile>& row = perMonitorProfiles[screen];
937     while ( static_cast<int>(row.size()) <= monitor ) {
938         MemProfile tmp;
939         row.push_back(tmp);
940     }
941     MemProfile& item = row[monitor];
943     if ( item.hprof ) {
944         cmsCloseProfile( item.hprof );
945         item.hprof = 0;
946     }
947     id.clear();
949     if ( buf && bufLen ) {
950         id = Digest::hashHex(Digest::HASH_MD5,
951                    reinterpret_cast<unsigned char*>(buf), bufLen);
953         // Note: if this is not a valid profile, item.hprof will be set to null.
954         item.hprof = cmsOpenProfileFromMem(buf, bufLen);
955     }
956     item.id = id;
958     return id;
961 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
963     cmsHTRANSFORM result = 0;
964     if ( id.empty() ) {
965         return 0;
966     }
968     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
969     bool found = false;
970     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
971         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
972             if ( id == it2->id ) {
973                 MemProfile& item = *it2;
975                 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
976                 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
977                 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
978                 bool bpc = prefs->getBool( "/options/softproof/bpc");
979 #if defined(cmsFLAGS_PRESERVEBLACK)
980                 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
981 #endif //defined(cmsFLAGS_PRESERVEBLACK)
982                 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
983                 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
985                 if ( (warn != gamutWarn)
986                      || (lastIntent != intent)
987                      || (lastProofIntent != proofIntent)
988                      || (bpc != lastBPC)
989 #if defined(cmsFLAGS_PRESERVEBLACK)
990                      || (preserveBlack != lastPreserveBlack)
991 #endif // defined(cmsFLAGS_PRESERVEBLACK)
992                      || (gamutColor != lastGamutColor)
993                     ) {
994                     gamutWarn = warn;
995                     free_transforms();
996                     lastIntent = intent;
997                     lastProofIntent = proofIntent;
998                     lastBPC = bpc;
999 #if defined(cmsFLAGS_PRESERVEBLACK)
1000                     lastPreserveBlack = preserveBlack;
1001 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1002                     lastGamutColor = gamutColor;
1003                 }
1005                 // Fetch these now, as they might clear the transform as a side effect.
1006                 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1008                 if ( !item.transf ) {
1009                     if ( item.hprof && proofProf ) {
1010                         DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1011                         if ( gamutWarn ) {
1012                             dwFlags |= cmsFLAGS_GAMUTCHECK;
1013                             cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1014                         }
1015                         if ( bpc ) {
1016                             dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1017                         }
1018 #if defined(cmsFLAGS_PRESERVEBLACK)
1019                         if ( preserveBlack ) {
1020                             dwFlags |= cmsFLAGS_PRESERVEBLACK;
1021                         }
1022 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1023                         item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
1024                     } else if ( item.hprof ) {
1025                         item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
1026                     }
1027                 }
1029                 result = item.transf;
1030                 found = true;
1031             }
1032         }
1033     }
1035     return result;
1040 #endif // ENABLE_LCMS
1042 /*
1043   Local Variables:
1044   mode:c++
1045   c-file-style:"stroustrup"
1046   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1047   indent-tabs-mode:nil
1048   fill-column:99
1049   End:
1050 */
1051 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :