Code

reenable buil inkview on windows
[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 "xml/repr.h"
14 #include "color-profile.h"
15 #include "color-profile-fns.h"
16 #include "attributes.h"
17 #include "inkscape.h"
18 #include "document.h"
19 #include "prefs-utils.h"
21 #include "dom/uri.h"
22 #include "dom/util/digest.h"
24 using Inkscape::ColorProfile;
25 using Inkscape::ColorProfileClass;
27 namespace Inkscape
28 {
29 #if ENABLE_LCMS
30 static cmsHPROFILE colorprofile_get_system_profile_handle();
31 static cmsHPROFILE colorprofile_get_proof_profile_handle();
32 #endif // ENABLE_LCMS
33 }
35 #ifdef DEBUG_LCMS
36 extern guint update_in_progress;
37 #define DEBUG_MESSAGE(key, ...) \
38 {\
39     gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
40     gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
41     gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
42     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
43     if ( dump )\
44     {\
45         g_message( __VA_ARGS__ );\
46 \
47     }\
48     if ( dumpD )\
49     {\
50         GtkWidget *dialog = gtk_message_dialog_new(NULL,\
51                                                    GTK_DIALOG_DESTROY_WITH_PARENT, \
52                                                    GTK_MESSAGE_INFO,    \
53                                                    GTK_BUTTONS_OK,      \
54                                                    __VA_ARGS__          \
55                                                    );\
56         g_signal_connect_swapped(dialog, "response",\
57                                  G_CALLBACK(gtk_widget_destroy),        \
58                                  dialog);                               \
59         gtk_widget_show_all( dialog );\
60     }\
61 }
62 #endif // DEBUG_LCMS
64 static SPObjectClass *cprof_parent_class;
66 #if ENABLE_LCMS
68 cmsHPROFILE ColorProfile::_sRGBProf = 0;
70 cmsHPROFILE ColorProfile::getSRGBProfile() {
71     if ( !_sRGBProf ) {
72         _sRGBProf = cmsCreate_sRGBProfile();
73     }
74     return _sRGBProf;
75 }
77 #endif // ENABLE_LCMS
79 /**
80  * Register ColorProfile class and return its type.
81  */
82 GType Inkscape::colorprofile_get_type()
83 {
84     return ColorProfile::getType();
85 }
87 GType ColorProfile::getType()
88 {
89     static GType type = 0;
90     if (!type) {
91         GTypeInfo info = {
92             sizeof(ColorProfileClass),
93             NULL, NULL,
94             (GClassInitFunc) ColorProfile::classInit,
95             NULL, NULL,
96             sizeof(ColorProfile),
97             16,
98             (GInstanceInitFunc) ColorProfile::init,
99             NULL,   /* value_table */
100         };
101         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
102     }
103     return type;
106 /**
107  * ColorProfile vtable initialization.
108  */
109 void ColorProfile::classInit( ColorProfileClass *klass )
111     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
113     cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
115     sp_object_class->release = ColorProfile::release;
116     sp_object_class->build = ColorProfile::build;
117     sp_object_class->set = ColorProfile::set;
118     sp_object_class->write = ColorProfile::write;
121 /**
122  * Callback for ColorProfile object initialization.
123  */
124 void ColorProfile::init( ColorProfile *cprof )
126     cprof->href = 0;
127     cprof->local = 0;
128     cprof->name = 0;
129     cprof->intentStr = 0;
130     cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
131 #if ENABLE_LCMS
132     cprof->profHandle = 0;
133     cprof->_profileClass = icSigInputClass;
134     cprof->_profileSpace = icSigRgbData;
135     cprof->_transf = 0;
136     cprof->_revTransf = 0;
137 #endif // ENABLE_LCMS
140 /**
141  * Callback: free object
142  */
143 void ColorProfile::release( SPObject *object )
145     // Unregister ourselves
146     SPDocument* document = SP_OBJECT_DOCUMENT(object);
147     if ( document ) {
148         sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
149     }
151     ColorProfile *cprof = COLORPROFILE(object);
152     if ( cprof->href ) {
153         g_free( cprof->href );
154         cprof->href = 0;
155     }
157     if ( cprof->local ) {
158         g_free( cprof->local );
159         cprof->local = 0;
160     }
162     if ( cprof->name ) {
163         g_free( cprof->name );
164         cprof->name = 0;
165     }
167     if ( cprof->intentStr ) {
168         g_free( cprof->intentStr );
169         cprof->intentStr = 0;
170     }
172 #if ENABLE_LCMS
173     cprof->_clearProfile();
174 #endif // ENABLE_LCMS
177 #if ENABLE_LCMS
178 void ColorProfile::_clearProfile()
180     _profileSpace = icSigRgbData;
182     if ( _transf ) {
183         cmsDeleteTransform( _transf );
184         _transf = 0;
185     }
186     if ( _revTransf ) {
187         cmsDeleteTransform( _revTransf );
188         _revTransf = 0;
189     }
190     if ( profHandle ) {
191         cmsCloseProfile( profHandle );
192         profHandle = 0;
193     }
195 #endif // ENABLE_LCMS
197 /**
198  * Callback: set attributes from associated repr.
199  */
200 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
202     ColorProfile *cprof = COLORPROFILE(object);
203     g_assert(cprof->href == 0);
204     g_assert(cprof->local == 0);
205     g_assert(cprof->name == 0);
206     g_assert(cprof->intentStr == 0);
208     if (cprof_parent_class->build) {
209         (* cprof_parent_class->build)(object, document, repr);
210     }
211     sp_object_read_attr( object, "xlink:href" );
212     sp_object_read_attr( object, "local" );
213     sp_object_read_attr( object, "name" );
214     sp_object_read_attr( object, "rendering-intent" );
216     // Register
217     if ( document ) {
218         sp_document_add_resource( document, "iccprofile", object );
219     }
222 /**
223  * Callback: set attribute.
224  */
225 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
227     ColorProfile *cprof = COLORPROFILE(object);
229     switch (key) {
230         case SP_ATTR_XLINK_HREF:
231             if ( cprof->href ) {
232                 g_free( cprof->href );
233                 cprof->href = 0;
234             }
235             if ( value ) {
236                 cprof->href = g_strdup( value );
237                 if ( *cprof->href ) {
238 #if ENABLE_LCMS
239                     cmsErrorAction( LCMS_ERROR_SHOW );
241                     // TODO open filename and URIs properly
242                     //FILE* fp = fopen_utf8name( filename, "r" );
243                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
245                     // Try to open relative
246                     SPDocument *doc = SP_OBJECT_DOCUMENT(object);
247                     if (!doc) {
248                         doc = SP_ACTIVE_DOCUMENT;
249                         g_warning("object has no document.  using active");
250                     }
251                     //# 1.  Get complete URI of document
252                     gchar const *docbase = SP_DOCUMENT_URI( doc );
253                     if (!docbase)
254                         {
255                         g_warning("null docbase");
256                         docbase = "";
257                         }
258                     //g_message("docbase:%s\n", docbase);
259                     org::w3c::dom::URI docUri(docbase);
260                     //# 2. Get href of icc file.  we don't care if it's rel or abs
261                     org::w3c::dom::URI hrefUri(cprof->href);
262                     //# 3.  Resolve the href according the docBase.  This follows
263                     //      the w3c specs.  All absolute and relative issues are considered
264                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
265                     gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
266                     cprof->_clearProfile();
267                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
268                     if ( cprof->profHandle ) {
269                         cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
270                         cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
271                     }
272 #ifdef DEBUG_LCMS
273                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
274 #endif // DEBUG_LCMS
276 #endif // ENABLE_LCMS
277                 }
278             }
279             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
280             break;
282         case SP_ATTR_LOCAL:
283             if ( cprof->local ) {
284                 g_free( cprof->local );
285                 cprof->local = 0;
286             }
287             cprof->local = g_strdup( value );
288             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
289             break;
291         case SP_ATTR_NAME:
292             if ( cprof->name ) {
293                 g_free( cprof->name );
294                 cprof->name = 0;
295             }
296             cprof->name = g_strdup( value );
297 #ifdef DEBUG_LCMS
298             DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
299 #endif // DEBUG_LCMS
300             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
301             break;
303         case SP_ATTR_RENDERING_INTENT:
304             if ( cprof->intentStr ) {
305                 g_free( cprof->intentStr );
306                 cprof->intentStr = 0;
307             }
308             cprof->intentStr = g_strdup( value );
310             if ( value ) {
311                 if ( strcmp( value, "auto" ) == 0 ) {
312                     cprof->rendering_intent = RENDERING_INTENT_AUTO;
313                 } else if ( strcmp( value, "perceptual" ) == 0 ) {
314                     cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
315                 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
316                     cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
317                 } else if ( strcmp( value, "saturation" ) == 0 ) {
318                     cprof->rendering_intent = RENDERING_INTENT_SATURATION;
319                 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
320                     cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
321                 } else {
322                     cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
323                 }
324             } else {
325                 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
326             }
328             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
329             break;
331         default:
332             if (cprof_parent_class->set) {
333                 (* cprof_parent_class->set)(object, key, value);
334             }
335             break;
336     }
340 /**
341  * Callback: write attributes to associated repr.
342  */
343 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
345     ColorProfile *cprof = COLORPROFILE(object);
347     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
348         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
349         repr = xml_doc->createElement("svg:color-profile");
350     }
352     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
353         repr->setAttribute( "xlink:href", cprof->href );
354     }
356     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
357         repr->setAttribute( "local", cprof->local );
358     }
360     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
361         repr->setAttribute( "name", cprof->name );
362     }
364     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
365         repr->setAttribute( "rendering-intent", cprof->intentStr );
366     }
368     if (cprof_parent_class->write) {
369         (* cprof_parent_class->write)(object, repr, flags);
370     }
372     return repr;
376 #if ENABLE_LCMS
378 struct MapMap {
379     icColorSpaceSignature space;
380     DWORD inForm;
381 };
383 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
385     MapMap possible[] = {
386         {icSigXYZData,   TYPE_XYZ_16},
387         {icSigLabData,   TYPE_Lab_16},
388         //icSigLuvData
389         {icSigYCbCrData, TYPE_YCbCr_16},
390         {icSigYxyData,   TYPE_Yxy_16},
391         {icSigRgbData,   TYPE_RGB_16},
392         {icSigGrayData,  TYPE_GRAY_16},
393         {icSigHsvData,   TYPE_HSV_16},
394         {icSigHlsData,   TYPE_HLS_16},
395         {icSigCmykData,  TYPE_CMYK_16},
396         {icSigCmyData,   TYPE_CMY_16},
397     };
399     int index = 0;
400     for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
401         if ( possible[i].space == space ) {
402             index = i;
403             break;
404         }
405     }
407     return possible[index].inForm;
410 static int getLcmsIntent( guint svgIntent )
412     int intent = INTENT_PERCEPTUAL;
413     switch ( svgIntent ) {
414         case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
415             intent = INTENT_RELATIVE_COLORIMETRIC;
416             break;
417         case Inkscape::RENDERING_INTENT_SATURATION:
418             intent = INTENT_SATURATION;
419             break;
420         case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
421             intent = INTENT_ABSOLUTE_COLORIMETRIC;
422             break;
423         case Inkscape::RENDERING_INTENT_PERCEPTUAL:
424         case Inkscape::RENDERING_INTENT_UNKNOWN:
425         case Inkscape::RENDERING_INTENT_AUTO:
426         default:
427             intent = INTENT_PERCEPTUAL;
428     }
429     return intent;
432 static SPObject* bruteFind( SPDocument* document, gchar const* name )
434     SPObject* result = 0;
435     const GSList * current = sp_document_get_resource_list(document, "iccprofile");
436     while ( current && !result ) {
437         if ( IS_COLORPROFILE(current->data) ) {
438             ColorProfile* prof = COLORPROFILE(current->data);
439             if ( prof ) {
440                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
441                     result = SP_OBJECT(current->data);
442                     break;
443                 }
444             }
445         }
446         current = g_slist_next(current);
447     }
449     return result;
452 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
454     cmsHPROFILE prof = 0;
456     SPObject* thing = bruteFind( document, name );
457     if ( thing ) {
458         prof = COLORPROFILE(thing)->profHandle;
459     }
461     if ( intent ) {
462         *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
463     }
465 #ifdef DEBUG_LCMS
466     DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
467 #endif // DEBUG_LCMS
469     return prof;
472 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
474     if ( !_transf ) {
475         int intent = getLcmsIntent(rendering_intent);
476         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
477     }
478     return _transf;
481 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
483     if ( !_revTransf ) {
484         int intent = getLcmsIntent(rendering_intent);
485         _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
486     }
487     return _revTransf;
491 #include <io/sys.h>
493 class ProfileInfo
495 public:
496     ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
498     Glib::ustring const& getName() {return _name;}
499     Glib::ustring const& getPath() {return _path;}
500     icColorSpaceSignature getSpace() {return _profileSpace;}
501     icProfileClassSignature getClass() {return _profileClass;}
503 private:
504     Glib::ustring _path;
505     Glib::ustring _name;
506     icColorSpaceSignature _profileSpace;
507     icProfileClassSignature _profileClass;
508 };
511 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
513     _path = path;
514     _name = cmsTakeProductDesc(prof);
515     _profileSpace = cmsGetColorSpace( prof );
516     _profileClass = cmsGetDeviceClass( prof );
521 static std::vector<ProfileInfo> knownProfiles;
523 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
525     std::vector<Glib::ustring> result;
527     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
528         if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
529             result.push_back( it->getName() );
530         }
531     }
533     return result;
536 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
538     std::vector<Glib::ustring> result;
540     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
541         if ( it->getClass() == icSigOutputClass ) {
542             result.push_back( it->getName() );
543         }
544     }
546     return result;
549 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
551     Glib::ustring result;
553     for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
554         if ( name == it->getName() ) {
555             result = it->getPath();
556             break;
557         }
558     }
560     return result;
563 static void findThings() {
564     std::list<gchar *> sources;
566     gchar* base = profile_path("XXX");
567     {
568         gchar* base2 = g_path_get_dirname(base);
569         g_free(base);
570         base = base2;
571         base2 = g_path_get_dirname(base);
572         g_free(base);
573         base = base2;
574     }
576     // first try user's local dir
577     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
578     sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
580     const gchar* const * dataDirs = g_get_system_data_dirs();
581     for ( int i = 0; dataDirs[i]; i++ ) {
582         sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
583     }
585     while (!sources.empty()) {
586         gchar *dirname = sources.front();
587         if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
588             GError *err = 0;
589             GDir *dir = g_dir_open(dirname, 0, &err);
591             if (dir) {
592                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
593                     gchar *filepath = g_build_filename(dirname, file, NULL);
596                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
597                         sources.push_back(g_strdup(filepath));
598                     } else {
599                         bool isIccFile = false;
600                         struct stat st;
601                         if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
602                             //0-3 == size
603                             //36-39 == 'acsp' 0x61637370
604                             int fd = g_open( filepath, O_RDONLY, S_IRWXU);
605                             if ( fd != -1 ) {
606                                 guchar scratch[40] = {0};
607                                 size_t len = sizeof(scratch);
609                                 //size_t left = 40;
610                                 ssize_t got = read(fd, scratch, len);
611                                 if ( got != -1 ) {
612                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
613                                     if ( calcSize > 128 && calcSize <= st.st_size ) {
614                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
615                                     }
616                                 }
618                                 close(fd);
619                             }
620                         }
622                         if ( isIccFile ) {
623                             cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
624                             if ( prof ) {
625                                 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
626                                 cmsCloseProfile( prof );
628                                 bool sameName = false;
629                                 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
630                                     if ( it->getName() == info.getName() ) {
631                                         sameName = true;
632                                         break;
633                                     }
634                                 }
636                                 if ( !sameName ) {
637                                     knownProfiles.push_back(info);
638                                 }
639                             }
640                         }
641                     }
643                     g_free(filepath);
644                 }
645             }
646         }
648         // toss the dirname
649         g_free(dirname);
650         sources.pop_front();
651     }
654 int errorHandlerCB(int ErrorCode, const char *ErrorText)
656     g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
658     return 1;
661 static bool gamutWarn = false;
662 static Gdk::Color lastGamutColor("#808080");
663 static bool lastBPC = false;
664 #if defined(cmsFLAGS_PRESERVEBLACK)
665 static bool lastPreserveBlack = false;
666 #endif // defined(cmsFLAGS_PRESERVEBLACK)
667 static int lastIntent = INTENT_PERCEPTUAL;
668 static int lastProofIntent = INTENT_PERCEPTUAL;
669 static cmsHTRANSFORM transf = 0;
671 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
673     static cmsHPROFILE theOne = 0;
674     static std::string lastURI;
676     static bool init = false;
677     if ( !init ) {
678         cmsSetErrorHandler(errorHandlerCB);
680         findThings();
681         init = true;
682     }
684     gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
686     if ( uri && *uri ) {
687         if ( lastURI != std::string(uri) ) {
688             lastURI.clear();
689             if ( theOne ) {
690                 cmsCloseProfile( theOne );
691             }
692             if ( transf ) {
693                 cmsDeleteTransform( transf );
694                 transf = 0;
695             }
696             theOne = cmsOpenProfileFromFile( uri, "r" );
697             if ( theOne ) {
698                 // a display profile must have the proper stuff
699                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
700                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
702                 if ( profClass != icSigDisplayClass ) {
703                     g_warning("Not a display profile");
704                     cmsCloseProfile( theOne );
705                     theOne = 0;
706                 } else if ( space != icSigRgbData ) {
707                     g_warning("Not an RGB profile");
708                     cmsCloseProfile( theOne );
709                     theOne = 0;
710                 } else {
711                     lastURI = uri;
712                 }
713             }
714         }
715     } else if ( theOne ) {
716         cmsCloseProfile( theOne );
717         theOne = 0;
718         lastURI.clear();
719         if ( transf ) {
720             cmsDeleteTransform( transf );
721             transf = 0;
722         }
723     }
725     return theOne;
729 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
731     static cmsHPROFILE theOne = 0;
732     static std::string lastURI;
734     static bool init = false;
735     if ( !init ) {
736         cmsSetErrorHandler(errorHandlerCB);
738         findThings();
739         init = true;
740     }
742     long long int which = prefs_get_int_attribute_limited( "options.softproof", "enable", 0, 0, 1 );
743     gchar const * uri = prefs_get_string_attribute("options.softproof", "uri");
745     if ( which && uri && *uri ) {
746         if ( lastURI != std::string(uri) ) {
747             lastURI.clear();
748             if ( theOne ) {
749                 cmsCloseProfile( theOne );
750             }
751             if ( transf ) {
752                 cmsDeleteTransform( transf );
753                 transf = 0;
754             }
755             theOne = cmsOpenProfileFromFile( uri, "r" );
756             if ( theOne ) {
757                 // a display profile must have the proper stuff
758                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
759                 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
761                 (void)space;
762                 (void)profClass;
763 /*
764                 if ( profClass != icSigDisplayClass ) {
765                     g_warning("Not a display profile");
766                     cmsCloseProfile( theOne );
767                     theOne = 0;
768                 } else if ( space != icSigRgbData ) {
769                     g_warning("Not an RGB profile");
770                     cmsCloseProfile( theOne );
771                     theOne = 0;
772                 } else {
773 */
774                     lastURI = uri;
775 /*
776                 }
777 */
778             }
779         }
780     } else if ( theOne ) {
781         cmsCloseProfile( theOne );
782         theOne = 0;
783         lastURI.clear();
784         if ( transf ) {
785             cmsDeleteTransform( transf );
786             transf = 0;
787         }
788     }
790     return theOne;
793 static void free_transforms();
795 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
797     long long int fromDisplay = prefs_get_int_attribute_limited( "options.displayprofile", "from_display", 0, 0, 1 );
798     if ( fromDisplay ) {
799         if ( transf ) {
800             cmsDeleteTransform(transf);
801             transf = 0;
802         }
803         return 0;
804     }
806     bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
807     int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
808     int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
809     bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
810 #if defined(cmsFLAGS_PRESERVEBLACK)
811     bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
812 #endif //defined(cmsFLAGS_PRESERVEBLACK)
813     gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
814     Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
816     if ( (warn != gamutWarn)
817          || (lastIntent != intent)
818          || (lastProofIntent != proofIntent)
819          || (bpc != lastBPC)
820 #if defined(cmsFLAGS_PRESERVEBLACK)
821          || (preserveBlack != lastPreserveBlack)
822 #endif // defined(cmsFLAGS_PRESERVEBLACK)
823          || (gamutColor != lastGamutColor)
824         ) {
825         gamutWarn = warn;
826         free_transforms();
827         lastIntent = intent;
828         lastProofIntent = proofIntent;
829         lastBPC = bpc;
830 #if defined(cmsFLAGS_PRESERVEBLACK)
831         lastPreserveBlack = preserveBlack;
832 #endif // defined(cmsFLAGS_PRESERVEBLACK)
833         lastGamutColor = gamutColor;
834     }
836     // Fetch these now, as they might clear the transform as a side effect.
837     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
838     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
840     if ( !transf ) {
841         if ( hprof && proofProf ) {
842             DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
843             if ( gamutWarn ) {
844                 dwFlags |= cmsFLAGS_GAMUTCHECK;
845                 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
846             }
847             if ( bpc ) {
848                 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
849             }
850 #if defined(cmsFLAGS_PRESERVEBLACK)
851             if ( preserveBlack ) {
852                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
853             }
854 #endif // defined(cmsFLAGS_PRESERVEBLACK)
855             transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
856         } else if ( hprof ) {
857             transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
858         }
859     }
861     return transf;
865 class MemProfile {
866 public:
867     MemProfile();
868     ~MemProfile();
870     std::string id;
871     cmsHPROFILE hprof;
872     cmsHTRANSFORM transf;
873 };
875 MemProfile::MemProfile() :
876     id(),
877     hprof(0),
878     transf(0)
882 MemProfile::~MemProfile()
886 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
888 void free_transforms()
890     if ( transf ) {
891         cmsDeleteTransform(transf);
892         transf = 0;
893     }
895     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
896         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
897             if ( it2->transf ) {
898                 cmsDeleteTransform(it2->transf);
899                 it2->transf = 0;
900             }
901         }
902     }
905 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
907     Glib::ustring id;
909     if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
910         std::vector<MemProfile>& row = perMonitorProfiles[screen];
911         if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
912             MemProfile& item = row[monitor];
913             id = item.id;
914         }
915     }
917     return id;
920 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
922     Glib::ustring id;
924     while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
925         std::vector<MemProfile> tmp;
926         perMonitorProfiles.push_back(tmp);
927     }
928     std::vector<MemProfile>& row = perMonitorProfiles[screen];
929     while ( static_cast<int>(row.size()) <= monitor ) {
930         MemProfile tmp;
931         row.push_back(tmp);
932     }
933     MemProfile& item = row[monitor];
935     if ( item.hprof ) {
936         cmsCloseProfile( item.hprof );
937         item.hprof = 0;
938     }
939     id.clear();
941     if ( buf && bufLen ) {
942         Md5Digest digest;
943         if ( buf && bufLen ) {
944             digest.append(reinterpret_cast<unsigned char*>(buf), bufLen);
945         }
946         id = digest.finishHex();
948         // Note: if this is not a valid profile, item.hprof will be set to null.
949         item.hprof = cmsOpenProfileFromMem(buf, bufLen);
950     }
951     item.id = id;
953     return id;
956 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
958     cmsHTRANSFORM result = 0;
959     if ( id.empty() ) {
960         return 0;
961     }
963     bool found = false;
964     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
965         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
966             if ( id == it2->id ) {
967                 MemProfile& item = *it2;
969                 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
970                 int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
971                 int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
972                 bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
973 #if defined(cmsFLAGS_PRESERVEBLACK)
974                 bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
975 #endif //defined(cmsFLAGS_PRESERVEBLACK)
976                 gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
977                 Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
979                 if ( (warn != gamutWarn)
980                      || (lastIntent != intent)
981                      || (lastProofIntent != proofIntent)
982                      || (bpc != lastBPC)
983 #if defined(cmsFLAGS_PRESERVEBLACK)
984                      || (preserveBlack != lastPreserveBlack)
985 #endif // defined(cmsFLAGS_PRESERVEBLACK)
986                      || (gamutColor != lastGamutColor)
987                     ) {
988                     gamutWarn = warn;
989                     free_transforms();
990                     lastIntent = intent;
991                     lastProofIntent = proofIntent;
992                     lastBPC = bpc;
993 #if defined(cmsFLAGS_PRESERVEBLACK)
994                     lastPreserveBlack = preserveBlack;
995 #endif // defined(cmsFLAGS_PRESERVEBLACK)
996                     lastGamutColor = gamutColor;
997                 }
999                 // Fetch these now, as they might clear the transform as a side effect.
1000                 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1002                 if ( !item.transf ) {
1003                     if ( item.hprof && proofProf ) {
1004                         DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1005                         if ( gamutWarn ) {
1006                             dwFlags |= cmsFLAGS_GAMUTCHECK;
1007                             cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1008                         }
1009                         if ( bpc ) {
1010                             dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1011                         }
1012 #if defined(cmsFLAGS_PRESERVEBLACK)
1013                         if ( preserveBlack ) {
1014                             dwFlags |= cmsFLAGS_PRESERVEBLACK;
1015                         }
1016 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1017                         item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
1018                     } else if ( item.hprof ) {
1019                         item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
1020                     }
1021                 }
1023                 result = item.transf;
1024                 found = true;
1025             }
1026         }
1027     }
1029     return result;
1034 #endif // ENABLE_LCMS
1036 /*
1037   Local Variables:
1038   mode:c++
1039   c-file-style:"stroustrup"
1040   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1041   indent-tabs-mode:nil
1042   fill-column:99
1043   End:
1044 */
1045 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :