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>
18 #ifdef DEBUG_LCMS
19 #include <gtk/gtkmessagedialog.h>
20 #endif // DEBUG_LCMS
22 using Inkscape::ColorProfile;
23 using Inkscape::ColorProfileClass;
25 namespace Inkscape
26 {
27 static void colorprofile_class_init( ColorProfileClass *klass );
28 static void colorprofile_init( ColorProfile *cprof );
30 static void colorprofile_release( SPObject *object );
31 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr );
32 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value );
33 static Inkscape::XML::Node *colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags );
35 static cmsHPROFILE colorprofile_get_system_profile_handle();
36 static cmsHPROFILE colorprofile_get_proof_profile_handle();
37 }
39 #ifdef DEBUG_LCMS
40 extern guint update_in_progress;
41 #define DEBUG_MESSAGE(key, ...) \
42 {\
43 gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
44 gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
45 gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
46 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
47 if ( dump )\
48 {\
49 g_message( __VA_ARGS__ );\
50 \
51 }\
52 if ( dumpD )\
53 {\
54 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
55 GTK_DIALOG_DESTROY_WITH_PARENT, \
56 GTK_MESSAGE_INFO, \
57 GTK_BUTTONS_OK, \
58 __VA_ARGS__ \
59 );\
60 g_signal_connect_swapped(dialog, "response",\
61 G_CALLBACK(gtk_widget_destroy), \
62 dialog); \
63 gtk_widget_show_all( dialog );\
64 }\
65 }
66 #endif // DEBUG_LCMS
68 static SPObject *cprof_parent_class;
70 /**
71 * Register ColorProfile class and return its type.
72 */
73 GType Inkscape::colorprofile_get_type()
74 {
75 static GType type = 0;
76 if (!type) {
77 GTypeInfo info = {
78 sizeof(ColorProfileClass),
79 NULL, NULL,
80 (GClassInitFunc) colorprofile_class_init,
81 NULL, NULL,
82 sizeof(ColorProfile),
83 16,
84 (GInstanceInitFunc) colorprofile_init,
85 NULL, /* value_table */
86 };
87 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
88 }
89 return type;
90 }
92 /**
93 * ColorProfile vtable initialization.
94 */
95 static void Inkscape::colorprofile_class_init( ColorProfileClass *klass )
96 {
97 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
99 cprof_parent_class = static_cast<SPObject*>(g_type_class_ref(SP_TYPE_OBJECT));
101 sp_object_class->release = colorprofile_release;
102 sp_object_class->build = colorprofile_build;
103 sp_object_class->set = colorprofile_set;
104 sp_object_class->write = colorprofile_write;
105 }
107 /**
108 * Callback for ColorProfile object initialization.
109 */
110 static void Inkscape::colorprofile_init( ColorProfile *cprof )
111 {
112 cprof->href = 0;
113 cprof->local = 0;
114 cprof->name = 0;
115 cprof->intentStr = 0;
116 cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
117 #if ENABLE_LCMS
118 cprof->profHandle = 0;
119 #endif // ENABLE_LCMS
120 }
122 /**
123 * Callback: free object
124 */
125 static void Inkscape::colorprofile_release( SPObject *object )
126 {
127 ColorProfile *cprof = COLORPROFILE(object);
128 if ( cprof->href ) {
129 g_free( cprof->href );
130 cprof->href = 0;
131 }
133 if ( cprof->local ) {
134 g_free( cprof->local );
135 cprof->local = 0;
136 }
138 if ( cprof->name ) {
139 g_free( cprof->name );
140 cprof->name = 0;
141 }
143 if ( cprof->intentStr ) {
144 g_free( cprof->intentStr );
145 cprof->intentStr = 0;
146 }
148 #if ENABLE_LCMS
149 if ( cprof->profHandle ) {
150 cmsCloseProfile( cprof->profHandle );
151 cprof->profHandle = 0;
152 }
153 #endif // ENABLE_LCMS
154 }
156 /**
157 * Callback: set attributes from associated repr.
158 */
159 static void Inkscape::colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
160 {
161 ColorProfile *cprof = COLORPROFILE(object);
162 g_assert(cprof->href == 0);
163 g_assert(cprof->local == 0);
164 g_assert(cprof->name == 0);
165 g_assert(cprof->intentStr == 0);
167 if (((SPObjectClass *) cprof_parent_class)->build) {
168 (* ((SPObjectClass *) cprof_parent_class)->build)(object, document, repr);
169 }
170 sp_object_read_attr( object, "xlink:href" );
171 sp_object_read_attr( object, "local" );
172 sp_object_read_attr( object, "name" );
173 sp_object_read_attr( object, "rendering-intent" );
174 }
176 /**
177 * Callback: set attribute.
178 */
179 static void Inkscape::colorprofile_set( SPObject *object, unsigned key, gchar const *value )
180 {
181 ColorProfile *cprof = COLORPROFILE(object);
183 switch (key) {
184 case SP_ATTR_XLINK_HREF:
185 if ( cprof->href ) {
186 g_free( cprof->href );
187 cprof->href = 0;
188 }
189 if ( value ) {
190 cprof->href = g_strdup( value );
191 if ( *cprof->href ) {
192 #if ENABLE_LCMS
193 cmsErrorAction( LCMS_ERROR_SHOW );
195 // TODO open filename and URIs properly
196 //FILE* fp = fopen_utf8name( filename, "r" );
197 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
199 // Try to open relative
200 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
201 if (!doc) {
202 doc = SP_ACTIVE_DOCUMENT;
203 g_warning("object has no document. using active");
204 }
205 //# 1. Get complete URI of document
206 gchar const *docbase = SP_DOCUMENT_URI( doc );
207 if (!docbase)
208 {
209 g_warning("null docbase");
210 docbase = "";
211 }
212 //g_message("docbase:%s\n", docbase);
213 org::w3c::dom::URI docUri(docbase);
214 //# 2. Get href of icc file. we don't care if it's rel or abs
215 org::w3c::dom::URI hrefUri(cprof->href);
216 //# 3. Resolve the href according the docBase. This follows
217 // the w3c specs. All absolute and relative issues are considered
218 org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
219 gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
220 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
221 #ifdef DEBUG_LCMS
222 DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
223 #endif // DEBUG_LCMS
225 #endif // ENABLE_LCMS
226 }
227 }
228 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
229 break;
231 case SP_ATTR_LOCAL:
232 if ( cprof->local ) {
233 g_free( cprof->local );
234 cprof->local = 0;
235 }
236 cprof->local = g_strdup( value );
237 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
238 break;
240 case SP_ATTR_NAME:
241 if ( cprof->name ) {
242 g_free( cprof->name );
243 cprof->name = 0;
244 }
245 cprof->name = g_strdup( value );
246 #ifdef DEBUG_LCMS
247 DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
248 #endif // DEBUG_LCMS
249 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
250 break;
252 case SP_ATTR_RENDERING_INTENT:
253 if ( cprof->intentStr ) {
254 g_free( cprof->intentStr );
255 cprof->intentStr = 0;
256 }
257 cprof->intentStr = g_strdup( value );
259 if ( value ) {
260 if ( strcmp( value, "auto" ) == 0 ) {
261 cprof->rendering_intent = RENDERING_INTENT_AUTO;
262 } else if ( strcmp( value, "perceptual" ) == 0 ) {
263 cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
264 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
265 cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
266 } else if ( strcmp( value, "saturation" ) == 0 ) {
267 cprof->rendering_intent = RENDERING_INTENT_SATURATION;
268 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
269 cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
270 } else {
271 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
272 }
273 } else {
274 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
275 }
277 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
278 break;
280 default:
281 if (((SPObjectClass *) cprof_parent_class)->set) {
282 (* ((SPObjectClass *) cprof_parent_class)->set)(object, key, value);
283 }
284 break;
285 }
287 }
289 /**
290 * Callback: write attributes to associated repr.
291 */
292 static Inkscape::XML::Node* Inkscape::colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
293 {
294 ColorProfile *cprof = COLORPROFILE(object);
296 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
297 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
298 repr = xml_doc->createElement("svg:color-profile");
299 }
301 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
302 repr->setAttribute( "xlink:href", cprof->href );
303 }
305 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
306 repr->setAttribute( "local", cprof->local );
307 }
309 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
310 repr->setAttribute( "name", cprof->name );
311 }
313 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
314 repr->setAttribute( "rendering-intent", cprof->intentStr );
315 }
317 if (((SPObjectClass *) cprof_parent_class)->write) {
318 (* ((SPObjectClass *) cprof_parent_class)->write)(object, repr, flags);
319 }
321 return repr;
322 }
325 #if ENABLE_LCMS
328 static SPObject* bruteFind( SPObject* curr, gchar const* name )
329 {
330 SPObject* result = 0;
332 if ( curr ) {
333 if ( IS_COLORPROFILE(curr) ) {
334 ColorProfile* prof = COLORPROFILE(curr);
335 if ( prof ) {
336 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
337 result = curr;
338 }
339 }
340 } else {
341 if ( curr->hasChildren() ) {
342 SPObject* child = curr->firstChild();
343 while ( child && !result ) {
344 result = bruteFind( child, name );
345 if ( !result ) {
346 child = child->next;
347 }
348 };
349 }
350 }
351 }
353 return result;
354 }
356 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
357 {
358 cmsHPROFILE prof = 0;
360 SPObject* root = SP_DOCUMENT_ROOT(document);
361 SPObject* thing = bruteFind( root, name );
362 if ( thing ) {
363 prof = COLORPROFILE(thing)->profHandle;
364 }
366 if ( intent ) {
367 *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
368 }
370 #ifdef DEBUG_LCMS
371 DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
372 #endif // DEBUG_LCMS
374 return prof;
375 }
378 #include <io/sys.h>
380 class ProfileInfo
381 {
382 public:
383 ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
385 Glib::ustring const& getName() {return _name;}
386 Glib::ustring const& getPath() {return _path;}
387 icColorSpaceSignature getSpace() {return _profileSpace;}
388 icProfileClassSignature getClass() {return _profileClass;}
390 private:
391 Glib::ustring _path;
392 Glib::ustring _name;
393 icColorSpaceSignature _profileSpace;
394 icProfileClassSignature _profileClass;
395 };
398 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
399 {
400 _path = path;
401 _name = cmsTakeProductName(prof);
402 _profileSpace = cmsGetColorSpace( prof );
403 _profileClass = cmsGetDeviceClass( prof );
404 }
408 static std::vector<ProfileInfo> knownProfiles;
410 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
411 {
412 std::vector<Glib::ustring> result;
414 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
415 if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
416 result.push_back( it->getName() );
417 }
418 }
420 return result;
421 }
423 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
424 {
425 std::vector<Glib::ustring> result;
427 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
428 if ( it->getClass() == icSigOutputClass ) {
429 result.push_back( it->getName() );
430 }
431 }
433 return result;
434 }
436 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
437 {
438 Glib::ustring result;
440 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
441 if ( name == it->getName() ) {
442 result = it->getPath();
443 break;
444 }
445 }
447 return result;
448 }
450 static void findThings() {
451 std::list<gchar *> sources;
453 gchar* base = profile_path("XXX");
454 {
455 gchar* base2 = g_path_get_dirname(base);
456 g_free(base);
457 base = base2;
458 base2 = g_path_get_dirname(base);
459 g_free(base);
460 base = base2;
461 }
463 // first try user's local dir
464 sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
465 sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
467 const gchar* const * dataDirs = g_get_system_data_dirs();
468 for ( int i = 0; dataDirs[i]; i++ ) {
469 sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
470 }
472 while (!sources.empty()) {
473 gchar *dirname = sources.front();
474 if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
475 GError *err = 0;
476 GDir *dir = g_dir_open(dirname, 0, &err);
478 if (dir) {
479 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
480 gchar *filepath = g_build_filename(dirname, file, NULL);
483 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
484 sources.push_back(g_strdup(filepath));
485 } else {
486 bool isIccFile = false;
487 struct stat st;
488 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
489 //0-3 == size
490 //36-39 == 'acsp' 0x61637370
491 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
492 if ( fd != -1 ) {
493 guchar scratch[40] = {0};
494 size_t len = sizeof(scratch);
496 //size_t left = 40;
497 ssize_t got = read(fd, scratch, len);
498 if ( got != -1 ) {
499 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
500 if ( calcSize > 128 && calcSize <= st.st_size ) {
501 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
502 }
503 }
505 close(fd);
506 }
507 }
509 if ( isIccFile ) {
510 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
511 if ( prof ) {
512 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
513 cmsCloseProfile( prof );
515 bool sameName = false;
516 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
517 if ( it->getName() == info.getName() ) {
518 sameName = true;
519 break;
520 }
521 }
523 if ( !sameName ) {
524 knownProfiles.push_back(info);
525 }
526 }
527 }
528 }
530 g_free(filepath);
531 }
532 }
533 }
535 // toss the dirname
536 g_free(dirname);
537 sources.pop_front();
538 }
539 }
541 int errorHandlerCB(int ErrorCode, const char *ErrorText)
542 {
543 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
545 return 1;
546 }
548 static bool gamutWarn = false;
549 static cmsHTRANSFORM transf = 0;
550 static cmsHPROFILE srcprof = 0;
552 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
553 {
554 static cmsHPROFILE theOne = 0;
555 static std::string lastURI;
557 static bool init = false;
558 if ( !init ) {
559 cmsSetErrorHandler(errorHandlerCB);
561 findThings();
562 init = true;
563 }
565 long long int which = prefs_get_int_attribute_limited( "options.displayprofile", "enable", 0, 0, 1 );
566 gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
568 if ( which && uri && *uri ) {
569 if ( lastURI != std::string(uri) ) {
570 lastURI.clear();
571 if ( theOne ) {
572 cmsCloseProfile( theOne );
573 }
574 if ( transf ) {
575 cmsDeleteTransform( transf );
576 transf = 0;
577 }
578 theOne = cmsOpenProfileFromFile( uri, "r" );
579 if ( theOne ) {
580 // a display profile must have the proper stuff
581 icColorSpaceSignature space = cmsGetColorSpace(theOne);
582 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
584 if ( profClass != icSigDisplayClass ) {
585 g_warning("Not a display profile");
586 cmsCloseProfile( theOne );
587 theOne = 0;
588 } else if ( space != icSigRgbData ) {
589 g_warning("Not an RGB profile");
590 cmsCloseProfile( theOne );
591 theOne = 0;
592 } else {
593 lastURI = uri;
594 }
595 }
596 }
597 } else if ( theOne ) {
598 cmsCloseProfile( theOne );
599 theOne = 0;
600 lastURI.clear();
601 if ( transf ) {
602 cmsDeleteTransform( transf );
603 transf = 0;
604 }
605 }
607 return theOne;
608 }
611 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
612 {
613 static cmsHPROFILE theOne = 0;
614 static std::string lastURI;
616 static bool init = false;
617 if ( !init ) {
618 cmsSetErrorHandler(errorHandlerCB);
620 findThings();
621 init = true;
622 }
624 long long int which = prefs_get_int_attribute_limited( "options.softproof", "enable", 0, 0, 1 );
625 gchar const * uri = prefs_get_string_attribute("options.softproof", "uri");
627 if ( which && uri && *uri ) {
628 if ( lastURI != std::string(uri) ) {
629 lastURI.clear();
630 if ( theOne ) {
631 cmsCloseProfile( theOne );
632 }
633 if ( transf ) {
634 cmsDeleteTransform( transf );
635 transf = 0;
636 }
637 theOne = cmsOpenProfileFromFile( uri, "r" );
638 if ( theOne ) {
639 // a display profile must have the proper stuff
640 icColorSpaceSignature space = cmsGetColorSpace(theOne);
641 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
643 /*
644 if ( profClass != icSigDisplayClass ) {
645 g_warning("Not a display profile");
646 cmsCloseProfile( theOne );
647 theOne = 0;
648 } else if ( space != icSigRgbData ) {
649 g_warning("Not an RGB profile");
650 cmsCloseProfile( theOne );
651 theOne = 0;
652 } else {
653 */
654 lastURI = uri;
655 /*
656 }
657 */
658 }
659 }
660 } else if ( theOne ) {
661 cmsCloseProfile( theOne );
662 theOne = 0;
663 lastURI.clear();
664 if ( transf ) {
665 cmsDeleteTransform( transf );
666 transf = 0;
667 }
668 }
670 return theOne;
671 }
673 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
674 {
675 bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
676 if ( (warn != gamutWarn) ) {
677 gamutWarn = warn;
678 if ( transf ) {
679 cmsDeleteTransform(transf);
680 transf = 0;
681 }
682 }
684 // Fecth these now, as they might clear the transform as a side effect.
685 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
686 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
688 if ( !transf ) {
689 if ( !srcprof ) {
690 srcprof = cmsCreate_sRGBProfile();
691 }
692 if ( hprof && proofProf ) {
693 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
694 if ( gamutWarn ) {
695 dwFlags |= cmsFLAGS_GAMUTCHECK;
696 }
697 cmsSetAlarmCodes(0, 255, 0);
698 transf = cmsCreateProofingTransform( srcprof, TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, INTENT_PERCEPTUAL, INTENT_PERCEPTUAL, dwFlags );
699 } else if ( hprof ) {
700 transf = cmsCreateTransform( srcprof, TYPE_RGB_8, hprof, TYPE_RGB_8, INTENT_PERCEPTUAL, 0 );
701 }
702 }
704 return transf;
705 }
707 #endif // ENABLE_LCMS
709 /*
710 Local Variables:
711 mode:c++
712 c-file-style:"stroustrup"
713 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
714 indent-tabs-mode:nil
715 fill-column:99
716 End:
717 */
718 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :