2aec6c2f001c97c932072e01e6a6e4cf44573190
3 //#define DEBUG_LCMS
5 #include <glib/gstdio.h>
6 #include <sys/fcntl.h>
7 #include <gdkmm/color.h>
9 #ifdef DEBUG_LCMS
10 #include <gtk/gtkmessagedialog.h>
11 #endif // DEBUG_LCMS
13 #include <cstring>
14 #include <string>
15 #include "xml/repr.h"
16 #include "color-profile.h"
17 #include "color-profile-fns.h"
18 #include "attributes.h"
19 #include "inkscape.h"
20 #include "document.h"
21 #include "preferences.h"
23 #include "dom/uri.h"
24 #include "dom/util/digest.h"
26 using Inkscape::ColorProfile;
27 using Inkscape::ColorProfileClass;
29 namespace Inkscape
30 {
31 #if ENABLE_LCMS
32 static cmsHPROFILE colorprofile_get_system_profile_handle();
33 static cmsHPROFILE colorprofile_get_proof_profile_handle();
34 #endif // ENABLE_LCMS
35 }
37 #ifdef DEBUG_LCMS
38 extern guint update_in_progress;
39 #define DEBUG_MESSAGE(key, ...) \
40 {\
41 Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
42 bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
43 bool dumpD = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D");\
44 bool dumpD2 = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D2");\
45 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
46 if ( dump )\
47 {\
48 g_message( __VA_ARGS__ );\
49 \
50 }\
51 if ( dumpD )\
52 {\
53 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
54 GTK_DIALOG_DESTROY_WITH_PARENT, \
55 GTK_MESSAGE_INFO, \
56 GTK_BUTTONS_OK, \
57 __VA_ARGS__ \
58 );\
59 g_signal_connect_swapped(dialog, "response",\
60 G_CALLBACK(gtk_widget_destroy), \
61 dialog); \
62 gtk_widget_show_all( dialog );\
63 }\
64 }
65 #endif // DEBUG_LCMS
67 static SPObjectClass *cprof_parent_class;
69 #if ENABLE_LCMS
71 cmsHPROFILE ColorProfile::_sRGBProf = 0;
73 cmsHPROFILE ColorProfile::getSRGBProfile() {
74 if ( !_sRGBProf ) {
75 _sRGBProf = cmsCreate_sRGBProfile();
76 }
77 return _sRGBProf;
78 }
80 #endif // ENABLE_LCMS
82 /**
83 * Register ColorProfile class and return its type.
84 */
85 GType Inkscape::colorprofile_get_type()
86 {
87 return ColorProfile::getType();
88 }
90 GType ColorProfile::getType()
91 {
92 static GType type = 0;
93 if (!type) {
94 GTypeInfo info = {
95 sizeof(ColorProfileClass),
96 NULL, NULL,
97 (GClassInitFunc) ColorProfile::classInit,
98 NULL, NULL,
99 sizeof(ColorProfile),
100 16,
101 (GInstanceInitFunc) ColorProfile::init,
102 NULL, /* value_table */
103 };
104 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
105 }
106 return type;
107 }
109 /**
110 * ColorProfile vtable initialization.
111 */
112 void ColorProfile::classInit( ColorProfileClass *klass )
113 {
114 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
116 cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
118 sp_object_class->release = ColorProfile::release;
119 sp_object_class->build = ColorProfile::build;
120 sp_object_class->set = ColorProfile::set;
121 sp_object_class->write = ColorProfile::write;
122 }
124 /**
125 * Callback for ColorProfile object initialization.
126 */
127 void ColorProfile::init( ColorProfile *cprof )
128 {
129 cprof->href = 0;
130 cprof->local = 0;
131 cprof->name = 0;
132 cprof->intentStr = 0;
133 cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
134 #if ENABLE_LCMS
135 cprof->profHandle = 0;
136 cprof->_profileClass = icSigInputClass;
137 cprof->_profileSpace = icSigRgbData;
138 cprof->_transf = 0;
139 cprof->_revTransf = 0;
140 #endif // ENABLE_LCMS
141 }
143 /**
144 * Callback: free object
145 */
146 void ColorProfile::release( SPObject *object )
147 {
148 // Unregister ourselves
149 SPDocument* document = SP_OBJECT_DOCUMENT(object);
150 if ( document ) {
151 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
152 }
154 ColorProfile *cprof = COLORPROFILE(object);
155 if ( cprof->href ) {
156 g_free( cprof->href );
157 cprof->href = 0;
158 }
160 if ( cprof->local ) {
161 g_free( cprof->local );
162 cprof->local = 0;
163 }
165 if ( cprof->name ) {
166 g_free( cprof->name );
167 cprof->name = 0;
168 }
170 if ( cprof->intentStr ) {
171 g_free( cprof->intentStr );
172 cprof->intentStr = 0;
173 }
175 #if ENABLE_LCMS
176 cprof->_clearProfile();
177 #endif // ENABLE_LCMS
178 }
180 #if ENABLE_LCMS
181 void ColorProfile::_clearProfile()
182 {
183 _profileSpace = icSigRgbData;
185 if ( _transf ) {
186 cmsDeleteTransform( _transf );
187 _transf = 0;
188 }
189 if ( _revTransf ) {
190 cmsDeleteTransform( _revTransf );
191 _revTransf = 0;
192 }
193 if ( profHandle ) {
194 cmsCloseProfile( profHandle );
195 profHandle = 0;
196 }
197 }
198 #endif // ENABLE_LCMS
200 /**
201 * Callback: set attributes from associated repr.
202 */
203 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
204 {
205 ColorProfile *cprof = COLORPROFILE(object);
206 g_assert(cprof->href == 0);
207 g_assert(cprof->local == 0);
208 g_assert(cprof->name == 0);
209 g_assert(cprof->intentStr == 0);
211 if (cprof_parent_class->build) {
212 (* cprof_parent_class->build)(object, document, repr);
213 }
214 sp_object_read_attr( object, "xlink:href" );
215 sp_object_read_attr( object, "local" );
216 sp_object_read_attr( object, "name" );
217 sp_object_read_attr( object, "rendering-intent" );
219 // Register
220 if ( document ) {
221 sp_document_add_resource( document, "iccprofile", object );
222 }
223 }
225 /**
226 * Callback: set attribute.
227 */
228 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
229 {
230 ColorProfile *cprof = COLORPROFILE(object);
232 switch (key) {
233 case SP_ATTR_XLINK_HREF:
234 if ( cprof->href ) {
235 g_free( cprof->href );
236 cprof->href = 0;
237 }
238 if ( value ) {
239 cprof->href = g_strdup( value );
240 if ( *cprof->href ) {
241 #if ENABLE_LCMS
242 cmsErrorAction( LCMS_ERROR_SHOW );
244 // TODO open filename and URIs properly
245 //FILE* fp = fopen_utf8name( filename, "r" );
246 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
248 // Try to open relative
249 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
250 if (!doc) {
251 doc = SP_ACTIVE_DOCUMENT;
252 g_warning("object has no document. using active");
253 }
254 //# 1. Get complete URI of document
255 gchar const *docbase = SP_DOCUMENT_URI( doc );
256 if (!docbase)
257 {
258 g_warning("null docbase");
259 docbase = "";
260 }
261 //g_message("docbase:%s\n", docbase);
262 org::w3c::dom::URI docUri(docbase);
263 //# 2. Get href of icc file. we don't care if it's rel or abs
264 org::w3c::dom::URI hrefUri(cprof->href);
265 //# 3. Resolve the href according the docBase. This follows
266 // the w3c specs. All absolute and relative issues are considered
267 org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
268 gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
269 cprof->_clearProfile();
270 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
271 if ( cprof->profHandle ) {
272 cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
273 cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
274 }
275 #ifdef DEBUG_LCMS
276 DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
277 #endif // DEBUG_LCMS
279 #endif // ENABLE_LCMS
280 }
281 }
282 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
283 break;
285 case SP_ATTR_LOCAL:
286 if ( cprof->local ) {
287 g_free( cprof->local );
288 cprof->local = 0;
289 }
290 cprof->local = g_strdup( value );
291 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
292 break;
294 case SP_ATTR_NAME:
295 if ( cprof->name ) {
296 g_free( cprof->name );
297 cprof->name = 0;
298 }
299 cprof->name = g_strdup( value );
300 #ifdef DEBUG_LCMS
301 DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
302 #endif // DEBUG_LCMS
303 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
304 break;
306 case SP_ATTR_RENDERING_INTENT:
307 if ( cprof->intentStr ) {
308 g_free( cprof->intentStr );
309 cprof->intentStr = 0;
310 }
311 cprof->intentStr = g_strdup( value );
313 if ( value ) {
314 if ( strcmp( value, "auto" ) == 0 ) {
315 cprof->rendering_intent = RENDERING_INTENT_AUTO;
316 } else if ( strcmp( value, "perceptual" ) == 0 ) {
317 cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
318 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
319 cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
320 } else if ( strcmp( value, "saturation" ) == 0 ) {
321 cprof->rendering_intent = RENDERING_INTENT_SATURATION;
322 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
323 cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
324 } else {
325 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
326 }
327 } else {
328 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
329 }
331 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
332 break;
334 default:
335 if (cprof_parent_class->set) {
336 (* cprof_parent_class->set)(object, key, value);
337 }
338 break;
339 }
341 }
343 /**
344 * Callback: write attributes to associated repr.
345 */
346 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
347 {
348 ColorProfile *cprof = COLORPROFILE(object);
350 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
351 repr = xml_doc->createElement("svg:color-profile");
352 }
354 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
355 repr->setAttribute( "xlink:href", cprof->href );
356 }
358 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
359 repr->setAttribute( "local", cprof->local );
360 }
362 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
363 repr->setAttribute( "name", cprof->name );
364 }
366 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
367 repr->setAttribute( "rendering-intent", cprof->intentStr );
368 }
370 if (cprof_parent_class->write) {
371 (* cprof_parent_class->write)(object, xml_doc, repr, flags);
372 }
374 return repr;
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 std::list<gchar *> ColorProfile::getProfileDirs() {
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 return sources;
588 }
590 static void findThings() {
591 std::list<gchar *> sources = ColorProfile::getProfileDirs();
593 while (!sources.empty()) {
594 gchar *dirname = sources.front();
595 if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
596 GError *err = 0;
597 GDir *dir = g_dir_open(dirname, 0, &err);
599 if (dir) {
600 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
601 gchar *filepath = g_build_filename(dirname, file, NULL);
604 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
605 sources.push_back(g_strdup(filepath));
606 } else {
607 bool isIccFile = false;
608 struct stat st;
609 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
610 //0-3 == size
611 //36-39 == 'acsp' 0x61637370
612 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
613 if ( fd != -1 ) {
614 guchar scratch[40] = {0};
615 size_t len = sizeof(scratch);
617 //size_t left = 40;
618 ssize_t got = read(fd, scratch, len);
619 if ( got != -1 ) {
620 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
621 if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
622 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
623 }
624 }
626 close(fd);
627 }
628 }
630 if ( isIccFile ) {
631 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
632 if ( prof ) {
633 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
634 cmsCloseProfile( prof );
636 bool sameName = false;
637 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
638 if ( it->getName() == info.getName() ) {
639 sameName = true;
640 break;
641 }
642 }
644 if ( !sameName ) {
645 knownProfiles.push_back(info);
646 }
647 }
648 }
649 }
651 g_free(filepath);
652 }
653 }
654 }
656 // toss the dirname
657 g_free(dirname);
658 sources.pop_front();
659 }
660 }
662 int errorHandlerCB(int ErrorCode, const char *ErrorText)
663 {
664 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
666 return 1;
667 }
669 static bool gamutWarn = false;
670 static Gdk::Color lastGamutColor("#808080");
671 static bool lastBPC = false;
672 #if defined(cmsFLAGS_PRESERVEBLACK)
673 static bool lastPreserveBlack = false;
674 #endif // defined(cmsFLAGS_PRESERVEBLACK)
675 static int lastIntent = INTENT_PERCEPTUAL;
676 static int lastProofIntent = INTENT_PERCEPTUAL;
677 static cmsHTRANSFORM transf = 0;
679 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
680 {
681 static cmsHPROFILE theOne = 0;
682 static Glib::ustring lastURI;
684 static bool init = false;
685 if ( !init ) {
686 cmsSetErrorHandler(errorHandlerCB);
688 findThings();
689 init = true;
690 }
692 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
693 Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
695 if ( !uri.empty() ) {
696 if ( uri != lastURI ) {
697 lastURI.clear();
698 if ( theOne ) {
699 cmsCloseProfile( theOne );
700 }
701 if ( transf ) {
702 cmsDeleteTransform( transf );
703 transf = 0;
704 }
705 theOne = cmsOpenProfileFromFile( uri.data(), "r" );
706 if ( theOne ) {
707 // a display profile must have the proper stuff
708 icColorSpaceSignature space = cmsGetColorSpace(theOne);
709 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
711 if ( profClass != icSigDisplayClass ) {
712 g_warning("Not a display profile");
713 cmsCloseProfile( theOne );
714 theOne = 0;
715 } else if ( space != icSigRgbData ) {
716 g_warning("Not an RGB profile");
717 cmsCloseProfile( theOne );
718 theOne = 0;
719 } else {
720 lastURI = uri;
721 }
722 }
723 }
724 } else if ( theOne ) {
725 cmsCloseProfile( theOne );
726 theOne = 0;
727 lastURI.clear();
728 if ( transf ) {
729 cmsDeleteTransform( transf );
730 transf = 0;
731 }
732 }
734 return theOne;
735 }
738 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
739 {
740 static cmsHPROFILE theOne = 0;
741 static Glib::ustring lastURI;
743 static bool init = false;
744 if ( !init ) {
745 cmsSetErrorHandler(errorHandlerCB);
747 findThings();
748 init = true;
749 }
751 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
752 bool which = prefs->getBool( "/options/softproof/enable");
753 Glib::ustring uri = prefs->getString("/options/softproof/uri");
755 if ( which && !uri.empty() ) {
756 if ( lastURI != uri ) {
757 lastURI.clear();
758 if ( theOne ) {
759 cmsCloseProfile( theOne );
760 }
761 if ( transf ) {
762 cmsDeleteTransform( transf );
763 transf = 0;
764 }
765 theOne = cmsOpenProfileFromFile( uri.data(), "r" );
766 if ( theOne ) {
767 // a display profile must have the proper stuff
768 icColorSpaceSignature space = cmsGetColorSpace(theOne);
769 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
771 (void)space;
772 (void)profClass;
773 /*
774 if ( profClass != icSigDisplayClass ) {
775 g_warning("Not a display profile");
776 cmsCloseProfile( theOne );
777 theOne = 0;
778 } else if ( space != icSigRgbData ) {
779 g_warning("Not an RGB profile");
780 cmsCloseProfile( theOne );
781 theOne = 0;
782 } else {
783 */
784 lastURI = uri;
785 /*
786 }
787 */
788 }
789 }
790 } else if ( theOne ) {
791 cmsCloseProfile( theOne );
792 theOne = 0;
793 lastURI.clear();
794 if ( transf ) {
795 cmsDeleteTransform( transf );
796 transf = 0;
797 }
798 }
800 return theOne;
801 }
803 static void free_transforms();
805 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
806 {
807 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
808 bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
809 if ( fromDisplay ) {
810 if ( transf ) {
811 cmsDeleteTransform(transf);
812 transf = 0;
813 }
814 return 0;
815 }
817 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
818 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
819 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
820 bool bpc = prefs->getBool( "/options/softproof/bpc");
821 #if defined(cmsFLAGS_PRESERVEBLACK)
822 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
823 #endif //defined(cmsFLAGS_PRESERVEBLACK)
824 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
825 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
827 if ( (warn != gamutWarn)
828 || (lastIntent != intent)
829 || (lastProofIntent != proofIntent)
830 || (bpc != lastBPC)
831 #if defined(cmsFLAGS_PRESERVEBLACK)
832 || (preserveBlack != lastPreserveBlack)
833 #endif // defined(cmsFLAGS_PRESERVEBLACK)
834 || (gamutColor != lastGamutColor)
835 ) {
836 gamutWarn = warn;
837 free_transforms();
838 lastIntent = intent;
839 lastProofIntent = proofIntent;
840 lastBPC = bpc;
841 #if defined(cmsFLAGS_PRESERVEBLACK)
842 lastPreserveBlack = preserveBlack;
843 #endif // defined(cmsFLAGS_PRESERVEBLACK)
844 lastGamutColor = gamutColor;
845 }
847 // Fetch these now, as they might clear the transform as a side effect.
848 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
849 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
851 if ( !transf ) {
852 if ( hprof && proofProf ) {
853 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
854 if ( gamutWarn ) {
855 dwFlags |= cmsFLAGS_GAMUTCHECK;
856 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
857 }
858 if ( bpc ) {
859 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
860 }
861 #if defined(cmsFLAGS_PRESERVEBLACK)
862 if ( preserveBlack ) {
863 dwFlags |= cmsFLAGS_PRESERVEBLACK;
864 }
865 #endif // defined(cmsFLAGS_PRESERVEBLACK)
866 transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
867 } else if ( hprof ) {
868 transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
869 }
870 }
872 return transf;
873 }
876 class MemProfile {
877 public:
878 MemProfile();
879 ~MemProfile();
881 std::string id;
882 cmsHPROFILE hprof;
883 cmsHTRANSFORM transf;
884 };
886 MemProfile::MemProfile() :
887 id(),
888 hprof(0),
889 transf(0)
890 {
891 }
893 MemProfile::~MemProfile()
894 {
895 }
897 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
899 void free_transforms()
900 {
901 if ( transf ) {
902 cmsDeleteTransform(transf);
903 transf = 0;
904 }
906 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
907 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
908 if ( it2->transf ) {
909 cmsDeleteTransform(it2->transf);
910 it2->transf = 0;
911 }
912 }
913 }
914 }
916 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
917 {
918 Glib::ustring id;
920 if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
921 std::vector<MemProfile>& row = perMonitorProfiles[screen];
922 if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
923 MemProfile& item = row[monitor];
924 id = item.id;
925 }
926 }
928 return id;
929 }
931 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
932 {
933 Glib::ustring id;
935 while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
936 std::vector<MemProfile> tmp;
937 perMonitorProfiles.push_back(tmp);
938 }
939 std::vector<MemProfile>& row = perMonitorProfiles[screen];
940 while ( static_cast<int>(row.size()) <= monitor ) {
941 MemProfile tmp;
942 row.push_back(tmp);
943 }
944 MemProfile& item = row[monitor];
946 if ( item.hprof ) {
947 cmsCloseProfile( item.hprof );
948 item.hprof = 0;
949 }
950 id.clear();
952 if ( buf && bufLen ) {
953 id = Digest::hashHex(Digest::HASH_MD5,
954 reinterpret_cast<unsigned char*>(buf), bufLen);
956 // Note: if this is not a valid profile, item.hprof will be set to null.
957 item.hprof = cmsOpenProfileFromMem(buf, bufLen);
958 }
959 item.id = id;
961 return id;
962 }
964 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
965 {
966 cmsHTRANSFORM result = 0;
967 if ( id.empty() ) {
968 return 0;
969 }
971 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
972 bool found = false;
973 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
974 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
975 if ( id == it2->id ) {
976 MemProfile& item = *it2;
978 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
979 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
980 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
981 bool bpc = prefs->getBool( "/options/softproof/bpc");
982 #if defined(cmsFLAGS_PRESERVEBLACK)
983 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
984 #endif //defined(cmsFLAGS_PRESERVEBLACK)
985 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
986 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
988 if ( (warn != gamutWarn)
989 || (lastIntent != intent)
990 || (lastProofIntent != proofIntent)
991 || (bpc != lastBPC)
992 #if defined(cmsFLAGS_PRESERVEBLACK)
993 || (preserveBlack != lastPreserveBlack)
994 #endif // defined(cmsFLAGS_PRESERVEBLACK)
995 || (gamutColor != lastGamutColor)
996 ) {
997 gamutWarn = warn;
998 free_transforms();
999 lastIntent = intent;
1000 lastProofIntent = proofIntent;
1001 lastBPC = bpc;
1002 #if defined(cmsFLAGS_PRESERVEBLACK)
1003 lastPreserveBlack = preserveBlack;
1004 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1005 lastGamutColor = gamutColor;
1006 }
1008 // Fetch these now, as they might clear the transform as a side effect.
1009 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1011 if ( !item.transf ) {
1012 if ( item.hprof && proofProf ) {
1013 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1014 if ( gamutWarn ) {
1015 dwFlags |= cmsFLAGS_GAMUTCHECK;
1016 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1017 }
1018 if ( bpc ) {
1019 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1020 }
1021 #if defined(cmsFLAGS_PRESERVEBLACK)
1022 if ( preserveBlack ) {
1023 dwFlags |= cmsFLAGS_PRESERVEBLACK;
1024 }
1025 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1026 item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
1027 } else if ( item.hprof ) {
1028 item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
1029 }
1030 }
1032 result = item.transf;
1033 found = true;
1034 }
1035 }
1036 }
1038 return result;
1039 }
1043 #endif // ENABLE_LCMS
1045 /*
1046 Local Variables:
1047 mode:c++
1048 c-file-style:"stroustrup"
1049 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1050 indent-tabs-mode:nil
1051 fill-column:99
1052 End:
1053 */
1054 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :