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 "prefs-utils.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 gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
42 gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
43 gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
44 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
45 if ( dump )\
46 {\
47 g_message( __VA_ARGS__ );\
48 \
49 }\
50 if ( dumpD )\
51 {\
52 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
53 GTK_DIALOG_DESTROY_WITH_PARENT, \
54 GTK_MESSAGE_INFO, \
55 GTK_BUTTONS_OK, \
56 __VA_ARGS__ \
57 );\
58 g_signal_connect_swapped(dialog, "response",\
59 G_CALLBACK(gtk_widget_destroy), \
60 dialog); \
61 gtk_widget_show_all( dialog );\
62 }\
63 }
64 #endif // DEBUG_LCMS
66 static SPObjectClass *cprof_parent_class;
68 #if ENABLE_LCMS
70 cmsHPROFILE ColorProfile::_sRGBProf = 0;
72 cmsHPROFILE ColorProfile::getSRGBProfile() {
73 if ( !_sRGBProf ) {
74 _sRGBProf = cmsCreate_sRGBProfile();
75 }
76 return _sRGBProf;
77 }
79 #endif // ENABLE_LCMS
81 /**
82 * Register ColorProfile class and return its type.
83 */
84 GType Inkscape::colorprofile_get_type()
85 {
86 return ColorProfile::getType();
87 }
89 GType ColorProfile::getType()
90 {
91 static GType type = 0;
92 if (!type) {
93 GTypeInfo info = {
94 sizeof(ColorProfileClass),
95 NULL, NULL,
96 (GClassInitFunc) ColorProfile::classInit,
97 NULL, NULL,
98 sizeof(ColorProfile),
99 16,
100 (GInstanceInitFunc) ColorProfile::init,
101 NULL, /* value_table */
102 };
103 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
104 }
105 return type;
106 }
108 /**
109 * ColorProfile vtable initialization.
110 */
111 void ColorProfile::classInit( ColorProfileClass *klass )
112 {
113 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
115 cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
117 sp_object_class->release = ColorProfile::release;
118 sp_object_class->build = ColorProfile::build;
119 sp_object_class->set = ColorProfile::set;
120 sp_object_class->write = ColorProfile::write;
121 }
123 /**
124 * Callback for ColorProfile object initialization.
125 */
126 void ColorProfile::init( ColorProfile *cprof )
127 {
128 cprof->href = 0;
129 cprof->local = 0;
130 cprof->name = 0;
131 cprof->intentStr = 0;
132 cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
133 #if ENABLE_LCMS
134 cprof->profHandle = 0;
135 cprof->_profileClass = icSigInputClass;
136 cprof->_profileSpace = icSigRgbData;
137 cprof->_transf = 0;
138 cprof->_revTransf = 0;
139 #endif // ENABLE_LCMS
140 }
142 /**
143 * Callback: free object
144 */
145 void ColorProfile::release( SPObject *object )
146 {
147 // Unregister ourselves
148 SPDocument* document = SP_OBJECT_DOCUMENT(object);
149 if ( document ) {
150 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
151 }
153 ColorProfile *cprof = COLORPROFILE(object);
154 if ( cprof->href ) {
155 g_free( cprof->href );
156 cprof->href = 0;
157 }
159 if ( cprof->local ) {
160 g_free( cprof->local );
161 cprof->local = 0;
162 }
164 if ( cprof->name ) {
165 g_free( cprof->name );
166 cprof->name = 0;
167 }
169 if ( cprof->intentStr ) {
170 g_free( cprof->intentStr );
171 cprof->intentStr = 0;
172 }
174 #if ENABLE_LCMS
175 cprof->_clearProfile();
176 #endif // ENABLE_LCMS
177 }
179 #if ENABLE_LCMS
180 void ColorProfile::_clearProfile()
181 {
182 _profileSpace = icSigRgbData;
184 if ( _transf ) {
185 cmsDeleteTransform( _transf );
186 _transf = 0;
187 }
188 if ( _revTransf ) {
189 cmsDeleteTransform( _revTransf );
190 _revTransf = 0;
191 }
192 if ( profHandle ) {
193 cmsCloseProfile( profHandle );
194 profHandle = 0;
195 }
196 }
197 #endif // ENABLE_LCMS
199 /**
200 * Callback: set attributes from associated repr.
201 */
202 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
203 {
204 ColorProfile *cprof = COLORPROFILE(object);
205 g_assert(cprof->href == 0);
206 g_assert(cprof->local == 0);
207 g_assert(cprof->name == 0);
208 g_assert(cprof->intentStr == 0);
210 if (cprof_parent_class->build) {
211 (* cprof_parent_class->build)(object, document, repr);
212 }
213 sp_object_read_attr( object, "xlink:href" );
214 sp_object_read_attr( object, "local" );
215 sp_object_read_attr( object, "name" );
216 sp_object_read_attr( object, "rendering-intent" );
218 // Register
219 if ( document ) {
220 sp_document_add_resource( document, "iccprofile", object );
221 }
222 }
224 /**
225 * Callback: set attribute.
226 */
227 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
228 {
229 ColorProfile *cprof = COLORPROFILE(object);
231 switch (key) {
232 case SP_ATTR_XLINK_HREF:
233 if ( cprof->href ) {
234 g_free( cprof->href );
235 cprof->href = 0;
236 }
237 if ( value ) {
238 cprof->href = g_strdup( value );
239 if ( *cprof->href ) {
240 #if ENABLE_LCMS
241 cmsErrorAction( LCMS_ERROR_SHOW );
243 // TODO open filename and URIs properly
244 //FILE* fp = fopen_utf8name( filename, "r" );
245 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
247 // Try to open relative
248 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
249 if (!doc) {
250 doc = SP_ACTIVE_DOCUMENT;
251 g_warning("object has no document. using active");
252 }
253 //# 1. Get complete URI of document
254 gchar const *docbase = SP_DOCUMENT_URI( doc );
255 if (!docbase)
256 {
257 g_warning("null docbase");
258 docbase = "";
259 }
260 //g_message("docbase:%s\n", docbase);
261 org::w3c::dom::URI docUri(docbase);
262 //# 2. Get href of icc file. we don't care if it's rel or abs
263 org::w3c::dom::URI hrefUri(cprof->href);
264 //# 3. Resolve the href according the docBase. This follows
265 // the w3c specs. All absolute and relative issues are considered
266 org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
267 gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
268 cprof->_clearProfile();
269 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
270 if ( cprof->profHandle ) {
271 cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
272 cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
273 }
274 #ifdef DEBUG_LCMS
275 DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
276 #endif // DEBUG_LCMS
278 #endif // ENABLE_LCMS
279 }
280 }
281 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
282 break;
284 case SP_ATTR_LOCAL:
285 if ( cprof->local ) {
286 g_free( cprof->local );
287 cprof->local = 0;
288 }
289 cprof->local = g_strdup( value );
290 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
291 break;
293 case SP_ATTR_NAME:
294 if ( cprof->name ) {
295 g_free( cprof->name );
296 cprof->name = 0;
297 }
298 cprof->name = g_strdup( value );
299 #ifdef DEBUG_LCMS
300 DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
301 #endif // DEBUG_LCMS
302 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
303 break;
305 case SP_ATTR_RENDERING_INTENT:
306 if ( cprof->intentStr ) {
307 g_free( cprof->intentStr );
308 cprof->intentStr = 0;
309 }
310 cprof->intentStr = g_strdup( value );
312 if ( value ) {
313 if ( strcmp( value, "auto" ) == 0 ) {
314 cprof->rendering_intent = RENDERING_INTENT_AUTO;
315 } else if ( strcmp( value, "perceptual" ) == 0 ) {
316 cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
317 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
318 cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
319 } else if ( strcmp( value, "saturation" ) == 0 ) {
320 cprof->rendering_intent = RENDERING_INTENT_SATURATION;
321 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
322 cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
323 } else {
324 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
325 }
326 } else {
327 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
328 }
330 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
331 break;
333 default:
334 if (cprof_parent_class->set) {
335 (* cprof_parent_class->set)(object, key, value);
336 }
337 break;
338 }
340 }
342 /**
343 * Callback: write attributes to associated repr.
344 */
345 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
346 {
347 ColorProfile *cprof = COLORPROFILE(object);
349 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
350 repr = xml_doc->createElement("svg:color-profile");
351 }
353 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
354 repr->setAttribute( "xlink:href", cprof->href );
355 }
357 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
358 repr->setAttribute( "local", cprof->local );
359 }
361 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
362 repr->setAttribute( "name", cprof->name );
363 }
365 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
366 repr->setAttribute( "rendering-intent", cprof->intentStr );
367 }
369 if (cprof_parent_class->write) {
370 (* cprof_parent_class->write)(object, xml_doc, repr, flags);
371 }
373 return repr;
374 }
377 #if ENABLE_LCMS
379 struct MapMap {
380 icColorSpaceSignature space;
381 DWORD inForm;
382 };
384 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
385 {
386 MapMap possible[] = {
387 {icSigXYZData, TYPE_XYZ_16},
388 {icSigLabData, TYPE_Lab_16},
389 //icSigLuvData
390 {icSigYCbCrData, TYPE_YCbCr_16},
391 {icSigYxyData, TYPE_Yxy_16},
392 {icSigRgbData, TYPE_RGB_16},
393 {icSigGrayData, TYPE_GRAY_16},
394 {icSigHsvData, TYPE_HSV_16},
395 {icSigHlsData, TYPE_HLS_16},
396 {icSigCmykData, TYPE_CMYK_16},
397 {icSigCmyData, TYPE_CMY_16},
398 };
400 int index = 0;
401 for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
402 if ( possible[i].space == space ) {
403 index = i;
404 break;
405 }
406 }
408 return possible[index].inForm;
409 }
411 static int getLcmsIntent( guint svgIntent )
412 {
413 int intent = INTENT_PERCEPTUAL;
414 switch ( svgIntent ) {
415 case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
416 intent = INTENT_RELATIVE_COLORIMETRIC;
417 break;
418 case Inkscape::RENDERING_INTENT_SATURATION:
419 intent = INTENT_SATURATION;
420 break;
421 case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
422 intent = INTENT_ABSOLUTE_COLORIMETRIC;
423 break;
424 case Inkscape::RENDERING_INTENT_PERCEPTUAL:
425 case Inkscape::RENDERING_INTENT_UNKNOWN:
426 case Inkscape::RENDERING_INTENT_AUTO:
427 default:
428 intent = INTENT_PERCEPTUAL;
429 }
430 return intent;
431 }
433 static SPObject* bruteFind( SPDocument* document, gchar const* name )
434 {
435 SPObject* result = 0;
436 const GSList * current = sp_document_get_resource_list(document, "iccprofile");
437 while ( current && !result ) {
438 if ( IS_COLORPROFILE(current->data) ) {
439 ColorProfile* prof = COLORPROFILE(current->data);
440 if ( prof ) {
441 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
442 result = SP_OBJECT(current->data);
443 break;
444 }
445 }
446 }
447 current = g_slist_next(current);
448 }
450 return result;
451 }
453 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
454 {
455 cmsHPROFILE prof = 0;
457 SPObject* thing = bruteFind( document, name );
458 if ( thing ) {
459 prof = COLORPROFILE(thing)->profHandle;
460 }
462 if ( intent ) {
463 *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
464 }
466 #ifdef DEBUG_LCMS
467 DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
468 #endif // DEBUG_LCMS
470 return prof;
471 }
473 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
474 {
475 if ( !_transf ) {
476 int intent = getLcmsIntent(rendering_intent);
477 _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
478 }
479 return _transf;
480 }
482 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
483 {
484 if ( !_revTransf ) {
485 int intent = getLcmsIntent(rendering_intent);
486 _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
487 }
488 return _revTransf;
489 }
492 #include <io/sys.h>
494 class ProfileInfo
495 {
496 public:
497 ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
499 Glib::ustring const& getName() {return _name;}
500 Glib::ustring const& getPath() {return _path;}
501 icColorSpaceSignature getSpace() {return _profileSpace;}
502 icProfileClassSignature getClass() {return _profileClass;}
504 private:
505 Glib::ustring _path;
506 Glib::ustring _name;
507 icColorSpaceSignature _profileSpace;
508 icProfileClassSignature _profileClass;
509 };
512 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
513 {
514 _path = path;
515 _name = cmsTakeProductDesc(prof);
516 _profileSpace = cmsGetColorSpace( prof );
517 _profileClass = cmsGetDeviceClass( prof );
518 }
522 static std::vector<ProfileInfo> knownProfiles;
524 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
525 {
526 std::vector<Glib::ustring> result;
528 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
529 if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
530 result.push_back( it->getName() );
531 }
532 }
534 return result;
535 }
537 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
538 {
539 std::vector<Glib::ustring> result;
541 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
542 if ( it->getClass() == icSigOutputClass ) {
543 result.push_back( it->getName() );
544 }
545 }
547 return result;
548 }
550 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
551 {
552 Glib::ustring result;
554 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
555 if ( name == it->getName() ) {
556 result = it->getPath();
557 break;
558 }
559 }
561 return result;
562 }
564 static void findThings() {
565 std::list<gchar *> sources;
567 gchar* base = profile_path("XXX");
568 {
569 gchar* base2 = g_path_get_dirname(base);
570 g_free(base);
571 base = base2;
572 base2 = g_path_get_dirname(base);
573 g_free(base);
574 base = base2;
575 }
577 // first try user's local dir
578 sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
579 sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
581 const gchar* const * dataDirs = g_get_system_data_dirs();
582 for ( int i = 0; dataDirs[i]; i++ ) {
583 sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
584 }
586 while (!sources.empty()) {
587 gchar *dirname = sources.front();
588 if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
589 GError *err = 0;
590 GDir *dir = g_dir_open(dirname, 0, &err);
592 if (dir) {
593 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
594 gchar *filepath = g_build_filename(dirname, file, NULL);
597 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
598 sources.push_back(g_strdup(filepath));
599 } else {
600 bool isIccFile = false;
601 struct stat st;
602 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
603 //0-3 == size
604 //36-39 == 'acsp' 0x61637370
605 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
606 if ( fd != -1 ) {
607 guchar scratch[40] = {0};
608 size_t len = sizeof(scratch);
610 //size_t left = 40;
611 ssize_t got = read(fd, scratch, len);
612 if ( got != -1 ) {
613 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
614 if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
615 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
616 }
617 }
619 close(fd);
620 }
621 }
623 if ( isIccFile ) {
624 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
625 if ( prof ) {
626 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
627 cmsCloseProfile( prof );
629 bool sameName = false;
630 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
631 if ( it->getName() == info.getName() ) {
632 sameName = true;
633 break;
634 }
635 }
637 if ( !sameName ) {
638 knownProfiles.push_back(info);
639 }
640 }
641 }
642 }
644 g_free(filepath);
645 }
646 }
647 }
649 // toss the dirname
650 g_free(dirname);
651 sources.pop_front();
652 }
653 }
655 int errorHandlerCB(int ErrorCode, const char *ErrorText)
656 {
657 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
659 return 1;
660 }
662 static bool gamutWarn = false;
663 static Gdk::Color lastGamutColor("#808080");
664 static bool lastBPC = false;
665 #if defined(cmsFLAGS_PRESERVEBLACK)
666 static bool lastPreserveBlack = false;
667 #endif // defined(cmsFLAGS_PRESERVEBLACK)
668 static int lastIntent = INTENT_PERCEPTUAL;
669 static int lastProofIntent = INTENT_PERCEPTUAL;
670 static cmsHTRANSFORM transf = 0;
672 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
673 {
674 static cmsHPROFILE theOne = 0;
675 static std::string lastURI;
677 static bool init = false;
678 if ( !init ) {
679 cmsSetErrorHandler(errorHandlerCB);
681 findThings();
682 init = true;
683 }
685 gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
687 if ( uri && *uri ) {
688 if ( lastURI != std::string(uri) ) {
689 lastURI.clear();
690 if ( theOne ) {
691 cmsCloseProfile( theOne );
692 }
693 if ( transf ) {
694 cmsDeleteTransform( transf );
695 transf = 0;
696 }
697 theOne = cmsOpenProfileFromFile( uri, "r" );
698 if ( theOne ) {
699 // a display profile must have the proper stuff
700 icColorSpaceSignature space = cmsGetColorSpace(theOne);
701 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
703 if ( profClass != icSigDisplayClass ) {
704 g_warning("Not a display profile");
705 cmsCloseProfile( theOne );
706 theOne = 0;
707 } else if ( space != icSigRgbData ) {
708 g_warning("Not an RGB profile");
709 cmsCloseProfile( theOne );
710 theOne = 0;
711 } else {
712 lastURI = uri;
713 }
714 }
715 }
716 } else if ( theOne ) {
717 cmsCloseProfile( theOne );
718 theOne = 0;
719 lastURI.clear();
720 if ( transf ) {
721 cmsDeleteTransform( transf );
722 transf = 0;
723 }
724 }
726 return theOne;
727 }
730 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
731 {
732 static cmsHPROFILE theOne = 0;
733 static std::string lastURI;
735 static bool init = false;
736 if ( !init ) {
737 cmsSetErrorHandler(errorHandlerCB);
739 findThings();
740 init = true;
741 }
743 long long int which = prefs_get_int_attribute_limited( "options.softproof", "enable", 0, 0, 1 );
744 gchar const * uri = prefs_get_string_attribute("options.softproof", "uri");
746 if ( which && uri && *uri ) {
747 if ( lastURI != std::string(uri) ) {
748 lastURI.clear();
749 if ( theOne ) {
750 cmsCloseProfile( theOne );
751 }
752 if ( transf ) {
753 cmsDeleteTransform( transf );
754 transf = 0;
755 }
756 theOne = cmsOpenProfileFromFile( uri, "r" );
757 if ( theOne ) {
758 // a display profile must have the proper stuff
759 icColorSpaceSignature space = cmsGetColorSpace(theOne);
760 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
762 (void)space;
763 (void)profClass;
764 /*
765 if ( profClass != icSigDisplayClass ) {
766 g_warning("Not a display profile");
767 cmsCloseProfile( theOne );
768 theOne = 0;
769 } else if ( space != icSigRgbData ) {
770 g_warning("Not an RGB profile");
771 cmsCloseProfile( theOne );
772 theOne = 0;
773 } else {
774 */
775 lastURI = uri;
776 /*
777 }
778 */
779 }
780 }
781 } else if ( theOne ) {
782 cmsCloseProfile( theOne );
783 theOne = 0;
784 lastURI.clear();
785 if ( transf ) {
786 cmsDeleteTransform( transf );
787 transf = 0;
788 }
789 }
791 return theOne;
792 }
794 static void free_transforms();
796 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
797 {
798 long long int fromDisplay = prefs_get_int_attribute_limited( "options.displayprofile", "from_display", 0, 0, 1 );
799 if ( fromDisplay ) {
800 if ( transf ) {
801 cmsDeleteTransform(transf);
802 transf = 0;
803 }
804 return 0;
805 }
807 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
808 int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
809 int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
810 bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
811 #if defined(cmsFLAGS_PRESERVEBLACK)
812 bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
813 #endif //defined(cmsFLAGS_PRESERVEBLACK)
814 gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
815 Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
817 if ( (warn != gamutWarn)
818 || (lastIntent != intent)
819 || (lastProofIntent != proofIntent)
820 || (bpc != lastBPC)
821 #if defined(cmsFLAGS_PRESERVEBLACK)
822 || (preserveBlack != lastPreserveBlack)
823 #endif // defined(cmsFLAGS_PRESERVEBLACK)
824 || (gamutColor != lastGamutColor)
825 ) {
826 gamutWarn = warn;
827 free_transforms();
828 lastIntent = intent;
829 lastProofIntent = proofIntent;
830 lastBPC = bpc;
831 #if defined(cmsFLAGS_PRESERVEBLACK)
832 lastPreserveBlack = preserveBlack;
833 #endif // defined(cmsFLAGS_PRESERVEBLACK)
834 lastGamutColor = gamutColor;
835 }
837 // Fetch these now, as they might clear the transform as a side effect.
838 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
839 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
841 if ( !transf ) {
842 if ( hprof && proofProf ) {
843 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
844 if ( gamutWarn ) {
845 dwFlags |= cmsFLAGS_GAMUTCHECK;
846 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
847 }
848 if ( bpc ) {
849 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
850 }
851 #if defined(cmsFLAGS_PRESERVEBLACK)
852 if ( preserveBlack ) {
853 dwFlags |= cmsFLAGS_PRESERVEBLACK;
854 }
855 #endif // defined(cmsFLAGS_PRESERVEBLACK)
856 transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
857 } else if ( hprof ) {
858 transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
859 }
860 }
862 return transf;
863 }
866 class MemProfile {
867 public:
868 MemProfile();
869 ~MemProfile();
871 std::string id;
872 cmsHPROFILE hprof;
873 cmsHTRANSFORM transf;
874 };
876 MemProfile::MemProfile() :
877 id(),
878 hprof(0),
879 transf(0)
880 {
881 }
883 MemProfile::~MemProfile()
884 {
885 }
887 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
889 void free_transforms()
890 {
891 if ( transf ) {
892 cmsDeleteTransform(transf);
893 transf = 0;
894 }
896 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
897 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
898 if ( it2->transf ) {
899 cmsDeleteTransform(it2->transf);
900 it2->transf = 0;
901 }
902 }
903 }
904 }
906 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
907 {
908 Glib::ustring id;
910 if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
911 std::vector<MemProfile>& row = perMonitorProfiles[screen];
912 if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
913 MemProfile& item = row[monitor];
914 id = item.id;
915 }
916 }
918 return id;
919 }
921 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
922 {
923 Glib::ustring id;
925 while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
926 std::vector<MemProfile> tmp;
927 perMonitorProfiles.push_back(tmp);
928 }
929 std::vector<MemProfile>& row = perMonitorProfiles[screen];
930 while ( static_cast<int>(row.size()) <= monitor ) {
931 MemProfile tmp;
932 row.push_back(tmp);
933 }
934 MemProfile& item = row[monitor];
936 if ( item.hprof ) {
937 cmsCloseProfile( item.hprof );
938 item.hprof = 0;
939 }
940 id.clear();
942 if ( buf && bufLen ) {
943 id = Digest::hashHex(Digest::HASH_MD5,
944 reinterpret_cast<unsigned char*>(buf), bufLen);
946 // Note: if this is not a valid profile, item.hprof will be set to null.
947 item.hprof = cmsOpenProfileFromMem(buf, bufLen);
948 }
949 item.id = id;
951 return id;
952 }
954 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
955 {
956 cmsHTRANSFORM result = 0;
957 if ( id.empty() ) {
958 return 0;
959 }
961 bool found = false;
962 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
963 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
964 if ( id == it2->id ) {
965 MemProfile& item = *it2;
967 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
968 int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
969 int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
970 bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
971 #if defined(cmsFLAGS_PRESERVEBLACK)
972 bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
973 #endif //defined(cmsFLAGS_PRESERVEBLACK)
974 gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
975 Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
977 if ( (warn != gamutWarn)
978 || (lastIntent != intent)
979 || (lastProofIntent != proofIntent)
980 || (bpc != lastBPC)
981 #if defined(cmsFLAGS_PRESERVEBLACK)
982 || (preserveBlack != lastPreserveBlack)
983 #endif // defined(cmsFLAGS_PRESERVEBLACK)
984 || (gamutColor != lastGamutColor)
985 ) {
986 gamutWarn = warn;
987 free_transforms();
988 lastIntent = intent;
989 lastProofIntent = proofIntent;
990 lastBPC = bpc;
991 #if defined(cmsFLAGS_PRESERVEBLACK)
992 lastPreserveBlack = preserveBlack;
993 #endif // defined(cmsFLAGS_PRESERVEBLACK)
994 lastGamutColor = gamutColor;
995 }
997 // Fetch these now, as they might clear the transform as a side effect.
998 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1000 if ( !item.transf ) {
1001 if ( item.hprof && proofProf ) {
1002 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1003 if ( gamutWarn ) {
1004 dwFlags |= cmsFLAGS_GAMUTCHECK;
1005 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1006 }
1007 if ( bpc ) {
1008 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1009 }
1010 #if defined(cmsFLAGS_PRESERVEBLACK)
1011 if ( preserveBlack ) {
1012 dwFlags |= cmsFLAGS_PRESERVEBLACK;
1013 }
1014 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1015 item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
1016 } else if ( item.hprof ) {
1017 item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
1018 }
1019 }
1021 result = item.transf;
1022 found = true;
1023 }
1024 }
1025 }
1027 return result;
1028 }
1032 #endif // ENABLE_LCMS
1034 /*
1035 Local Variables:
1036 mode:c++
1037 c-file-style:"stroustrup"
1038 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1039 indent-tabs-mode:nil
1040 fill-column:99
1041 End:
1042 */
1043 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :