3 #include "xml/repr.h"
4 #include "color-profile.h"
5 #include "color-profile-fns.h"
6 #include "attributes.h"
7 #include "inkscape.h"
8 #include "document.h"
9 #include "prefs-utils.h"
11 #include "dom/uri.h"
13 //#define DEBUG_LCMS
15 #include <glib/gstdio.h>
16 #include <sys/fcntl.h>
17 #include <gdkmm/color.h>
19 #ifdef DEBUG_LCMS
20 #include <gtk/gtkmessagedialog.h>
21 #endif // DEBUG_LCMS
23 using Inkscape::ColorProfile;
24 using Inkscape::ColorProfileClass;
26 namespace Inkscape
27 {
28 #if ENABLE_LCMS
29 static cmsHPROFILE colorprofile_get_system_profile_handle();
30 static cmsHPROFILE colorprofile_get_proof_profile_handle();
31 #endif // ENABLE_LCMS
32 }
34 #ifdef DEBUG_LCMS
35 extern guint update_in_progress;
36 #define DEBUG_MESSAGE(key, ...) \
37 {\
38 gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
39 gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
40 gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
41 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
42 if ( dump )\
43 {\
44 g_message( __VA_ARGS__ );\
45 \
46 }\
47 if ( dumpD )\
48 {\
49 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
50 GTK_DIALOG_DESTROY_WITH_PARENT, \
51 GTK_MESSAGE_INFO, \
52 GTK_BUTTONS_OK, \
53 __VA_ARGS__ \
54 );\
55 g_signal_connect_swapped(dialog, "response",\
56 G_CALLBACK(gtk_widget_destroy), \
57 dialog); \
58 gtk_widget_show_all( dialog );\
59 }\
60 }
61 #endif // DEBUG_LCMS
63 static SPObjectClass *cprof_parent_class;
65 #if ENABLE_LCMS
67 cmsHPROFILE ColorProfile::_sRGBProf = 0;
69 cmsHPROFILE ColorProfile::getSRGBProfile() {
70 if ( !_sRGBProf ) {
71 _sRGBProf = cmsCreate_sRGBProfile();
72 }
73 return _sRGBProf;
74 }
76 #endif // ENABLE_LCMS
78 /**
79 * Register ColorProfile class and return its type.
80 */
81 GType Inkscape::colorprofile_get_type()
82 {
83 return ColorProfile::getType();
84 }
86 GType ColorProfile::getType()
87 {
88 static GType type = 0;
89 if (!type) {
90 GTypeInfo info = {
91 sizeof(ColorProfileClass),
92 NULL, NULL,
93 (GClassInitFunc) ColorProfile::classInit,
94 NULL, NULL,
95 sizeof(ColorProfile),
96 16,
97 (GInstanceInitFunc) ColorProfile::init,
98 NULL, /* value_table */
99 };
100 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
101 }
102 return type;
103 }
105 /**
106 * ColorProfile vtable initialization.
107 */
108 void ColorProfile::classInit( ColorProfileClass *klass )
109 {
110 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
112 cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
114 sp_object_class->release = ColorProfile::release;
115 sp_object_class->build = ColorProfile::build;
116 sp_object_class->set = ColorProfile::set;
117 sp_object_class->write = ColorProfile::write;
118 }
120 /**
121 * Callback for ColorProfile object initialization.
122 */
123 void ColorProfile::init( ColorProfile *cprof )
124 {
125 cprof->href = 0;
126 cprof->local = 0;
127 cprof->name = 0;
128 cprof->intentStr = 0;
129 cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
130 #if ENABLE_LCMS
131 cprof->profHandle = 0;
132 cprof->_profileClass = icSigInputClass;
133 cprof->_profileSpace = icSigRgbData;
134 cprof->_transf = 0;
135 cprof->_revTransf = 0;
136 #endif // ENABLE_LCMS
137 }
139 /**
140 * Callback: free object
141 */
142 void ColorProfile::release( SPObject *object )
143 {
144 // Unregister ourselves
145 SPDocument* document = SP_OBJECT_DOCUMENT(object);
146 if ( document ) {
147 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
148 }
150 ColorProfile *cprof = COLORPROFILE(object);
151 if ( cprof->href ) {
152 g_free( cprof->href );
153 cprof->href = 0;
154 }
156 if ( cprof->local ) {
157 g_free( cprof->local );
158 cprof->local = 0;
159 }
161 if ( cprof->name ) {
162 g_free( cprof->name );
163 cprof->name = 0;
164 }
166 if ( cprof->intentStr ) {
167 g_free( cprof->intentStr );
168 cprof->intentStr = 0;
169 }
171 #if ENABLE_LCMS
172 cprof->_clearProfile();
173 #endif // ENABLE_LCMS
174 }
176 #if ENABLE_LCMS
177 void ColorProfile::_clearProfile()
178 {
179 _profileSpace = icSigRgbData;
181 if ( _transf ) {
182 cmsDeleteTransform( _transf );
183 _transf = 0;
184 }
185 if ( _revTransf ) {
186 cmsDeleteTransform( _revTransf );
187 _revTransf = 0;
188 }
189 if ( profHandle ) {
190 cmsCloseProfile( profHandle );
191 profHandle = 0;
192 }
193 }
194 #endif // ENABLE_LCMS
196 /**
197 * Callback: set attributes from associated repr.
198 */
199 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
200 {
201 ColorProfile *cprof = COLORPROFILE(object);
202 g_assert(cprof->href == 0);
203 g_assert(cprof->local == 0);
204 g_assert(cprof->name == 0);
205 g_assert(cprof->intentStr == 0);
207 if (cprof_parent_class->build) {
208 (* cprof_parent_class->build)(object, document, repr);
209 }
210 sp_object_read_attr( object, "xlink:href" );
211 sp_object_read_attr( object, "local" );
212 sp_object_read_attr( object, "name" );
213 sp_object_read_attr( object, "rendering-intent" );
215 // Register
216 if ( document ) {
217 sp_document_add_resource( document, "iccprofile", object );
218 }
219 }
221 /**
222 * Callback: set attribute.
223 */
224 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
225 {
226 ColorProfile *cprof = COLORPROFILE(object);
228 switch (key) {
229 case SP_ATTR_XLINK_HREF:
230 if ( cprof->href ) {
231 g_free( cprof->href );
232 cprof->href = 0;
233 }
234 if ( value ) {
235 cprof->href = g_strdup( value );
236 if ( *cprof->href ) {
237 #if ENABLE_LCMS
238 cmsErrorAction( LCMS_ERROR_SHOW );
240 // TODO open filename and URIs properly
241 //FILE* fp = fopen_utf8name( filename, "r" );
242 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
244 // Try to open relative
245 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
246 if (!doc) {
247 doc = SP_ACTIVE_DOCUMENT;
248 g_warning("object has no document. using active");
249 }
250 //# 1. Get complete URI of document
251 gchar const *docbase = SP_DOCUMENT_URI( doc );
252 if (!docbase)
253 {
254 g_warning("null docbase");
255 docbase = "";
256 }
257 //g_message("docbase:%s\n", docbase);
258 org::w3c::dom::URI docUri(docbase);
259 //# 2. Get href of icc file. we don't care if it's rel or abs
260 org::w3c::dom::URI hrefUri(cprof->href);
261 //# 3. Resolve the href according the docBase. This follows
262 // the w3c specs. All absolute and relative issues are considered
263 org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
264 gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
265 cprof->_clearProfile();
266 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
267 if ( cprof->profHandle ) {
268 cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
269 cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
270 }
271 #ifdef DEBUG_LCMS
272 DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
273 #endif // DEBUG_LCMS
275 #endif // ENABLE_LCMS
276 }
277 }
278 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
279 break;
281 case SP_ATTR_LOCAL:
282 if ( cprof->local ) {
283 g_free( cprof->local );
284 cprof->local = 0;
285 }
286 cprof->local = g_strdup( value );
287 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
288 break;
290 case SP_ATTR_NAME:
291 if ( cprof->name ) {
292 g_free( cprof->name );
293 cprof->name = 0;
294 }
295 cprof->name = g_strdup( value );
296 #ifdef DEBUG_LCMS
297 DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
298 #endif // DEBUG_LCMS
299 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
300 break;
302 case SP_ATTR_RENDERING_INTENT:
303 if ( cprof->intentStr ) {
304 g_free( cprof->intentStr );
305 cprof->intentStr = 0;
306 }
307 cprof->intentStr = g_strdup( value );
309 if ( value ) {
310 if ( strcmp( value, "auto" ) == 0 ) {
311 cprof->rendering_intent = RENDERING_INTENT_AUTO;
312 } else if ( strcmp( value, "perceptual" ) == 0 ) {
313 cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
314 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
315 cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
316 } else if ( strcmp( value, "saturation" ) == 0 ) {
317 cprof->rendering_intent = RENDERING_INTENT_SATURATION;
318 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
319 cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
320 } else {
321 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
322 }
323 } else {
324 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
325 }
327 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
328 break;
330 default:
331 if (cprof_parent_class->set) {
332 (* cprof_parent_class->set)(object, key, value);
333 }
334 break;
335 }
337 }
339 /**
340 * Callback: write attributes to associated repr.
341 */
342 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
343 {
344 ColorProfile *cprof = COLORPROFILE(object);
346 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
347 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
348 repr = xml_doc->createElement("svg:color-profile");
349 }
351 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
352 repr->setAttribute( "xlink:href", cprof->href );
353 }
355 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
356 repr->setAttribute( "local", cprof->local );
357 }
359 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
360 repr->setAttribute( "name", cprof->name );
361 }
363 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
364 repr->setAttribute( "rendering-intent", cprof->intentStr );
365 }
367 if (cprof_parent_class->write) {
368 (* cprof_parent_class->write)(object, repr, flags);
369 }
371 return repr;
372 }
375 #if ENABLE_LCMS
377 struct MapMap {
378 icColorSpaceSignature space;
379 DWORD inForm;
380 };
382 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
383 {
384 MapMap possible[] = {
385 {icSigXYZData, TYPE_XYZ_16},
386 {icSigLabData, TYPE_Lab_16},
387 //icSigLuvData
388 {icSigYCbCrData, TYPE_YCbCr_16},
389 {icSigYxyData, TYPE_Yxy_16},
390 {icSigRgbData, TYPE_RGB_16},
391 {icSigGrayData, TYPE_GRAY_16},
392 {icSigHsvData, TYPE_HSV_16},
393 {icSigHlsData, TYPE_HLS_16},
394 {icSigCmykData, TYPE_CMYK_16},
395 {icSigCmyData, TYPE_CMY_16},
396 };
398 int index = 0;
399 for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
400 if ( possible[i].space == space ) {
401 index = i;
402 break;
403 }
404 }
406 return possible[index].inForm;
407 }
409 static int getLcmsIntent( guint svgIntent )
410 {
411 int intent = INTENT_PERCEPTUAL;
412 switch ( svgIntent ) {
413 case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
414 intent = INTENT_RELATIVE_COLORIMETRIC;
415 break;
416 case Inkscape::RENDERING_INTENT_SATURATION:
417 intent = INTENT_SATURATION;
418 break;
419 case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
420 intent = INTENT_ABSOLUTE_COLORIMETRIC;
421 break;
422 case Inkscape::RENDERING_INTENT_PERCEPTUAL:
423 case Inkscape::RENDERING_INTENT_UNKNOWN:
424 case Inkscape::RENDERING_INTENT_AUTO:
425 default:
426 intent = INTENT_PERCEPTUAL;
427 }
428 return intent;
429 }
431 static SPObject* bruteFind( SPDocument* document, gchar const* name )
432 {
433 SPObject* result = 0;
434 const GSList * current = sp_document_get_resource_list(document, "iccprofile");
435 while ( current && !result ) {
436 if ( IS_COLORPROFILE(current->data) ) {
437 ColorProfile* prof = COLORPROFILE(current->data);
438 if ( prof ) {
439 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
440 result = SP_OBJECT(current->data);
441 break;
442 }
443 }
444 }
445 current = g_slist_next(current);
446 }
448 return result;
449 }
451 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
452 {
453 cmsHPROFILE prof = 0;
455 SPObject* thing = bruteFind( document, name );
456 if ( thing ) {
457 prof = COLORPROFILE(thing)->profHandle;
458 }
460 if ( intent ) {
461 *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
462 }
464 #ifdef DEBUG_LCMS
465 DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
466 #endif // DEBUG_LCMS
468 return prof;
469 }
471 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
472 {
473 if ( !_transf ) {
474 int intent = getLcmsIntent(rendering_intent);
475 _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
476 }
477 return _transf;
478 }
480 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
481 {
482 if ( !_revTransf ) {
483 int intent = getLcmsIntent(rendering_intent);
484 _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
485 }
486 return _revTransf;
487 }
490 #include <io/sys.h>
492 class ProfileInfo
493 {
494 public:
495 ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
497 Glib::ustring const& getName() {return _name;}
498 Glib::ustring const& getPath() {return _path;}
499 icColorSpaceSignature getSpace() {return _profileSpace;}
500 icProfileClassSignature getClass() {return _profileClass;}
502 private:
503 Glib::ustring _path;
504 Glib::ustring _name;
505 icColorSpaceSignature _profileSpace;
506 icProfileClassSignature _profileClass;
507 };
510 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
511 {
512 _path = path;
513 _name = cmsTakeProductDesc(prof);
514 _profileSpace = cmsGetColorSpace( prof );
515 _profileClass = cmsGetDeviceClass( prof );
516 }
520 static std::vector<ProfileInfo> knownProfiles;
522 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
523 {
524 std::vector<Glib::ustring> result;
526 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
527 if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
528 result.push_back( it->getName() );
529 }
530 }
532 return result;
533 }
535 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
536 {
537 std::vector<Glib::ustring> result;
539 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
540 if ( it->getClass() == icSigOutputClass ) {
541 result.push_back( it->getName() );
542 }
543 }
545 return result;
546 }
548 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
549 {
550 Glib::ustring result;
552 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
553 if ( name == it->getName() ) {
554 result = it->getPath();
555 break;
556 }
557 }
559 return result;
560 }
562 static void findThings() {
563 std::list<gchar *> sources;
565 gchar* base = profile_path("XXX");
566 {
567 gchar* base2 = g_path_get_dirname(base);
568 g_free(base);
569 base = base2;
570 base2 = g_path_get_dirname(base);
571 g_free(base);
572 base = base2;
573 }
575 // first try user's local dir
576 sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
577 sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
579 const gchar* const * dataDirs = g_get_system_data_dirs();
580 for ( int i = 0; dataDirs[i]; i++ ) {
581 sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
582 }
584 while (!sources.empty()) {
585 gchar *dirname = sources.front();
586 if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
587 GError *err = 0;
588 GDir *dir = g_dir_open(dirname, 0, &err);
590 if (dir) {
591 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
592 gchar *filepath = g_build_filename(dirname, file, NULL);
595 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
596 sources.push_back(g_strdup(filepath));
597 } else {
598 bool isIccFile = false;
599 struct stat st;
600 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
601 //0-3 == size
602 //36-39 == 'acsp' 0x61637370
603 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
604 if ( fd != -1 ) {
605 guchar scratch[40] = {0};
606 size_t len = sizeof(scratch);
608 //size_t left = 40;
609 ssize_t got = read(fd, scratch, len);
610 if ( got != -1 ) {
611 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
612 if ( calcSize > 128 && calcSize <= st.st_size ) {
613 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
614 }
615 }
617 close(fd);
618 }
619 }
621 if ( isIccFile ) {
622 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
623 if ( prof ) {
624 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
625 cmsCloseProfile( prof );
627 bool sameName = false;
628 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
629 if ( it->getName() == info.getName() ) {
630 sameName = true;
631 break;
632 }
633 }
635 if ( !sameName ) {
636 knownProfiles.push_back(info);
637 }
638 }
639 }
640 }
642 g_free(filepath);
643 }
644 }
645 }
647 // toss the dirname
648 g_free(dirname);
649 sources.pop_front();
650 }
651 }
653 int errorHandlerCB(int ErrorCode, const char *ErrorText)
654 {
655 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
657 return 1;
658 }
660 static bool gamutWarn = false;
661 static Gdk::Color lastGamutColor("#808080");
662 static bool lastBPC = false;
663 #if defined(cmsFLAGS_PRESERVEBLACK)
664 static bool lastPreserveBlack = false;
665 #endif // defined(cmsFLAGS_PRESERVEBLACK)
666 static int lastIntent = INTENT_PERCEPTUAL;
667 static int lastProofIntent = INTENT_PERCEPTUAL;
668 static cmsHTRANSFORM transf = 0;
670 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
671 {
672 static cmsHPROFILE theOne = 0;
673 static std::string lastURI;
675 static bool init = false;
676 if ( !init ) {
677 cmsSetErrorHandler(errorHandlerCB);
679 findThings();
680 init = true;
681 }
683 long long int which = prefs_get_int_attribute_limited( "options.displayprofile", "enable", 0, 0, 1 );
684 gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
686 if ( which && 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;
726 }
729 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
730 {
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;
791 }
793 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
794 {
795 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
796 int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
797 int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
798 bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
799 #if defined(cmsFLAGS_PRESERVEBLACK)
800 bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
801 #endif //defined(cmsFLAGS_PRESERVEBLACK)
802 gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
803 Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
805 if ( (warn != gamutWarn)
806 || (lastIntent != intent)
807 || (lastProofIntent != proofIntent)
808 || (bpc != lastBPC)
809 #if defined(cmsFLAGS_PRESERVEBLACK)
810 || (preserveBlack != lastPreserveBlack)
811 #endif // defined(cmsFLAGS_PRESERVEBLACK)
812 || (gamutColor != lastGamutColor)
813 ) {
814 gamutWarn = warn;
815 if ( transf ) {
816 cmsDeleteTransform(transf);
817 transf = 0;
818 }
819 lastIntent = intent;
820 lastProofIntent = proofIntent;
821 lastBPC = bpc;
822 #if defined(cmsFLAGS_PRESERVEBLACK)
823 lastPreserveBlack = preserveBlack;
824 #endif // defined(cmsFLAGS_PRESERVEBLACK)
825 lastGamutColor = gamutColor;
826 }
828 // Fecth these now, as they might clear the transform as a side effect.
829 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
830 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
832 if ( !transf ) {
833 if ( hprof && proofProf ) {
834 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
835 if ( gamutWarn ) {
836 dwFlags |= cmsFLAGS_GAMUTCHECK;
837 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
838 }
839 if ( bpc ) {
840 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
841 }
842 #if defined(cmsFLAGS_PRESERVEBLACK)
843 if ( preserveBlack ) {
844 dwFlags |= cmsFLAGS_PRESERVEBLACK;
845 }
846 #endif // defined(cmsFLAGS_PRESERVEBLACK)
847 transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
848 } else if ( hprof ) {
849 transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
850 }
851 }
853 return transf;
854 }
856 #endif // ENABLE_LCMS
858 /*
859 Local Variables:
860 mode:c++
861 c-file-style:"stroustrup"
862 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
863 indent-tabs-mode:nil
864 fill-column:99
865 End:
866 */
867 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :