Code

Make curvature work again by fixing a minor omission
[inkscape.git] / src / color-profile.cpp
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
5 //#define DEBUG_LCMS
7 #include <glib/gstdio.h>
8 #include <sys/fcntl.h>
9 #include <gdkmm/color.h>
11 #ifdef DEBUG_LCMS
12 #include <gtk/gtkmessagedialog.h>
13 #endif // DEBUG_LCMS
15 #include <cstring>
16 #include <string>
18 #ifdef WIN32
19 #ifndef _WIN32_WINDOWS         // Allow use of features specific to Windows 98 or later. Required for correctly including icm.h
20 #define _WIN32_WINDOWS 0x0410
21 #endif
22 #include <windows.h>
23 #endif
25 #include "xml/repr.h"
26 #include "color-profile.h"
27 #include "color-profile-fns.h"
28 #include "attributes.h"
29 #include "inkscape.h"
30 #include "document.h"
31 #include "preferences.h"
33 #include "dom/uri.h"
34 #include "dom/util/digest.h"
36 #ifdef WIN32
37 #include <Icm.h>
38 #endif // WIN32
40 using Inkscape::ColorProfile;
41 using Inkscape::ColorProfileClass;
43 namespace Inkscape
44 {
45 #if ENABLE_LCMS
46 static cmsHPROFILE colorprofile_get_system_profile_handle();
47 static cmsHPROFILE colorprofile_get_proof_profile_handle();
48 #endif // ENABLE_LCMS
49 }
51 #ifdef DEBUG_LCMS
52 extern guint update_in_progress;
53 #define DEBUG_MESSAGE(key, ...) \
54 {\
55     Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
56     bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
57     bool dumpD = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D");\
58     bool dumpD2 = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D2");\
59     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
60     if ( dump )\
61     {\
62         g_message( __VA_ARGS__ );\
63 \
64     }\
65     if ( dumpD )\
66     {\
67         GtkWidget *dialog = gtk_message_dialog_new(NULL,\
68                                                    GTK_DIALOG_DESTROY_WITH_PARENT, \
69                                                    GTK_MESSAGE_INFO,    \
70                                                    GTK_BUTTONS_OK,      \
71                                                    __VA_ARGS__          \
72                                                    );\
73         g_signal_connect_swapped(dialog, "response",\
74                                  G_CALLBACK(gtk_widget_destroy),        \
75                                  dialog);                               \
76         gtk_widget_show_all( dialog );\
77     }\
78 }
79 #endif // DEBUG_LCMS
81 static SPObjectClass *cprof_parent_class;
83 #if ENABLE_LCMS
85 cmsHPROFILE ColorProfile::_sRGBProf = 0;
87 cmsHPROFILE ColorProfile::getSRGBProfile() {
88     if ( !_sRGBProf ) {
89         _sRGBProf = cmsCreate_sRGBProfile();
90     }
91     return _sRGBProf;
92 }
94 #endif // ENABLE_LCMS
96 /**
97  * Register ColorProfile class and return its type.
98  */
99 GType Inkscape::colorprofile_get_type()
101     return ColorProfile::getType();
104 GType ColorProfile::getType()
106     static GType type = 0;
107     if (!type) {
108         GTypeInfo info = {
109             sizeof(ColorProfileClass),
110             NULL, NULL,
111             (GClassInitFunc) ColorProfile::classInit,
112             NULL, NULL,
113             sizeof(ColorProfile),
114             16,
115             (GInstanceInitFunc) ColorProfile::init,
116             NULL,   /* value_table */
117         };
118         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
119     }
120     return type;
123 /**
124  * ColorProfile vtable initialization.
125  */
126 void ColorProfile::classInit( ColorProfileClass *klass )
128     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
130     cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
132     sp_object_class->release = ColorProfile::release;
133     sp_object_class->build = ColorProfile::build;
134     sp_object_class->set = ColorProfile::set;
135     sp_object_class->write = ColorProfile::write;
138 /**
139  * Callback for ColorProfile object initialization.
140  */
141 void ColorProfile::init( ColorProfile *cprof )
143     cprof->href = 0;
144     cprof->local = 0;
145     cprof->name = 0;
146     cprof->intentStr = 0;
147     cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
148 #if ENABLE_LCMS
149     cprof->profHandle = 0;
150     cprof->_profileClass = icSigInputClass;
151     cprof->_profileSpace = icSigRgbData;
152     cprof->_transf = 0;
153     cprof->_revTransf = 0;
154 #endif // ENABLE_LCMS
157 /**
158  * Callback: free object
159  */
160 void ColorProfile::release( SPObject *object )
162     // Unregister ourselves
163     SPDocument* document = SP_OBJECT_DOCUMENT(object);
164     if ( document ) {
165         sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
166     }
168     ColorProfile *cprof = COLORPROFILE(object);
169     if ( cprof->href ) {
170         g_free( cprof->href );
171         cprof->href = 0;
172     }
174     if ( cprof->local ) {
175         g_free( cprof->local );
176         cprof->local = 0;
177     }
179     if ( cprof->name ) {
180         g_free( cprof->name );
181         cprof->name = 0;
182     }
184     if ( cprof->intentStr ) {
185         g_free( cprof->intentStr );
186         cprof->intentStr = 0;
187     }
189 #if ENABLE_LCMS
190     cprof->_clearProfile();
191 #endif // ENABLE_LCMS
194 #if ENABLE_LCMS
195 void ColorProfile::_clearProfile()
197     _profileSpace = icSigRgbData;
199     if ( _transf ) {
200         cmsDeleteTransform( _transf );
201         _transf = 0;
202     }
203     if ( _revTransf ) {
204         cmsDeleteTransform( _revTransf );
205         _revTransf = 0;
206     }
207     if ( profHandle ) {
208         cmsCloseProfile( profHandle );
209         profHandle = 0;
210     }
212 #endif // ENABLE_LCMS
214 /**
215  * Callback: set attributes from associated repr.
216  */
217 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
219     ColorProfile *cprof = COLORPROFILE(object);
220     g_assert(cprof->href == 0);
221     g_assert(cprof->local == 0);
222     g_assert(cprof->name == 0);
223     g_assert(cprof->intentStr == 0);
225     if (cprof_parent_class->build) {
226         (* cprof_parent_class->build)(object, document, repr);
227     }
228     sp_object_read_attr( object, "xlink:href" );
229     sp_object_read_attr( object, "local" );
230     sp_object_read_attr( object, "name" );
231     sp_object_read_attr( object, "rendering-intent" );
233     // Register
234     if ( document ) {
235         sp_document_add_resource( document, "iccprofile", object );
236     }
239 /**
240  * Callback: set attribute.
241  */
242 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
244     ColorProfile *cprof = COLORPROFILE(object);
246     switch (key) {
247         case SP_ATTR_XLINK_HREF:
248             if ( cprof->href ) {
249                 g_free( cprof->href );
250                 cprof->href = 0;
251             }
252             if ( value ) {
253                 cprof->href = g_strdup( value );
254                 if ( *cprof->href ) {
255 #if ENABLE_LCMS
256                     cmsErrorAction( LCMS_ERROR_SHOW );
258                     // TODO open filename and URIs properly
259                     //FILE* fp = fopen_utf8name( filename, "r" );
260                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
262                     // Try to open relative
263                     SPDocument *doc = SP_OBJECT_DOCUMENT(object);
264                     if (!doc) {
265                         doc = SP_ACTIVE_DOCUMENT;
266                         g_warning("object has no document.  using active");
267                     }
268                     //# 1.  Get complete URI of document
269                     gchar const *docbase = SP_DOCUMENT_URI( doc );
270                     if (!docbase)
271                     {
272                         // Normal for files that have not yet been saved.
273                         docbase = "";
274                     }
276                     gchar* escaped = g_uri_escape_string(cprof->href, "!*'();:@=+$,/?#[]", TRUE);
278                     //g_message("docbase:%s\n", docbase);
279                     org::w3c::dom::URI docUri(docbase);
280                     //# 2. Get href of icc file.  we don't care if it's rel or abs
281                     org::w3c::dom::URI hrefUri(escaped);
282                     //# 3.  Resolve the href according the docBase.  This follows
283                     //      the w3c specs.  All absolute and relative issues are considered
284                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
285                     gchar* fullname = g_uri_unescape_string(cprofUri.getNativePath().c_str(), "");
286                     cprof->_clearProfile();
287                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
288                     if ( cprof->profHandle ) {
289                         cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
290                         cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
291                     }
292 #ifdef DEBUG_LCMS
293                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
294 #endif // DEBUG_LCMS
295                     g_free(escaped);
296                     escaped = 0;
297                     g_free(fullname);
298 #endif // ENABLE_LCMS
299                 }
300             }
301             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
302             break;
304         case SP_ATTR_LOCAL:
305             if ( cprof->local ) {
306                 g_free( cprof->local );
307                 cprof->local = 0;
308             }
309             cprof->local = g_strdup( value );
310             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
311             break;
313         case SP_ATTR_NAME:
314             if ( cprof->name ) {
315                 g_free( cprof->name );
316                 cprof->name = 0;
317             }
318             cprof->name = g_strdup( value );
319 #ifdef DEBUG_LCMS
320             DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
321 #endif // DEBUG_LCMS
322             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
323             break;
325         case SP_ATTR_RENDERING_INTENT:
326             if ( cprof->intentStr ) {
327                 g_free( cprof->intentStr );
328                 cprof->intentStr = 0;
329             }
330             cprof->intentStr = g_strdup( value );
332             if ( value ) {
333                 if ( strcmp( value, "auto" ) == 0 ) {
334                     cprof->rendering_intent = RENDERING_INTENT_AUTO;
335                 } else if ( strcmp( value, "perceptual" ) == 0 ) {
336                     cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
337                 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
338                     cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
339                 } else if ( strcmp( value, "saturation" ) == 0 ) {
340                     cprof->rendering_intent = RENDERING_INTENT_SATURATION;
341                 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
342                     cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
343                 } else {
344                     cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
345                 }
346             } else {
347                 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
348             }
350             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
351             break;
353         default:
354             if (cprof_parent_class->set) {
355                 (* cprof_parent_class->set)(object, key, value);
356             }
357             break;
358     }
362 /**
363  * Callback: write attributes to associated repr.
364  */
365 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
367     ColorProfile *cprof = COLORPROFILE(object);
369     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
370         repr = xml_doc->createElement("svg:color-profile");
371     }
373     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
374         repr->setAttribute( "xlink:href", cprof->href );
375     }
377     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
378         repr->setAttribute( "local", cprof->local );
379     }
381     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
382         repr->setAttribute( "name", cprof->name );
383     }
385     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
386         repr->setAttribute( "rendering-intent", cprof->intentStr );
387     }
389     if (cprof_parent_class->write) {
390         (* cprof_parent_class->write)(object, xml_doc, repr, flags);
391     }
393     return repr;
397 #if ENABLE_LCMS
399 struct MapMap {
400     icColorSpaceSignature space;
401     DWORD inForm;
402 };
404 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
406     MapMap possible[] = {
407         {icSigXYZData,   TYPE_XYZ_16},
408         {icSigLabData,   TYPE_Lab_16},
409         //icSigLuvData
410         {icSigYCbCrData, TYPE_YCbCr_16},
411         {icSigYxyData,   TYPE_Yxy_16},
412         {icSigRgbData,   TYPE_RGB_16},
413         {icSigGrayData,  TYPE_GRAY_16},
414         {icSigHsvData,   TYPE_HSV_16},
415         {icSigHlsData,   TYPE_HLS_16},
416         {icSigCmykData,  TYPE_CMYK_16},
417         {icSigCmyData,   TYPE_CMY_16},
418     };
420     int index = 0;
421     for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
422         if ( possible[i].space == space ) {
423             index = i;
424             break;
425         }
426     }
428     return possible[index].inForm;
431 static int getLcmsIntent( guint svgIntent )
433     int intent = INTENT_PERCEPTUAL;
434     switch ( svgIntent ) {
435         case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
436             intent = INTENT_RELATIVE_COLORIMETRIC;
437             break;
438         case Inkscape::RENDERING_INTENT_SATURATION:
439             intent = INTENT_SATURATION;
440             break;
441         case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
442             intent = INTENT_ABSOLUTE_COLORIMETRIC;
443             break;
444         case Inkscape::RENDERING_INTENT_PERCEPTUAL:
445         case Inkscape::RENDERING_INTENT_UNKNOWN:
446         case Inkscape::RENDERING_INTENT_AUTO:
447         default:
448             intent = INTENT_PERCEPTUAL;
449     }
450     return intent;
453 static SPObject* bruteFind( SPDocument* document, gchar const* name )
455     SPObject* result = 0;
456     const GSList * current = sp_document_get_resource_list(document, "iccprofile");
457     while ( current && !result ) {
458         if ( IS_COLORPROFILE(current->data) ) {
459             ColorProfile* prof = COLORPROFILE(current->data);
460             if ( prof ) {
461                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
462                     result = SP_OBJECT(current->data);
463                     break;
464                 }
465             }
466         }
467         current = g_slist_next(current);
468     }
470     return result;
473 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
475     cmsHPROFILE prof = 0;
477     SPObject* thing = bruteFind( document, name );
478     if ( thing ) {
479         prof = COLORPROFILE(thing)->profHandle;
480     }
482     if ( intent ) {
483         *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
484     }
486 #ifdef DEBUG_LCMS
487     DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
488 #endif // DEBUG_LCMS
490     return prof;
493 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
495     if ( !_transf && profHandle ) {
496         int intent = getLcmsIntent(rendering_intent);
497         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
498     }
499     return _transf;
502 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
504     if ( !_revTransf && profHandle ) {
505         int intent = getLcmsIntent(rendering_intent);
506         _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
507     }
508     return _revTransf;
512 #include <io/sys.h>
514 class ProfileInfo
516 public:
517     ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
519     Glib::ustring const& getName() {return _name;}
520     Glib::ustring const& getPath() {return _path;}
521     icColorSpaceSignature getSpace() {return _profileSpace;}
522     icProfileClassSignature getClass() {return _profileClass;}
524 private:
525     Glib::ustring _path;
526     Glib::ustring _name;
527     icColorSpaceSignature _profileSpace;
528     icProfileClassSignature _profileClass;
529 };
532 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
534     _path = path;
535     _name = cmsTakeProductDesc(prof);
536     _profileSpace = cmsGetColorSpace( prof );
537     _profileClass = cmsGetDeviceClass( prof );
542 static std::vector<ProfileInfo> knownProfiles;
544 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
546     std::vector<Glib::ustring> result;
548     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
549         if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
550             result.push_back( it->getName() );
551         }
552     }
554     return result;
557 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
559     std::vector<Glib::ustring> result;
561     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
562         if ( it->getClass() == icSigOutputClass ) {
563             result.push_back( it->getName() );
564         }
565     }
567     return result;
570 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
572     Glib::ustring result;
574     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
575         if ( name == it->getName() ) {
576             result = it->getPath();
577             break;
578         }
579     }
581     return result;
583 #endif // ENABLE_LCMS
585 std::list<Glib::ustring> ColorProfile::getProfileDirs() {
586     std::list<Glib::ustring> sources;
588     gchar* base = profile_path("XXX");
589     {
590         gchar* base2 = g_path_get_dirname(base);
591         g_free(base);
592         base = base2;
593         base2 = g_path_get_dirname(base);
594         g_free(base);
595         base = base2;
596     }
598     // first try user's local dir
599     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
602     const gchar* const * dataDirs = g_get_system_data_dirs();
603     for ( int i = 0; dataDirs[i]; i++ ) {
604         gchar* path = g_build_filename(dataDirs[i], "color", "icc", NULL);
605         sources.push_back(path);
606         g_free(path);
607     }
609     // On OS X:
610     if ( g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_EXISTS)  && g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_IS_DIR) ) {
611         sources.push_back("/Library/ColorSync/Profiles");
613         gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL);
614         if ( g_file_test(path, G_FILE_TEST_EXISTS)  && g_file_test(path, G_FILE_TEST_IS_DIR) ) {
615             sources.push_back(path);
616         }
617         g_free(path);
618     }
621 #ifdef WIN32
622     wchar_t pathBuf[MAX_PATH + 1];
623     pathBuf[0] = 0;
624     DWORD pathSize = sizeof(pathBuf);
625     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
626     if ( GetColorDirectoryW( NULL, pathBuf, &pathSize ) ) {
627         gchar * utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
628         if ( !g_utf8_validate(utf8Path, -1, NULL) ) {
629             g_warning( "GetColorDirectoryW() resulted in invalid UTF-8" );
630         } else {
631             sources.push_back(utf8Path);
632         }
633         g_free( utf8Path );
634     }
635 #endif // WIN32
637     return sources;
640 #if ENABLE_LCMS
641 static void findThings() {
642     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
644     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
645         if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
646             GError *err = 0;
647             GDir *dir = g_dir_open(it->c_str(), 0, &err);
649             if (dir) {
650                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
651                     gchar *filepath = g_build_filename(it->c_str(), file, NULL);
654                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
655                         sources.push_back(g_strdup(filepath));
656                     } else {
657                         bool isIccFile = false;
658                         struct stat st;
659                         if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
660                             //0-3 == size
661                             //36-39 == 'acsp' 0x61637370
662                             int fd = g_open( filepath, O_RDONLY, S_IRWXU);
663                             if ( fd != -1 ) {
664                                 guchar scratch[40] = {0};
665                                 size_t len = sizeof(scratch);
667                                 //size_t left = 40;
668                                 ssize_t got = read(fd, scratch, len);
669                                 if ( got != -1 ) {
670                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
671                                     if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
672                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
673                                     }
674                                 }
676                                 close(fd);
677                             }
678                         }
680                         if ( isIccFile ) {
681                             cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
682                             if ( prof ) {
683                                 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
684                                 cmsCloseProfile( prof );
686                                 bool sameName = false;
687                                 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
688                                     if ( it->getName() == info.getName() ) {
689                                         sameName = true;
690                                         break;
691                                     }
692                                 }
694                                 if ( !sameName ) {
695                                     knownProfiles.push_back(info);
696                                 }
697                             }
698                         }
699                     }
701                     g_free(filepath);
702                 }
703             }
704         }
705     }
708 int errorHandlerCB(int ErrorCode, const char *ErrorText)
710     g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
712     return 1;
715 static bool gamutWarn = false;
716 static Gdk::Color lastGamutColor("#808080");
717 static bool lastBPC = false;
718 #if defined(cmsFLAGS_PRESERVEBLACK)
719 static bool lastPreserveBlack = false;
720 #endif // defined(cmsFLAGS_PRESERVEBLACK)
721 static int lastIntent = INTENT_PERCEPTUAL;
722 static int lastProofIntent = INTENT_PERCEPTUAL;
723 static cmsHTRANSFORM transf = 0;
725 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
727     static cmsHPROFILE theOne = 0;
728     static Glib::ustring lastURI;
730     static bool init = false;
731     if ( !init ) {
732         cmsSetErrorHandler(errorHandlerCB);
734         findThings();
735         init = true;
736     }
738     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
739     Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
741     if ( !uri.empty() ) {
742         if ( uri != lastURI ) {
743             lastURI.clear();
744             if ( theOne ) {
745                 cmsCloseProfile( theOne );
746             }
747             if ( transf ) {
748                 cmsDeleteTransform( transf );
749                 transf = 0;
750             }
751             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
752             if ( theOne ) {
753                 // a display profile must have the proper stuff
754                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
755                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
757                 if ( profClass != icSigDisplayClass ) {
758                     g_warning("Not a display profile");
759                     cmsCloseProfile( theOne );
760                     theOne = 0;
761                 } else if ( space != icSigRgbData ) {
762                     g_warning("Not an RGB profile");
763                     cmsCloseProfile( theOne );
764                     theOne = 0;
765                 } else {
766                     lastURI = uri;
767                 }
768             }
769         }
770     } else if ( theOne ) {
771         cmsCloseProfile( theOne );
772         theOne = 0;
773         lastURI.clear();
774         if ( transf ) {
775             cmsDeleteTransform( transf );
776             transf = 0;
777         }
778     }
780     return theOne;
784 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
786     static cmsHPROFILE theOne = 0;
787     static Glib::ustring lastURI;
789     static bool init = false;
790     if ( !init ) {
791         cmsSetErrorHandler(errorHandlerCB);
793         findThings();
794         init = true;
795     }
797     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
798     bool which = prefs->getBool( "/options/softproof/enable");
799     Glib::ustring uri = prefs->getString("/options/softproof/uri");
801     if ( which && !uri.empty() ) {
802         if ( lastURI != uri ) {
803             lastURI.clear();
804             if ( theOne ) {
805                 cmsCloseProfile( theOne );
806             }
807             if ( transf ) {
808                 cmsDeleteTransform( transf );
809                 transf = 0;
810             }
811             theOne = cmsOpenProfileFromFile( uri.data(), "r" );
812             if ( theOne ) {
813                 // a display profile must have the proper stuff
814                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
815                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
817                 (void)space;
818                 (void)profClass;
819 /*
820                 if ( profClass != icSigDisplayClass ) {
821                     g_warning("Not a display profile");
822                     cmsCloseProfile( theOne );
823                     theOne = 0;
824                 } else if ( space != icSigRgbData ) {
825                     g_warning("Not an RGB profile");
826                     cmsCloseProfile( theOne );
827                     theOne = 0;
828                 } else {
829 */
830                     lastURI = uri;
831 /*
832                 }
833 */
834             }
835         }
836     } else if ( theOne ) {
837         cmsCloseProfile( theOne );
838         theOne = 0;
839         lastURI.clear();
840         if ( transf ) {
841             cmsDeleteTransform( transf );
842             transf = 0;
843         }
844     }
846     return theOne;
849 static void free_transforms();
851 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
853     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
854     bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
855     if ( fromDisplay ) {
856         if ( transf ) {
857             cmsDeleteTransform(transf);
858             transf = 0;
859         }
860         return 0;
861     }
863     bool warn = prefs->getBool( "/options/softproof/gamutwarn");
864     int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
865     int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
866     bool bpc = prefs->getBool( "/options/softproof/bpc");
867 #if defined(cmsFLAGS_PRESERVEBLACK)
868     bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
869 #endif //defined(cmsFLAGS_PRESERVEBLACK)
870     Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
871     Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
873     if ( (warn != gamutWarn)
874          || (lastIntent != intent)
875          || (lastProofIntent != proofIntent)
876          || (bpc != lastBPC)
877 #if defined(cmsFLAGS_PRESERVEBLACK)
878          || (preserveBlack != lastPreserveBlack)
879 #endif // defined(cmsFLAGS_PRESERVEBLACK)
880          || (gamutColor != lastGamutColor)
881         ) {
882         gamutWarn = warn;
883         free_transforms();
884         lastIntent = intent;
885         lastProofIntent = proofIntent;
886         lastBPC = bpc;
887 #if defined(cmsFLAGS_PRESERVEBLACK)
888         lastPreserveBlack = preserveBlack;
889 #endif // defined(cmsFLAGS_PRESERVEBLACK)
890         lastGamutColor = gamutColor;
891     }
893     // Fetch these now, as they might clear the transform as a side effect.
894     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
895     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
897     if ( !transf ) {
898         if ( hprof && proofProf ) {
899             DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
900             if ( gamutWarn ) {
901                 dwFlags |= cmsFLAGS_GAMUTCHECK;
902                 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
903             }
904             if ( bpc ) {
905                 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
906             }
907 #if defined(cmsFLAGS_PRESERVEBLACK)
908             if ( preserveBlack ) {
909                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
910             }
911 #endif // defined(cmsFLAGS_PRESERVEBLACK)
912             transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
913         } else if ( hprof ) {
914             transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, intent, 0 );
915         }
916     }
918     return transf;
922 class MemProfile {
923 public:
924     MemProfile();
925     ~MemProfile();
927     std::string id;
928     cmsHPROFILE hprof;
929     cmsHTRANSFORM transf;
930 };
932 MemProfile::MemProfile() :
933     id(),
934     hprof(0),
935     transf(0)
939 MemProfile::~MemProfile()
943 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
945 void free_transforms()
947     if ( transf ) {
948         cmsDeleteTransform(transf);
949         transf = 0;
950     }
952     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
953         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
954             if ( it2->transf ) {
955                 cmsDeleteTransform(it2->transf);
956                 it2->transf = 0;
957             }
958         }
959     }
962 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
964     Glib::ustring id;
966     if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
967         std::vector<MemProfile>& row = perMonitorProfiles[screen];
968         if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
969             MemProfile& item = row[monitor];
970             id = item.id;
971         }
972     }
974     return id;
977 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
979     Glib::ustring id;
981     while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
982         std::vector<MemProfile> tmp;
983         perMonitorProfiles.push_back(tmp);
984     }
985     std::vector<MemProfile>& row = perMonitorProfiles[screen];
986     while ( static_cast<int>(row.size()) <= monitor ) {
987         MemProfile tmp;
988         row.push_back(tmp);
989     }
990     MemProfile& item = row[monitor];
992     if ( item.hprof ) {
993         cmsCloseProfile( item.hprof );
994         item.hprof = 0;
995     }
996     id.clear();
998     if ( buf && bufLen ) {
999         id = Digest::hashHex(Digest::HASH_MD5,
1000                    reinterpret_cast<unsigned char*>(buf), bufLen);
1002         // Note: if this is not a valid profile, item.hprof will be set to null.
1003         item.hprof = cmsOpenProfileFromMem(buf, bufLen);
1004     }
1005     item.id = id;
1007     return id;
1010 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
1012     cmsHTRANSFORM result = 0;
1013     if ( id.empty() ) {
1014         return 0;
1015     }
1017     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1018     bool found = false;
1019     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
1020         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
1021             if ( id == it2->id ) {
1022                 MemProfile& item = *it2;
1024                 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
1025                 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
1026                 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
1027                 bool bpc = prefs->getBool( "/options/softproof/bpc");
1028 #if defined(cmsFLAGS_PRESERVEBLACK)
1029                 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
1030 #endif //defined(cmsFLAGS_PRESERVEBLACK)
1031                 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
1032                 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
1034                 if ( (warn != gamutWarn)
1035                      || (lastIntent != intent)
1036                      || (lastProofIntent != proofIntent)
1037                      || (bpc != lastBPC)
1038 #if defined(cmsFLAGS_PRESERVEBLACK)
1039                      || (preserveBlack != lastPreserveBlack)
1040 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1041                      || (gamutColor != lastGamutColor)
1042                     ) {
1043                     gamutWarn = warn;
1044                     free_transforms();
1045                     lastIntent = intent;
1046                     lastProofIntent = proofIntent;
1047                     lastBPC = bpc;
1048 #if defined(cmsFLAGS_PRESERVEBLACK)
1049                     lastPreserveBlack = preserveBlack;
1050 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1051                     lastGamutColor = gamutColor;
1052                 }
1054                 // Fetch these now, as they might clear the transform as a side effect.
1055                 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1057                 if ( !item.transf ) {
1058                     if ( item.hprof && proofProf ) {
1059                         DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1060                         if ( gamutWarn ) {
1061                             dwFlags |= cmsFLAGS_GAMUTCHECK;
1062                             cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1063                         }
1064                         if ( bpc ) {
1065                             dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1066                         }
1067 #if defined(cmsFLAGS_PRESERVEBLACK)
1068                         if ( preserveBlack ) {
1069                             dwFlags |= cmsFLAGS_PRESERVEBLACK;
1070                         }
1071 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1072                         item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
1073                     } else if ( item.hprof ) {
1074                         item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, intent, 0 );
1075                     }
1076                 }
1078                 result = item.transf;
1079                 found = true;
1080             }
1081         }
1082     }
1084     return result;
1089 #endif // ENABLE_LCMS
1091 /*
1092   Local Variables:
1093   mode:c++
1094   c-file-style:"stroustrup"
1095   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1096   indent-tabs-mode:nil
1097   fill-column:99
1098   End:
1099 */
1100 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :