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::Node *repr, guint flags )
346 {
347 ColorProfile *cprof = COLORPROFILE(object);
349 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
350 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
351 repr = xml_doc->createElement("svg:color-profile");
352 }
354 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
355 repr->setAttribute( "xlink:href", cprof->href );
356 }
358 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
359 repr->setAttribute( "local", cprof->local );
360 }
362 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
363 repr->setAttribute( "name", cprof->name );
364 }
366 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
367 repr->setAttribute( "rendering-intent", cprof->intentStr );
368 }
370 if (cprof_parent_class->write) {
371 (* cprof_parent_class->write)(object, repr, flags);
372 }
374 return repr;
375 }
378 #if ENABLE_LCMS
380 struct MapMap {
381 icColorSpaceSignature space;
382 DWORD inForm;
383 };
385 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
386 {
387 MapMap possible[] = {
388 {icSigXYZData, TYPE_XYZ_16},
389 {icSigLabData, TYPE_Lab_16},
390 //icSigLuvData
391 {icSigYCbCrData, TYPE_YCbCr_16},
392 {icSigYxyData, TYPE_Yxy_16},
393 {icSigRgbData, TYPE_RGB_16},
394 {icSigGrayData, TYPE_GRAY_16},
395 {icSigHsvData, TYPE_HSV_16},
396 {icSigHlsData, TYPE_HLS_16},
397 {icSigCmykData, TYPE_CMYK_16},
398 {icSigCmyData, TYPE_CMY_16},
399 };
401 int index = 0;
402 for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
403 if ( possible[i].space == space ) {
404 index = i;
405 break;
406 }
407 }
409 return possible[index].inForm;
410 }
412 static int getLcmsIntent( guint svgIntent )
413 {
414 int intent = INTENT_PERCEPTUAL;
415 switch ( svgIntent ) {
416 case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
417 intent = INTENT_RELATIVE_COLORIMETRIC;
418 break;
419 case Inkscape::RENDERING_INTENT_SATURATION:
420 intent = INTENT_SATURATION;
421 break;
422 case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
423 intent = INTENT_ABSOLUTE_COLORIMETRIC;
424 break;
425 case Inkscape::RENDERING_INTENT_PERCEPTUAL:
426 case Inkscape::RENDERING_INTENT_UNKNOWN:
427 case Inkscape::RENDERING_INTENT_AUTO:
428 default:
429 intent = INTENT_PERCEPTUAL;
430 }
431 return intent;
432 }
434 static SPObject* bruteFind( SPDocument* document, gchar const* name )
435 {
436 SPObject* result = 0;
437 const GSList * current = sp_document_get_resource_list(document, "iccprofile");
438 while ( current && !result ) {
439 if ( IS_COLORPROFILE(current->data) ) {
440 ColorProfile* prof = COLORPROFILE(current->data);
441 if ( prof ) {
442 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
443 result = SP_OBJECT(current->data);
444 break;
445 }
446 }
447 }
448 current = g_slist_next(current);
449 }
451 return result;
452 }
454 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
455 {
456 cmsHPROFILE prof = 0;
458 SPObject* thing = bruteFind( document, name );
459 if ( thing ) {
460 prof = COLORPROFILE(thing)->profHandle;
461 }
463 if ( intent ) {
464 *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
465 }
467 #ifdef DEBUG_LCMS
468 DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
469 #endif // DEBUG_LCMS
471 return prof;
472 }
474 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
475 {
476 if ( !_transf ) {
477 int intent = getLcmsIntent(rendering_intent);
478 _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
479 }
480 return _transf;
481 }
483 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
484 {
485 if ( !_revTransf ) {
486 int intent = getLcmsIntent(rendering_intent);
487 _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
488 }
489 return _revTransf;
490 }
493 #include <io/sys.h>
495 class ProfileInfo
496 {
497 public:
498 ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
500 Glib::ustring const& getName() {return _name;}
501 Glib::ustring const& getPath() {return _path;}
502 icColorSpaceSignature getSpace() {return _profileSpace;}
503 icProfileClassSignature getClass() {return _profileClass;}
505 private:
506 Glib::ustring _path;
507 Glib::ustring _name;
508 icColorSpaceSignature _profileSpace;
509 icProfileClassSignature _profileClass;
510 };
513 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
514 {
515 _path = path;
516 _name = cmsTakeProductDesc(prof);
517 _profileSpace = cmsGetColorSpace( prof );
518 _profileClass = cmsGetDeviceClass( prof );
519 }
523 static std::vector<ProfileInfo> knownProfiles;
525 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
526 {
527 std::vector<Glib::ustring> result;
529 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
530 if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
531 result.push_back( it->getName() );
532 }
533 }
535 return result;
536 }
538 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
539 {
540 std::vector<Glib::ustring> result;
542 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
543 if ( it->getClass() == icSigOutputClass ) {
544 result.push_back( it->getName() );
545 }
546 }
548 return result;
549 }
551 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
552 {
553 Glib::ustring result;
555 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
556 if ( name == it->getName() ) {
557 result = it->getPath();
558 break;
559 }
560 }
562 return result;
563 }
565 static void findThings() {
566 std::list<gchar *> sources;
568 gchar* base = profile_path("XXX");
569 {
570 gchar* base2 = g_path_get_dirname(base);
571 g_free(base);
572 base = base2;
573 base2 = g_path_get_dirname(base);
574 g_free(base);
575 base = base2;
576 }
578 // first try user's local dir
579 sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
580 sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
582 const gchar* const * dataDirs = g_get_system_data_dirs();
583 for ( int i = 0; dataDirs[i]; i++ ) {
584 sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
585 }
587 while (!sources.empty()) {
588 gchar *dirname = sources.front();
589 if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
590 GError *err = 0;
591 GDir *dir = g_dir_open(dirname, 0, &err);
593 if (dir) {
594 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
595 gchar *filepath = g_build_filename(dirname, file, NULL);
598 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
599 sources.push_back(g_strdup(filepath));
600 } else {
601 bool isIccFile = false;
602 struct stat st;
603 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
604 //0-3 == size
605 //36-39 == 'acsp' 0x61637370
606 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
607 if ( fd != -1 ) {
608 guchar scratch[40] = {0};
609 size_t len = sizeof(scratch);
611 //size_t left = 40;
612 ssize_t got = read(fd, scratch, len);
613 if ( got != -1 ) {
614 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
615 if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
616 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
617 }
618 }
620 close(fd);
621 }
622 }
624 if ( isIccFile ) {
625 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
626 if ( prof ) {
627 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
628 cmsCloseProfile( prof );
630 bool sameName = false;
631 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
632 if ( it->getName() == info.getName() ) {
633 sameName = true;
634 break;
635 }
636 }
638 if ( !sameName ) {
639 knownProfiles.push_back(info);
640 }
641 }
642 }
643 }
645 g_free(filepath);
646 }
647 }
648 }
650 // toss the dirname
651 g_free(dirname);
652 sources.pop_front();
653 }
654 }
656 int errorHandlerCB(int ErrorCode, const char *ErrorText)
657 {
658 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
660 return 1;
661 }
663 static bool gamutWarn = false;
664 static Gdk::Color lastGamutColor("#808080");
665 static bool lastBPC = false;
666 #if defined(cmsFLAGS_PRESERVEBLACK)
667 static bool lastPreserveBlack = false;
668 #endif // defined(cmsFLAGS_PRESERVEBLACK)
669 static int lastIntent = INTENT_PERCEPTUAL;
670 static int lastProofIntent = INTENT_PERCEPTUAL;
671 static cmsHTRANSFORM transf = 0;
673 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
674 {
675 static cmsHPROFILE theOne = 0;
676 static std::string lastURI;
678 static bool init = false;
679 if ( !init ) {
680 cmsSetErrorHandler(errorHandlerCB);
682 findThings();
683 init = true;
684 }
686 gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
688 if ( uri && *uri ) {
689 if ( lastURI != std::string(uri) ) {
690 lastURI.clear();
691 if ( theOne ) {
692 cmsCloseProfile( theOne );
693 }
694 if ( transf ) {
695 cmsDeleteTransform( transf );
696 transf = 0;
697 }
698 theOne = cmsOpenProfileFromFile( uri, "r" );
699 if ( theOne ) {
700 // a display profile must have the proper stuff
701 icColorSpaceSignature space = cmsGetColorSpace(theOne);
702 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
704 if ( profClass != icSigDisplayClass ) {
705 g_warning("Not a display profile");
706 cmsCloseProfile( theOne );
707 theOne = 0;
708 } else if ( space != icSigRgbData ) {
709 g_warning("Not an RGB profile");
710 cmsCloseProfile( theOne );
711 theOne = 0;
712 } else {
713 lastURI = uri;
714 }
715 }
716 }
717 } else if ( theOne ) {
718 cmsCloseProfile( theOne );
719 theOne = 0;
720 lastURI.clear();
721 if ( transf ) {
722 cmsDeleteTransform( transf );
723 transf = 0;
724 }
725 }
727 return theOne;
728 }
731 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
732 {
733 static cmsHPROFILE theOne = 0;
734 static std::string lastURI;
736 static bool init = false;
737 if ( !init ) {
738 cmsSetErrorHandler(errorHandlerCB);
740 findThings();
741 init = true;
742 }
744 long long int which = prefs_get_int_attribute_limited( "options.softproof", "enable", 0, 0, 1 );
745 gchar const * uri = prefs_get_string_attribute("options.softproof", "uri");
747 if ( which && uri && *uri ) {
748 if ( lastURI != std::string(uri) ) {
749 lastURI.clear();
750 if ( theOne ) {
751 cmsCloseProfile( theOne );
752 }
753 if ( transf ) {
754 cmsDeleteTransform( transf );
755 transf = 0;
756 }
757 theOne = cmsOpenProfileFromFile( uri, "r" );
758 if ( theOne ) {
759 // a display profile must have the proper stuff
760 icColorSpaceSignature space = cmsGetColorSpace(theOne);
761 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
763 (void)space;
764 (void)profClass;
765 /*
766 if ( profClass != icSigDisplayClass ) {
767 g_warning("Not a display profile");
768 cmsCloseProfile( theOne );
769 theOne = 0;
770 } else if ( space != icSigRgbData ) {
771 g_warning("Not an RGB profile");
772 cmsCloseProfile( theOne );
773 theOne = 0;
774 } else {
775 */
776 lastURI = uri;
777 /*
778 }
779 */
780 }
781 }
782 } else if ( theOne ) {
783 cmsCloseProfile( theOne );
784 theOne = 0;
785 lastURI.clear();
786 if ( transf ) {
787 cmsDeleteTransform( transf );
788 transf = 0;
789 }
790 }
792 return theOne;
793 }
795 static void free_transforms();
797 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
798 {
799 long long int fromDisplay = prefs_get_int_attribute_limited( "options.displayprofile", "from_display", 0, 0, 1 );
800 if ( fromDisplay ) {
801 if ( transf ) {
802 cmsDeleteTransform(transf);
803 transf = 0;
804 }
805 return 0;
806 }
808 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
809 int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
810 int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
811 bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
812 #if defined(cmsFLAGS_PRESERVEBLACK)
813 bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
814 #endif //defined(cmsFLAGS_PRESERVEBLACK)
815 gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
816 Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
818 if ( (warn != gamutWarn)
819 || (lastIntent != intent)
820 || (lastProofIntent != proofIntent)
821 || (bpc != lastBPC)
822 #if defined(cmsFLAGS_PRESERVEBLACK)
823 || (preserveBlack != lastPreserveBlack)
824 #endif // defined(cmsFLAGS_PRESERVEBLACK)
825 || (gamutColor != lastGamutColor)
826 ) {
827 gamutWarn = warn;
828 free_transforms();
829 lastIntent = intent;
830 lastProofIntent = proofIntent;
831 lastBPC = bpc;
832 #if defined(cmsFLAGS_PRESERVEBLACK)
833 lastPreserveBlack = preserveBlack;
834 #endif // defined(cmsFLAGS_PRESERVEBLACK)
835 lastGamutColor = gamutColor;
836 }
838 // Fetch these now, as they might clear the transform as a side effect.
839 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
840 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
842 if ( !transf ) {
843 if ( hprof && proofProf ) {
844 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
845 if ( gamutWarn ) {
846 dwFlags |= cmsFLAGS_GAMUTCHECK;
847 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
848 }
849 if ( bpc ) {
850 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
851 }
852 #if defined(cmsFLAGS_PRESERVEBLACK)
853 if ( preserveBlack ) {
854 dwFlags |= cmsFLAGS_PRESERVEBLACK;
855 }
856 #endif // defined(cmsFLAGS_PRESERVEBLACK)
857 transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
858 } else if ( hprof ) {
859 transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
860 }
861 }
863 return transf;
864 }
867 class MemProfile {
868 public:
869 MemProfile();
870 ~MemProfile();
872 std::string id;
873 cmsHPROFILE hprof;
874 cmsHTRANSFORM transf;
875 };
877 MemProfile::MemProfile() :
878 id(),
879 hprof(0),
880 transf(0)
881 {
882 }
884 MemProfile::~MemProfile()
885 {
886 }
888 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
890 void free_transforms()
891 {
892 if ( transf ) {
893 cmsDeleteTransform(transf);
894 transf = 0;
895 }
897 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
898 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
899 if ( it2->transf ) {
900 cmsDeleteTransform(it2->transf);
901 it2->transf = 0;
902 }
903 }
904 }
905 }
907 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
908 {
909 Glib::ustring id;
911 if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
912 std::vector<MemProfile>& row = perMonitorProfiles[screen];
913 if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
914 MemProfile& item = row[monitor];
915 id = item.id;
916 }
917 }
919 return id;
920 }
922 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
923 {
924 Glib::ustring id;
926 while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
927 std::vector<MemProfile> tmp;
928 perMonitorProfiles.push_back(tmp);
929 }
930 std::vector<MemProfile>& row = perMonitorProfiles[screen];
931 while ( static_cast<int>(row.size()) <= monitor ) {
932 MemProfile tmp;
933 row.push_back(tmp);
934 }
935 MemProfile& item = row[monitor];
937 if ( item.hprof ) {
938 cmsCloseProfile( item.hprof );
939 item.hprof = 0;
940 }
941 id.clear();
943 if ( buf && bufLen ) {
944 Md5Digest digest;
945 if ( buf && bufLen ) {
946 digest.append(reinterpret_cast<unsigned char*>(buf), bufLen);
947 }
948 id = digest.finishHex();
950 // Note: if this is not a valid profile, item.hprof will be set to null.
951 item.hprof = cmsOpenProfileFromMem(buf, bufLen);
952 }
953 item.id = id;
955 return id;
956 }
958 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
959 {
960 cmsHTRANSFORM result = 0;
961 if ( id.empty() ) {
962 return 0;
963 }
965 bool found = false;
966 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
967 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
968 if ( id == it2->id ) {
969 MemProfile& item = *it2;
971 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
972 int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
973 int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
974 bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
975 #if defined(cmsFLAGS_PRESERVEBLACK)
976 bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
977 #endif //defined(cmsFLAGS_PRESERVEBLACK)
978 gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
979 Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
981 if ( (warn != gamutWarn)
982 || (lastIntent != intent)
983 || (lastProofIntent != proofIntent)
984 || (bpc != lastBPC)
985 #if defined(cmsFLAGS_PRESERVEBLACK)
986 || (preserveBlack != lastPreserveBlack)
987 #endif // defined(cmsFLAGS_PRESERVEBLACK)
988 || (gamutColor != lastGamutColor)
989 ) {
990 gamutWarn = warn;
991 free_transforms();
992 lastIntent = intent;
993 lastProofIntent = proofIntent;
994 lastBPC = bpc;
995 #if defined(cmsFLAGS_PRESERVEBLACK)
996 lastPreserveBlack = preserveBlack;
997 #endif // defined(cmsFLAGS_PRESERVEBLACK)
998 lastGamutColor = gamutColor;
999 }
1001 // Fetch these now, as they might clear the transform as a side effect.
1002 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1004 if ( !item.transf ) {
1005 if ( item.hprof && proofProf ) {
1006 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1007 if ( gamutWarn ) {
1008 dwFlags |= cmsFLAGS_GAMUTCHECK;
1009 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1010 }
1011 if ( bpc ) {
1012 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1013 }
1014 #if defined(cmsFLAGS_PRESERVEBLACK)
1015 if ( preserveBlack ) {
1016 dwFlags |= cmsFLAGS_PRESERVEBLACK;
1017 }
1018 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1019 item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
1020 } else if ( item.hprof ) {
1021 item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
1022 }
1023 }
1025 result = item.transf;
1026 found = true;
1027 }
1028 }
1029 }
1031 return result;
1032 }
1036 #endif // ENABLE_LCMS
1038 /*
1039 Local Variables:
1040 mode:c++
1041 c-file-style:"stroustrup"
1042 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1043 indent-tabs-mode:nil
1044 fill-column:99
1045 End:
1046 */
1047 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :