1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
5 #define noDEBUG_LCMS
7 #include <glib/gstdio.h>
8 #include <sys/fcntl.h>
9 #include <gdkmm/color.h>
11 #ifdef DEBUG_LCMS
12 #include <gtk/gtkmessagedialog.h>
13 #endif // DEBUG_LCMS
15 #include <cstring>
16 #include <string>
18 #ifdef WIN32
19 #ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later. Required for correctly including icm.h
20 #define _WIN32_WINDOWS 0x0410
21 #endif
22 #include <windows.h>
23 #endif
25 #include "xml/repr.h"
26 #include "color.h"
27 #include "color-profile.h"
28 #include "color-profile-fns.h"
29 #include "attributes.h"
30 #include "inkscape.h"
31 #include "document.h"
32 #include "preferences.h"
34 #include "dom/uri.h"
35 #include "dom/util/digest.h"
37 #ifdef WIN32
38 #include <icm.h>
39 #endif // WIN32
41 using Inkscape::ColorProfile;
42 using Inkscape::ColorProfileClass;
44 namespace Inkscape
45 {
46 #if ENABLE_LCMS
47 static cmsHPROFILE colorprofile_get_system_profile_handle();
48 static cmsHPROFILE colorprofile_get_proof_profile_handle();
49 #endif // ENABLE_LCMS
50 }
52 #ifdef DEBUG_LCMS
53 extern guint update_in_progress;
54 #define DEBUG_MESSAGE_SCISLAC(key, ...) \
55 {\
56 Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
57 bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
58 bool dumpD = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D");\
59 bool dumpD2 = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D2");\
60 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
61 if ( dump )\
62 {\
63 g_message( __VA_ARGS__ );\
64 \
65 }\
66 if ( dumpD )\
67 {\
68 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
69 GTK_DIALOG_DESTROY_WITH_PARENT, \
70 GTK_MESSAGE_INFO, \
71 GTK_BUTTONS_OK, \
72 __VA_ARGS__ \
73 );\
74 g_signal_connect_swapped(dialog, "response",\
75 G_CALLBACK(gtk_widget_destroy), \
76 dialog); \
77 gtk_widget_show_all( dialog );\
78 }\
79 }
82 #define DEBUG_MESSAGE(key, ...)\
83 {\
84 g_message( __VA_ARGS__ );\
85 }
87 #endif // DEBUG_LCMS
89 static SPObjectClass *cprof_parent_class;
91 #if ENABLE_LCMS
93 cmsHPROFILE ColorProfile::_sRGBProf = 0;
95 cmsHPROFILE ColorProfile::getSRGBProfile() {
96 if ( !_sRGBProf ) {
97 _sRGBProf = cmsCreate_sRGBProfile();
98 }
99 return _sRGBProf;
100 }
102 cmsHPROFILE ColorProfile::_NullProf = 0;
104 cmsHPROFILE ColorProfile::getNULLProfile() {
105 if ( !_NullProf ) {
106 _NullProf = cmsCreateNULLProfile();
107 }
108 return _NullProf;
109 }
111 #endif // ENABLE_LCMS
113 /**
114 * Register ColorProfile class and return its type.
115 */
116 GType Inkscape::colorprofile_get_type()
117 {
118 return ColorProfile::getType();
119 }
121 GType ColorProfile::getType()
122 {
123 static GType type = 0;
124 if (!type) {
125 GTypeInfo info = {
126 sizeof(ColorProfileClass),
127 NULL, NULL,
128 (GClassInitFunc) ColorProfile::classInit,
129 NULL, NULL,
130 sizeof(ColorProfile),
131 16,
132 (GInstanceInitFunc) ColorProfile::init,
133 NULL, /* value_table */
134 };
135 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
136 }
137 return type;
138 }
140 /**
141 * ColorProfile vtable initialization.
142 */
143 void ColorProfile::classInit( ColorProfileClass *klass )
144 {
145 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
147 cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
149 sp_object_class->release = ColorProfile::release;
150 sp_object_class->build = ColorProfile::build;
151 sp_object_class->set = ColorProfile::set;
152 sp_object_class->write = ColorProfile::write;
153 }
155 /**
156 * Callback for ColorProfile object initialization.
157 */
158 void ColorProfile::init( ColorProfile *cprof )
159 {
160 cprof->href = 0;
161 cprof->local = 0;
162 cprof->name = 0;
163 cprof->intentStr = 0;
164 cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
165 #if ENABLE_LCMS
166 cprof->profHandle = 0;
167 cprof->_profileClass = icSigInputClass;
168 cprof->_profileSpace = icSigRgbData;
169 cprof->_transf = 0;
170 cprof->_revTransf = 0;
171 cprof->_gamutTransf = 0;
172 #endif // ENABLE_LCMS
173 }
175 /**
176 * Callback: free object
177 */
178 void ColorProfile::release( SPObject *object )
179 {
180 // Unregister ourselves
181 SPDocument* document = SP_OBJECT_DOCUMENT(object);
182 if ( document ) {
183 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
184 }
186 ColorProfile *cprof = COLORPROFILE(object);
187 if ( cprof->href ) {
188 g_free( cprof->href );
189 cprof->href = 0;
190 }
192 if ( cprof->local ) {
193 g_free( cprof->local );
194 cprof->local = 0;
195 }
197 if ( cprof->name ) {
198 g_free( cprof->name );
199 cprof->name = 0;
200 }
202 if ( cprof->intentStr ) {
203 g_free( cprof->intentStr );
204 cprof->intentStr = 0;
205 }
207 #if ENABLE_LCMS
208 cprof->_clearProfile();
209 #endif // ENABLE_LCMS
210 }
212 #if ENABLE_LCMS
213 void ColorProfile::_clearProfile()
214 {
215 _profileSpace = icSigRgbData;
217 if ( _transf ) {
218 cmsDeleteTransform( _transf );
219 _transf = 0;
220 }
221 if ( _revTransf ) {
222 cmsDeleteTransform( _revTransf );
223 _revTransf = 0;
224 }
225 if ( _gamutTransf ) {
226 cmsDeleteTransform( _gamutTransf );
227 _gamutTransf = 0;
228 }
229 if ( profHandle ) {
230 cmsCloseProfile( profHandle );
231 profHandle = 0;
232 }
233 }
234 #endif // ENABLE_LCMS
236 /**
237 * Callback: set attributes from associated repr.
238 */
239 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
240 {
241 ColorProfile *cprof = COLORPROFILE(object);
242 g_assert(cprof->href == 0);
243 g_assert(cprof->local == 0);
244 g_assert(cprof->name == 0);
245 g_assert(cprof->intentStr == 0);
247 if (cprof_parent_class->build) {
248 (* cprof_parent_class->build)(object, document, repr);
249 }
250 sp_object_read_attr( object, "xlink:href" );
251 sp_object_read_attr( object, "local" );
252 sp_object_read_attr( object, "name" );
253 sp_object_read_attr( object, "rendering-intent" );
255 // Register
256 if ( document ) {
257 sp_document_add_resource( document, "iccprofile", object );
258 }
259 }
261 /**
262 * Callback: set attribute.
263 */
264 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
265 {
266 ColorProfile *cprof = COLORPROFILE(object);
268 switch (key) {
269 case SP_ATTR_XLINK_HREF:
270 if ( cprof->href ) {
271 g_free( cprof->href );
272 cprof->href = 0;
273 }
274 if ( value ) {
275 cprof->href = g_strdup( value );
276 if ( *cprof->href ) {
277 #if ENABLE_LCMS
278 cmsErrorAction( LCMS_ERROR_SHOW );
280 // TODO open filename and URIs properly
281 //FILE* fp = fopen_utf8name( filename, "r" );
282 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
284 // Try to open relative
285 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
286 if (!doc) {
287 doc = SP_ACTIVE_DOCUMENT;
288 g_warning("object has no document. using active");
289 }
290 //# 1. Get complete URI of document
291 gchar const *docbase = SP_DOCUMENT_URI( doc );
292 if (!docbase)
293 {
294 // Normal for files that have not yet been saved.
295 docbase = "";
296 }
298 gchar* escaped = g_uri_escape_string(cprof->href, "!*'();:@=+$,/?#[]", TRUE);
300 //g_message("docbase:%s\n", docbase);
301 org::w3c::dom::URI docUri(docbase);
302 //# 2. Get href of icc file. we don't care if it's rel or abs
303 org::w3c::dom::URI hrefUri(escaped);
304 //# 3. Resolve the href according the docBase. This follows
305 // the w3c specs. All absolute and relative issues are considered
306 org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
307 gchar* fullname = g_uri_unescape_string(cprofUri.getNativePath().c_str(), "");
308 cprof->_clearProfile();
309 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
310 if ( cprof->profHandle ) {
311 cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
312 cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
313 }
314 #ifdef DEBUG_LCMS
315 DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
316 #endif // DEBUG_LCMS
317 g_free(escaped);
318 escaped = 0;
319 g_free(fullname);
320 #endif // ENABLE_LCMS
321 }
322 }
323 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
324 break;
326 case SP_ATTR_LOCAL:
327 if ( cprof->local ) {
328 g_free( cprof->local );
329 cprof->local = 0;
330 }
331 cprof->local = g_strdup( value );
332 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
333 break;
335 case SP_ATTR_NAME:
336 if ( cprof->name ) {
337 g_free( cprof->name );
338 cprof->name = 0;
339 }
340 cprof->name = g_strdup( value );
341 #ifdef DEBUG_LCMS
342 DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
343 #endif // DEBUG_LCMS
344 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
345 break;
347 case SP_ATTR_RENDERING_INTENT:
348 if ( cprof->intentStr ) {
349 g_free( cprof->intentStr );
350 cprof->intentStr = 0;
351 }
352 cprof->intentStr = g_strdup( value );
354 if ( value ) {
355 if ( strcmp( value, "auto" ) == 0 ) {
356 cprof->rendering_intent = RENDERING_INTENT_AUTO;
357 } else if ( strcmp( value, "perceptual" ) == 0 ) {
358 cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
359 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
360 cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
361 } else if ( strcmp( value, "saturation" ) == 0 ) {
362 cprof->rendering_intent = RENDERING_INTENT_SATURATION;
363 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
364 cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
365 } else {
366 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
367 }
368 } else {
369 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
370 }
372 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
373 break;
375 default:
376 if (cprof_parent_class->set) {
377 (* cprof_parent_class->set)(object, key, value);
378 }
379 break;
380 }
382 }
384 /**
385 * Callback: write attributes to associated repr.
386 */
387 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
388 {
389 ColorProfile *cprof = COLORPROFILE(object);
391 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
392 repr = xml_doc->createElement("svg:color-profile");
393 }
395 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
396 repr->setAttribute( "xlink:href", cprof->href );
397 }
399 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
400 repr->setAttribute( "local", cprof->local );
401 }
403 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
404 repr->setAttribute( "name", cprof->name );
405 }
407 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
408 repr->setAttribute( "rendering-intent", cprof->intentStr );
409 }
411 if (cprof_parent_class->write) {
412 (* cprof_parent_class->write)(object, xml_doc, repr, flags);
413 }
415 return repr;
416 }
419 #if ENABLE_LCMS
421 struct MapMap {
422 icColorSpaceSignature space;
423 DWORD inForm;
424 };
426 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
427 {
428 MapMap possible[] = {
429 {icSigXYZData, TYPE_XYZ_16},
430 {icSigLabData, TYPE_Lab_16},
431 //icSigLuvData
432 {icSigYCbCrData, TYPE_YCbCr_16},
433 {icSigYxyData, TYPE_Yxy_16},
434 {icSigRgbData, TYPE_RGB_16},
435 {icSigGrayData, TYPE_GRAY_16},
436 {icSigHsvData, TYPE_HSV_16},
437 {icSigHlsData, TYPE_HLS_16},
438 {icSigCmykData, TYPE_CMYK_16},
439 {icSigCmyData, TYPE_CMY_16},
440 };
442 int index = 0;
443 for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
444 if ( possible[i].space == space ) {
445 index = i;
446 break;
447 }
448 }
450 return possible[index].inForm;
451 }
453 static int getLcmsIntent( guint svgIntent )
454 {
455 int intent = INTENT_PERCEPTUAL;
456 switch ( svgIntent ) {
457 case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
458 intent = INTENT_RELATIVE_COLORIMETRIC;
459 break;
460 case Inkscape::RENDERING_INTENT_SATURATION:
461 intent = INTENT_SATURATION;
462 break;
463 case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
464 intent = INTENT_ABSOLUTE_COLORIMETRIC;
465 break;
466 case Inkscape::RENDERING_INTENT_PERCEPTUAL:
467 case Inkscape::RENDERING_INTENT_UNKNOWN:
468 case Inkscape::RENDERING_INTENT_AUTO:
469 default:
470 intent = INTENT_PERCEPTUAL;
471 }
472 return intent;
473 }
475 static SPObject* bruteFind( SPDocument* document, gchar const* name )
476 {
477 SPObject* result = 0;
478 const GSList * current = sp_document_get_resource_list(document, "iccprofile");
479 while ( current && !result ) {
480 if ( IS_COLORPROFILE(current->data) ) {
481 ColorProfile* prof = COLORPROFILE(current->data);
482 if ( prof ) {
483 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
484 result = SP_OBJECT(current->data);
485 break;
486 }
487 }
488 }
489 current = g_slist_next(current);
490 }
492 return result;
493 }
495 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
496 {
497 cmsHPROFILE prof = 0;
499 SPObject* thing = bruteFind( document, name );
500 if ( thing ) {
501 prof = COLORPROFILE(thing)->profHandle;
502 }
504 if ( intent ) {
505 *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
506 }
508 #ifdef DEBUG_LCMS
509 DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
510 #endif // DEBUG_LCMS
512 return prof;
513 }
515 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
516 {
517 if ( !_transf && profHandle ) {
518 int intent = getLcmsIntent(rendering_intent);
519 _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
520 }
521 return _transf;
522 }
524 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
525 {
526 if ( !_revTransf && profHandle ) {
527 int intent = getLcmsIntent(rendering_intent);
528 _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
529 }
530 return _revTransf;
531 }
533 cmsHTRANSFORM ColorProfile::getTransfGamutCheck()
534 {
535 if ( !_gamutTransf ) {
536 _gamutTransf = cmsCreateProofingTransform(getSRGBProfile(), TYPE_RGBA_8, getNULLProfile(), TYPE_GRAY_8, profHandle, INTENT_RELATIVE_COLORIMETRIC, INTENT_RELATIVE_COLORIMETRIC, (cmsFLAGS_GAMUTCHECK|cmsFLAGS_SOFTPROOFING));
537 }
538 return _gamutTransf;
539 }
541 bool ColorProfile::GamutCheck(SPColor color){
542 BYTE outofgamut = 0;
544 guint32 val = color.toRGBA32(0);
545 guchar check_color[4] = {
546 SP_RGBA32_R_U(val),
547 SP_RGBA32_G_U(val),
548 SP_RGBA32_B_U(val),
549 255};
551 int alarm_r, alarm_g, alarm_b;
552 cmsGetAlarmCodes(&alarm_r, &alarm_g, &alarm_b);
553 cmsSetAlarmCodes(255, 255, 255);
554 cmsDoTransform(ColorProfile::getTransfGamutCheck(), &check_color, &outofgamut, 1);
555 cmsSetAlarmCodes(alarm_r, alarm_g, alarm_b);
556 return (outofgamut == 255);
557 }
560 #include <io/sys.h>
562 class ProfileInfo
563 {
564 public:
565 ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
567 Glib::ustring const& getName() {return _name;}
568 Glib::ustring const& getPath() {return _path;}
569 icColorSpaceSignature getSpace() {return _profileSpace;}
570 icProfileClassSignature getClass() {return _profileClass;}
572 private:
573 Glib::ustring _path;
574 Glib::ustring _name;
575 icColorSpaceSignature _profileSpace;
576 icProfileClassSignature _profileClass;
577 };
580 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
581 {
582 _path = path;
583 _name = cmsTakeProductDesc(prof);
584 _profileSpace = cmsGetColorSpace( prof );
585 _profileClass = cmsGetDeviceClass( prof );
586 }
590 static std::vector<ProfileInfo> knownProfiles;
592 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
593 {
594 std::vector<Glib::ustring> result;
596 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
597 if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
598 result.push_back( it->getName() );
599 }
600 }
602 return result;
603 }
605 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
606 {
607 std::vector<Glib::ustring> result;
609 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
610 if ( it->getClass() == icSigOutputClass ) {
611 result.push_back( it->getName() );
612 }
613 }
615 return result;
616 }
618 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
619 {
620 Glib::ustring result;
622 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
623 if ( name == it->getName() ) {
624 result = it->getPath();
625 break;
626 }
627 }
629 return result;
630 }
631 #endif // ENABLE_LCMS
633 std::list<Glib::ustring> ColorProfile::getProfileDirs() {
634 std::list<Glib::ustring> sources;
636 gchar* base = profile_path("XXX");
637 {
638 gchar* base2 = g_path_get_dirname(base);
639 g_free(base);
640 base = base2;
641 base2 = g_path_get_dirname(base);
642 g_free(base);
643 base = base2;
644 }
646 // first try user's local dir
647 sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
650 const gchar* const * dataDirs = g_get_system_data_dirs();
651 for ( int i = 0; dataDirs[i]; i++ ) {
652 gchar* path = g_build_filename(dataDirs[i], "color", "icc", NULL);
653 sources.push_back(path);
654 g_free(path);
655 }
657 // On OS X:
658 if ( g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_EXISTS) && g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_IS_DIR) ) {
659 sources.push_back("/Library/ColorSync/Profiles");
661 gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL);
662 if ( g_file_test(path, G_FILE_TEST_EXISTS) && g_file_test(path, G_FILE_TEST_IS_DIR) ) {
663 sources.push_back(path);
664 }
665 g_free(path);
666 }
669 #ifdef WIN32
670 wchar_t pathBuf[MAX_PATH + 1];
671 pathBuf[0] = 0;
672 DWORD pathSize = sizeof(pathBuf);
673 g_assert(sizeof(wchar_t) == sizeof(gunichar2));
674 if ( GetColorDirectoryW( NULL, pathBuf, &pathSize ) ) {
675 gchar * utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
676 if ( !g_utf8_validate(utf8Path, -1, NULL) ) {
677 g_warning( "GetColorDirectoryW() resulted in invalid UTF-8" );
678 } else {
679 sources.push_back(utf8Path);
680 }
681 g_free( utf8Path );
682 }
683 #endif // WIN32
685 return sources;
686 }
688 #if ENABLE_LCMS
689 static void findThings() {
690 std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
692 for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
693 if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
694 GError *err = 0;
695 GDir *dir = g_dir_open(it->c_str(), 0, &err);
697 if (dir) {
698 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
699 gchar *filepath = g_build_filename(it->c_str(), file, NULL);
702 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
703 sources.push_back(g_strdup(filepath));
704 } else {
705 bool isIccFile = false;
706 struct stat st;
707 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
708 //0-3 == size
709 //36-39 == 'acsp' 0x61637370
710 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
711 if ( fd != -1 ) {
712 guchar scratch[40] = {0};
713 size_t len = sizeof(scratch);
715 //size_t left = 40;
716 ssize_t got = read(fd, scratch, len);
717 if ( got != -1 ) {
718 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
719 if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
720 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
721 }
722 }
724 close(fd);
725 }
726 }
728 if ( isIccFile ) {
729 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
730 if ( prof ) {
731 ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
732 cmsCloseProfile( prof );
734 bool sameName = false;
735 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
736 if ( it->getName() == info.getName() ) {
737 sameName = true;
738 break;
739 }
740 }
742 if ( !sameName ) {
743 knownProfiles.push_back(info);
744 }
745 }
746 }
747 }
749 g_free(filepath);
750 }
751 }
752 }
753 }
754 }
756 int errorHandlerCB(int ErrorCode, const char *ErrorText)
757 {
758 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
760 return 1;
761 }
763 static bool gamutWarn = false;
764 static Gdk::Color lastGamutColor("#808080");
765 static bool lastBPC = false;
766 #if defined(cmsFLAGS_PRESERVEBLACK)
767 static bool lastPreserveBlack = false;
768 #endif // defined(cmsFLAGS_PRESERVEBLACK)
769 static int lastIntent = INTENT_PERCEPTUAL;
770 static int lastProofIntent = INTENT_PERCEPTUAL;
771 static cmsHTRANSFORM transf = 0;
773 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
774 {
775 static cmsHPROFILE theOne = 0;
776 static Glib::ustring lastURI;
778 static bool init = false;
779 if ( !init ) {
780 cmsSetErrorHandler(errorHandlerCB);
782 findThings();
783 init = true;
784 }
786 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
787 Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
789 if ( !uri.empty() ) {
790 if ( uri != lastURI ) {
791 lastURI.clear();
792 if ( theOne ) {
793 cmsCloseProfile( theOne );
794 }
795 if ( transf ) {
796 cmsDeleteTransform( transf );
797 transf = 0;
798 }
799 theOne = cmsOpenProfileFromFile( uri.data(), "r" );
800 if ( theOne ) {
801 // a display profile must have the proper stuff
802 icColorSpaceSignature space = cmsGetColorSpace(theOne);
803 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
805 if ( profClass != icSigDisplayClass ) {
806 g_warning("Not a display profile");
807 cmsCloseProfile( theOne );
808 theOne = 0;
809 } else if ( space != icSigRgbData ) {
810 g_warning("Not an RGB profile");
811 cmsCloseProfile( theOne );
812 theOne = 0;
813 } else {
814 lastURI = uri;
815 }
816 }
817 }
818 } else if ( theOne ) {
819 cmsCloseProfile( theOne );
820 theOne = 0;
821 lastURI.clear();
822 if ( transf ) {
823 cmsDeleteTransform( transf );
824 transf = 0;
825 }
826 }
828 return theOne;
829 }
832 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
833 {
834 static cmsHPROFILE theOne = 0;
835 static Glib::ustring lastURI;
837 static bool init = false;
838 if ( !init ) {
839 cmsSetErrorHandler(errorHandlerCB);
841 findThings();
842 init = true;
843 }
845 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
846 bool which = prefs->getBool( "/options/softproof/enable");
847 Glib::ustring uri = prefs->getString("/options/softproof/uri");
849 if ( which && !uri.empty() ) {
850 if ( lastURI != uri ) {
851 lastURI.clear();
852 if ( theOne ) {
853 cmsCloseProfile( theOne );
854 }
855 if ( transf ) {
856 cmsDeleteTransform( transf );
857 transf = 0;
858 }
859 theOne = cmsOpenProfileFromFile( uri.data(), "r" );
860 if ( theOne ) {
861 // a display profile must have the proper stuff
862 icColorSpaceSignature space = cmsGetColorSpace(theOne);
863 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
865 (void)space;
866 (void)profClass;
867 /*
868 if ( profClass != icSigDisplayClass ) {
869 g_warning("Not a display profile");
870 cmsCloseProfile( theOne );
871 theOne = 0;
872 } else if ( space != icSigRgbData ) {
873 g_warning("Not an RGB profile");
874 cmsCloseProfile( theOne );
875 theOne = 0;
876 } else {
877 */
878 lastURI = uri;
879 /*
880 }
881 */
882 }
883 }
884 } else if ( theOne ) {
885 cmsCloseProfile( theOne );
886 theOne = 0;
887 lastURI.clear();
888 if ( transf ) {
889 cmsDeleteTransform( transf );
890 transf = 0;
891 }
892 }
894 return theOne;
895 }
897 static void free_transforms();
899 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
900 {
901 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
902 bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
903 if ( fromDisplay ) {
904 if ( transf ) {
905 cmsDeleteTransform(transf);
906 transf = 0;
907 }
908 return 0;
909 }
911 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
912 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
913 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
914 bool bpc = prefs->getBool( "/options/softproof/bpc");
915 #if defined(cmsFLAGS_PRESERVEBLACK)
916 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
917 #endif //defined(cmsFLAGS_PRESERVEBLACK)
918 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
919 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
921 if ( (warn != gamutWarn)
922 || (lastIntent != intent)
923 || (lastProofIntent != proofIntent)
924 || (bpc != lastBPC)
925 #if defined(cmsFLAGS_PRESERVEBLACK)
926 || (preserveBlack != lastPreserveBlack)
927 #endif // defined(cmsFLAGS_PRESERVEBLACK)
928 || (gamutColor != lastGamutColor)
929 ) {
930 gamutWarn = warn;
931 free_transforms();
932 lastIntent = intent;
933 lastProofIntent = proofIntent;
934 lastBPC = bpc;
935 #if defined(cmsFLAGS_PRESERVEBLACK)
936 lastPreserveBlack = preserveBlack;
937 #endif // defined(cmsFLAGS_PRESERVEBLACK)
938 lastGamutColor = gamutColor;
939 }
941 // Fetch these now, as they might clear the transform as a side effect.
942 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
943 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
945 if ( !transf ) {
946 if ( hprof && proofProf ) {
947 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
948 if ( gamutWarn ) {
949 dwFlags |= cmsFLAGS_GAMUTCHECK;
950 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
951 }
952 if ( bpc ) {
953 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
954 }
955 #if defined(cmsFLAGS_PRESERVEBLACK)
956 if ( preserveBlack ) {
957 dwFlags |= cmsFLAGS_PRESERVEBLACK;
958 }
959 #endif // defined(cmsFLAGS_PRESERVEBLACK)
960 transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
961 } else if ( hprof ) {
962 transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, intent, 0 );
963 }
964 }
966 return transf;
967 }
970 class MemProfile {
971 public:
972 MemProfile();
973 ~MemProfile();
975 std::string id;
976 cmsHPROFILE hprof;
977 cmsHTRANSFORM transf;
978 };
980 MemProfile::MemProfile() :
981 id(),
982 hprof(0),
983 transf(0)
984 {
985 }
987 MemProfile::~MemProfile()
988 {
989 }
991 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
993 void free_transforms()
994 {
995 if ( transf ) {
996 cmsDeleteTransform(transf);
997 transf = 0;
998 }
1000 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
1001 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
1002 if ( it2->transf ) {
1003 cmsDeleteTransform(it2->transf);
1004 it2->transf = 0;
1005 }
1006 }
1007 }
1008 }
1010 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
1011 {
1012 Glib::ustring id;
1014 if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
1015 std::vector<MemProfile>& row = perMonitorProfiles[screen];
1016 if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
1017 MemProfile& item = row[monitor];
1018 id = item.id;
1019 }
1020 }
1022 return id;
1023 }
1025 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
1026 {
1027 Glib::ustring id;
1029 while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
1030 std::vector<MemProfile> tmp;
1031 perMonitorProfiles.push_back(tmp);
1032 }
1033 std::vector<MemProfile>& row = perMonitorProfiles[screen];
1034 while ( static_cast<int>(row.size()) <= monitor ) {
1035 MemProfile tmp;
1036 row.push_back(tmp);
1037 }
1038 MemProfile& item = row[monitor];
1040 if ( item.hprof ) {
1041 cmsCloseProfile( item.hprof );
1042 item.hprof = 0;
1043 }
1044 id.clear();
1046 if ( buf && bufLen ) {
1047 id = Digest::hashHex(Digest::HASH_MD5,
1048 reinterpret_cast<unsigned char*>(buf), bufLen);
1050 // Note: if this is not a valid profile, item.hprof will be set to null.
1051 item.hprof = cmsOpenProfileFromMem(buf, bufLen);
1052 }
1053 item.id = id;
1055 return id;
1056 }
1058 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
1059 {
1060 cmsHTRANSFORM result = 0;
1061 if ( id.empty() ) {
1062 return 0;
1063 }
1065 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1066 bool found = false;
1067 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
1068 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
1069 if ( id == it2->id ) {
1070 MemProfile& item = *it2;
1072 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
1073 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
1074 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
1075 bool bpc = prefs->getBool( "/options/softproof/bpc");
1076 #if defined(cmsFLAGS_PRESERVEBLACK)
1077 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
1078 #endif //defined(cmsFLAGS_PRESERVEBLACK)
1079 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
1080 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
1082 if ( (warn != gamutWarn)
1083 || (lastIntent != intent)
1084 || (lastProofIntent != proofIntent)
1085 || (bpc != lastBPC)
1086 #if defined(cmsFLAGS_PRESERVEBLACK)
1087 || (preserveBlack != lastPreserveBlack)
1088 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1089 || (gamutColor != lastGamutColor)
1090 ) {
1091 gamutWarn = warn;
1092 free_transforms();
1093 lastIntent = intent;
1094 lastProofIntent = proofIntent;
1095 lastBPC = bpc;
1096 #if defined(cmsFLAGS_PRESERVEBLACK)
1097 lastPreserveBlack = preserveBlack;
1098 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1099 lastGamutColor = gamutColor;
1100 }
1102 // Fetch these now, as they might clear the transform as a side effect.
1103 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1105 if ( !item.transf ) {
1106 if ( item.hprof && proofProf ) {
1107 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1108 if ( gamutWarn ) {
1109 dwFlags |= cmsFLAGS_GAMUTCHECK;
1110 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1111 }
1112 if ( bpc ) {
1113 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1114 }
1115 #if defined(cmsFLAGS_PRESERVEBLACK)
1116 if ( preserveBlack ) {
1117 dwFlags |= cmsFLAGS_PRESERVEBLACK;
1118 }
1119 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1120 item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
1121 } else if ( item.hprof ) {
1122 item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, intent, 0 );
1123 }
1124 }
1126 result = item.transf;
1127 found = true;
1128 }
1129 }
1130 }
1132 return result;
1133 }
1137 #endif // ENABLE_LCMS
1139 /*
1140 Local Variables:
1141 mode:c++
1142 c-file-style:"stroustrup"
1143 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1144 indent-tabs-mode:nil
1145 fill-column:99
1146 End:
1147 */
1148 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :