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>
10 #include <glib/gi18n.h>
12 #ifdef DEBUG_LCMS
13 #include <gtk/gtkmessagedialog.h>
14 #endif // DEBUG_LCMS
16 #include <cstring>
17 #include <string>
18 #include <io/sys.h>
20 #ifdef WIN32
21 #ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later. Required for correctly including icm.h
22 #define _WIN32_WINDOWS 0x0410
23 #endif
24 #include <windows.h>
25 #endif
27 #include "xml/repr.h"
28 #include "color.h"
29 #include "color-profile.h"
30 #include "color-profile-fns.h"
31 #include "attributes.h"
32 #include "inkscape.h"
33 #include "document.h"
34 #include "preferences.h"
36 #include "dom/uri.h"
37 #include "dom/util/digest.h"
39 #ifdef WIN32
40 #include <icm.h>
41 #endif // WIN32
43 using Inkscape::ColorProfile;
44 using Inkscape::ColorProfileClass;
46 namespace Inkscape
47 {
48 #if ENABLE_LCMS
49 static cmsHPROFILE colorprofile_get_system_profile_handle();
50 static cmsHPROFILE colorprofile_get_proof_profile_handle();
51 #endif // ENABLE_LCMS
52 }
54 #ifdef DEBUG_LCMS
55 extern guint update_in_progress;
56 #define DEBUG_MESSAGE_SCISLAC(key, ...) \
57 {\
58 Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
59 bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
60 bool dumpD = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D");\
61 bool dumpD2 = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D2");\
62 dumpD &= ( (update_in_progress == 0) || dumpD2 );\
63 if ( dump )\
64 {\
65 g_message( __VA_ARGS__ );\
66 \
67 }\
68 if ( dumpD )\
69 {\
70 GtkWidget *dialog = gtk_message_dialog_new(NULL,\
71 GTK_DIALOG_DESTROY_WITH_PARENT, \
72 GTK_MESSAGE_INFO, \
73 GTK_BUTTONS_OK, \
74 __VA_ARGS__ \
75 );\
76 g_signal_connect_swapped(dialog, "response",\
77 G_CALLBACK(gtk_widget_destroy), \
78 dialog); \
79 gtk_widget_show_all( dialog );\
80 }\
81 }
84 #define DEBUG_MESSAGE(key, ...)\
85 {\
86 g_message( __VA_ARGS__ );\
87 }
89 #endif // DEBUG_LCMS
91 static SPObjectClass *cprof_parent_class;
93 #if ENABLE_LCMS
95 cmsHPROFILE ColorProfile::_sRGBProf = 0;
97 cmsHPROFILE ColorProfile::getSRGBProfile() {
98 if ( !_sRGBProf ) {
99 _sRGBProf = cmsCreate_sRGBProfile();
100 }
101 return _sRGBProf;
102 }
104 cmsHPROFILE ColorProfile::_NullProf = 0;
106 cmsHPROFILE ColorProfile::getNULLProfile() {
107 if ( !_NullProf ) {
108 _NullProf = cmsCreateNULLProfile();
109 }
110 return _NullProf;
111 }
113 #endif // ENABLE_LCMS
115 /**
116 * Register ColorProfile class and return its type.
117 */
118 GType Inkscape::colorprofile_get_type()
119 {
120 return ColorProfile::getType();
121 }
123 GType ColorProfile::getType()
124 {
125 static GType type = 0;
126 if (!type) {
127 GTypeInfo info = {
128 sizeof(ColorProfileClass),
129 NULL, NULL,
130 (GClassInitFunc) ColorProfile::classInit,
131 NULL, NULL,
132 sizeof(ColorProfile),
133 16,
134 (GInstanceInitFunc) ColorProfile::init,
135 NULL, /* value_table */
136 };
137 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
138 }
139 return type;
140 }
142 /**
143 * ColorProfile vtable initialization.
144 */
145 void ColorProfile::classInit( ColorProfileClass *klass )
146 {
147 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
149 cprof_parent_class = static_cast<SPObjectClass*>(g_type_class_ref(SP_TYPE_OBJECT));
151 sp_object_class->release = ColorProfile::release;
152 sp_object_class->build = ColorProfile::build;
153 sp_object_class->set = ColorProfile::set;
154 sp_object_class->write = ColorProfile::write;
155 }
157 /**
158 * Callback for ColorProfile object initialization.
159 */
160 void ColorProfile::init( ColorProfile *cprof )
161 {
162 cprof->href = 0;
163 cprof->local = 0;
164 cprof->name = 0;
165 cprof->intentStr = 0;
166 cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
167 #if ENABLE_LCMS
168 cprof->profHandle = 0;
169 cprof->_profileClass = icSigInputClass;
170 cprof->_profileSpace = icSigRgbData;
171 cprof->_transf = 0;
172 cprof->_revTransf = 0;
173 cprof->_gamutTransf = 0;
174 #endif // ENABLE_LCMS
175 }
177 /**
178 * Callback: free object
179 */
180 void ColorProfile::release( SPObject *object )
181 {
182 // Unregister ourselves
183 if ( object->document ) {
184 object->document->removeResource("iccprofile", object);
185 }
187 ColorProfile *cprof = COLORPROFILE(object);
188 if ( cprof->href ) {
189 g_free( cprof->href );
190 cprof->href = 0;
191 }
193 if ( cprof->local ) {
194 g_free( cprof->local );
195 cprof->local = 0;
196 }
198 if ( cprof->name ) {
199 g_free( cprof->name );
200 cprof->name = 0;
201 }
203 if ( cprof->intentStr ) {
204 g_free( cprof->intentStr );
205 cprof->intentStr = 0;
206 }
208 #if ENABLE_LCMS
209 cprof->_clearProfile();
210 #endif // ENABLE_LCMS
211 }
213 #if ENABLE_LCMS
214 void ColorProfile::_clearProfile()
215 {
216 _profileSpace = icSigRgbData;
218 if ( _transf ) {
219 cmsDeleteTransform( _transf );
220 _transf = 0;
221 }
222 if ( _revTransf ) {
223 cmsDeleteTransform( _revTransf );
224 _revTransf = 0;
225 }
226 if ( _gamutTransf ) {
227 cmsDeleteTransform( _gamutTransf );
228 _gamutTransf = 0;
229 }
230 if ( profHandle ) {
231 cmsCloseProfile( profHandle );
232 profHandle = 0;
233 }
234 }
235 #endif // ENABLE_LCMS
237 /**
238 * Callback: set attributes from associated repr.
239 */
240 void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
241 {
242 ColorProfile *cprof = COLORPROFILE(object);
243 g_assert(cprof->href == 0);
244 g_assert(cprof->local == 0);
245 g_assert(cprof->name == 0);
246 g_assert(cprof->intentStr == 0);
248 if (cprof_parent_class->build) {
249 (* cprof_parent_class->build)(object, document, repr);
250 }
251 object->readAttr( "xlink:href" );
252 object->readAttr( "local" );
253 object->readAttr( "name" );
254 object->readAttr( "rendering-intent" );
256 // Register
257 if ( document ) {
258 document->addResource( "iccprofile", object );
259 }
260 }
262 /**
263 * Callback: set attribute.
264 */
265 void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
266 {
267 ColorProfile *cprof = COLORPROFILE(object);
269 switch (key) {
270 case SP_ATTR_XLINK_HREF:
271 if ( cprof->href ) {
272 g_free( cprof->href );
273 cprof->href = 0;
274 }
275 if ( value ) {
276 cprof->href = g_strdup( value );
277 if ( *cprof->href ) {
278 #if ENABLE_LCMS
279 cmsErrorAction( LCMS_ERROR_SHOW );
281 // TODO open filename and URIs properly
282 //FILE* fp = fopen_utf8name( filename, "r" );
283 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
285 // Try to open relative
286 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
287 if (!doc) {
288 doc = SP_ACTIVE_DOCUMENT;
289 g_warning("object has no document. using active");
290 }
291 //# 1. Get complete URI of document
292 gchar const *docbase = doc->getURI();
293 if (!docbase)
294 {
295 // Normal for files that have not yet been saved.
296 docbase = "";
297 }
299 gchar* escaped = g_uri_escape_string(cprof->href, "!*'();:@=+$,/?#[]", TRUE);
301 //g_message("docbase:%s\n", docbase);
302 org::w3c::dom::URI docUri(docbase);
303 //# 2. Get href of icc file. we don't care if it's rel or abs
304 org::w3c::dom::URI hrefUri(escaped);
305 //# 3. Resolve the href according the docBase. This follows
306 // the w3c specs. All absolute and relative issues are considered
307 org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
308 gchar* fullname = g_uri_unescape_string(cprofUri.getNativePath().c_str(), "");
309 cprof->_clearProfile();
310 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
311 if ( cprof->profHandle ) {
312 cprof->_profileSpace = cmsGetColorSpace( cprof->profHandle );
313 cprof->_profileClass = cmsGetDeviceClass( cprof->profHandle );
314 }
315 #ifdef DEBUG_LCMS
316 DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
317 #endif // DEBUG_LCMS
318 g_free(escaped);
319 escaped = 0;
320 g_free(fullname);
321 #endif // ENABLE_LCMS
322 }
323 }
324 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
325 break;
327 case SP_ATTR_LOCAL:
328 if ( cprof->local ) {
329 g_free( cprof->local );
330 cprof->local = 0;
331 }
332 cprof->local = g_strdup( value );
333 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
334 break;
336 case SP_ATTR_NAME:
337 if ( cprof->name ) {
338 g_free( cprof->name );
339 cprof->name = 0;
340 }
341 cprof->name = g_strdup( value );
342 #ifdef DEBUG_LCMS
343 DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
344 #endif // DEBUG_LCMS
345 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
346 break;
348 case SP_ATTR_RENDERING_INTENT:
349 if ( cprof->intentStr ) {
350 g_free( cprof->intentStr );
351 cprof->intentStr = 0;
352 }
353 cprof->intentStr = g_strdup( value );
355 if ( value ) {
356 if ( strcmp( value, "auto" ) == 0 ) {
357 cprof->rendering_intent = RENDERING_INTENT_AUTO;
358 } else if ( strcmp( value, "perceptual" ) == 0 ) {
359 cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
360 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
361 cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
362 } else if ( strcmp( value, "saturation" ) == 0 ) {
363 cprof->rendering_intent = RENDERING_INTENT_SATURATION;
364 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
365 cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
366 } else {
367 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
368 }
369 } else {
370 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
371 }
373 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
374 break;
376 default:
377 if (cprof_parent_class->set) {
378 (* cprof_parent_class->set)(object, key, value);
379 }
380 break;
381 }
383 }
385 /**
386 * Callback: write attributes to associated repr.
387 */
388 Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
389 {
390 ColorProfile *cprof = COLORPROFILE(object);
392 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
393 repr = xml_doc->createElement("svg:color-profile");
394 }
396 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
397 repr->setAttribute( "xlink:href", cprof->href );
398 }
400 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
401 repr->setAttribute( "local", cprof->local );
402 }
404 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
405 repr->setAttribute( "name", cprof->name );
406 }
408 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
409 repr->setAttribute( "rendering-intent", cprof->intentStr );
410 }
412 if (cprof_parent_class->write) {
413 (* cprof_parent_class->write)(object, xml_doc, repr, flags);
414 }
416 return repr;
417 }
420 #if ENABLE_LCMS
422 struct MapMap {
423 icColorSpaceSignature space;
424 DWORD inForm;
425 };
427 DWORD ColorProfile::_getInputFormat( icColorSpaceSignature space )
428 {
429 MapMap possible[] = {
430 {icSigXYZData, TYPE_XYZ_16},
431 {icSigLabData, TYPE_Lab_16},
432 //icSigLuvData
433 {icSigYCbCrData, TYPE_YCbCr_16},
434 {icSigYxyData, TYPE_Yxy_16},
435 {icSigRgbData, TYPE_RGB_16},
436 {icSigGrayData, TYPE_GRAY_16},
437 {icSigHsvData, TYPE_HSV_16},
438 {icSigHlsData, TYPE_HLS_16},
439 {icSigCmykData, TYPE_CMYK_16},
440 {icSigCmyData, TYPE_CMY_16},
441 };
443 int index = 0;
444 for ( guint i = 0; i < G_N_ELEMENTS(possible); i++ ) {
445 if ( possible[i].space == space ) {
446 index = i;
447 break;
448 }
449 }
451 return possible[index].inForm;
452 }
454 static int getLcmsIntent( guint svgIntent )
455 {
456 int intent = INTENT_PERCEPTUAL;
457 switch ( svgIntent ) {
458 case Inkscape::RENDERING_INTENT_RELATIVE_COLORIMETRIC:
459 intent = INTENT_RELATIVE_COLORIMETRIC;
460 break;
461 case Inkscape::RENDERING_INTENT_SATURATION:
462 intent = INTENT_SATURATION;
463 break;
464 case Inkscape::RENDERING_INTENT_ABSOLUTE_COLORIMETRIC:
465 intent = INTENT_ABSOLUTE_COLORIMETRIC;
466 break;
467 case Inkscape::RENDERING_INTENT_PERCEPTUAL:
468 case Inkscape::RENDERING_INTENT_UNKNOWN:
469 case Inkscape::RENDERING_INTENT_AUTO:
470 default:
471 intent = INTENT_PERCEPTUAL;
472 }
473 return intent;
474 }
476 static SPObject* bruteFind( SPDocument* document, gchar const* name )
477 {
478 SPObject* result = 0;
479 const GSList * current = document->getResourceList("iccprofile");
480 while ( current && !result ) {
481 if ( IS_COLORPROFILE(current->data) ) {
482 ColorProfile* prof = COLORPROFILE(current->data);
483 if ( prof ) {
484 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
485 result = SP_OBJECT(current->data);
486 break;
487 }
488 }
489 }
490 current = g_slist_next(current);
491 }
493 return result;
494 }
496 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
497 {
498 cmsHPROFILE prof = 0;
500 SPObject* thing = bruteFind( document, name );
501 if ( thing ) {
502 prof = COLORPROFILE(thing)->profHandle;
503 }
505 if ( intent ) {
506 *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
507 }
509 #ifdef DEBUG_LCMS
510 DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
511 #endif // DEBUG_LCMS
513 return prof;
514 }
516 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
517 {
518 if ( !_transf && profHandle ) {
519 int intent = getLcmsIntent(rendering_intent);
520 _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
521 }
522 return _transf;
523 }
525 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
526 {
527 if ( !_revTransf && profHandle ) {
528 int intent = getLcmsIntent(rendering_intent);
529 _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
530 }
531 return _revTransf;
532 }
534 cmsHTRANSFORM ColorProfile::getTransfGamutCheck()
535 {
536 if ( !_gamutTransf ) {
537 _gamutTransf = cmsCreateProofingTransform(getSRGBProfile(), TYPE_RGBA_8, getNULLProfile(), TYPE_GRAY_8, profHandle, INTENT_RELATIVE_COLORIMETRIC, INTENT_RELATIVE_COLORIMETRIC, (cmsFLAGS_GAMUTCHECK|cmsFLAGS_SOFTPROOFING));
538 }
539 return _gamutTransf;
540 }
542 bool ColorProfile::GamutCheck(SPColor color){
543 BYTE outofgamut = 0;
545 guint32 val = color.toRGBA32(0);
546 guchar check_color[4] = {
547 SP_RGBA32_R_U(val),
548 SP_RGBA32_G_U(val),
549 SP_RGBA32_B_U(val),
550 255};
552 int alarm_r, alarm_g, alarm_b;
553 cmsGetAlarmCodes(&alarm_r, &alarm_g, &alarm_b);
554 cmsSetAlarmCodes(255, 255, 255);
555 cmsDoTransform(ColorProfile::getTransfGamutCheck(), &check_color, &outofgamut, 1);
556 cmsSetAlarmCodes(alarm_r, alarm_g, alarm_b);
557 return (outofgamut == 255);
558 }
560 class ProfileInfo
561 {
562 public:
563 ProfileInfo( cmsHPROFILE, Glib::ustring const & path );
565 Glib::ustring const& getName() {return _name;}
566 Glib::ustring const& getPath() {return _path;}
567 icColorSpaceSignature getSpace() {return _profileSpace;}
568 icProfileClassSignature getClass() {return _profileClass;}
570 private:
571 Glib::ustring _path;
572 Glib::ustring _name;
573 icColorSpaceSignature _profileSpace;
574 icProfileClassSignature _profileClass;
575 };
578 ProfileInfo::ProfileInfo( cmsHPROFILE prof, Glib::ustring const & path )
579 {
580 _path = path;
581 _name = cmsTakeProductDesc(prof);
582 _profileSpace = cmsGetColorSpace( prof );
583 _profileClass = cmsGetDeviceClass( prof );
584 }
588 static std::vector<ProfileInfo> knownProfiles;
590 std::vector<Glib::ustring> Inkscape::colorprofile_get_display_names()
591 {
592 std::vector<Glib::ustring> result;
594 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
595 if ( it->getClass() == icSigDisplayClass && it->getSpace() == icSigRgbData ) {
596 result.push_back( it->getName() );
597 }
598 }
600 return result;
601 }
603 std::vector<Glib::ustring> Inkscape::colorprofile_get_softproof_names()
604 {
605 std::vector<Glib::ustring> result;
607 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
608 if ( it->getClass() == icSigOutputClass ) {
609 result.push_back( it->getName() );
610 }
611 }
613 return result;
614 }
616 Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
617 {
618 Glib::ustring result;
620 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
621 if ( name == it->getName() ) {
622 result = it->getPath();
623 break;
624 }
625 }
627 return result;
628 }
629 #endif // ENABLE_LCMS
631 std::list<Glib::ustring> ColorProfile::getBaseProfileDirs() {
632 #if ENABLE_LCMS
633 static bool warnSet = false;
634 if (!warnSet) {
635 cmsErrorAction( LCMS_ERROR_SHOW );
636 warnSet = true;
637 }
638 #endif // ENABLE_LCMS
639 std::list<Glib::ustring> sources;
641 gchar* base = profile_path("XXX");
642 {
643 gchar* base2 = g_path_get_dirname(base);
644 g_free(base);
645 base = base2;
646 base2 = g_path_get_dirname(base);
647 g_free(base);
648 base = base2;
649 }
651 // first try user's local dir
652 sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
655 const gchar* const * dataDirs = g_get_system_data_dirs();
656 for ( int i = 0; dataDirs[i]; i++ ) {
657 gchar* path = g_build_filename(dataDirs[i], "color", "icc", NULL);
658 sources.push_back(path);
659 g_free(path);
660 }
662 // On OS X:
663 {
664 bool onOSX = false;
665 std::list<Glib::ustring> possible;
666 possible.push_back("/System/Library/ColorSync/Profiles");
667 possible.push_back("/Library/ColorSync/Profiles");
668 for ( std::list<Glib::ustring>::const_iterator it = possible.begin(); it != possible.end(); ++it ) {
669 if ( g_file_test(it->c_str(), G_FILE_TEST_EXISTS) && g_file_test(it->c_str(), G_FILE_TEST_IS_DIR) ) {
670 sources.push_back(it->c_str());
671 onOSX = true;
672 }
673 }
674 if ( onOSX ) {
675 gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL);
676 if ( g_file_test(path, G_FILE_TEST_EXISTS) && g_file_test(path, G_FILE_TEST_IS_DIR) ) {
677 sources.push_back(path);
678 }
679 g_free(path);
680 }
681 }
683 #ifdef WIN32
684 wchar_t pathBuf[MAX_PATH + 1];
685 pathBuf[0] = 0;
686 DWORD pathSize = sizeof(pathBuf);
687 g_assert(sizeof(wchar_t) == sizeof(gunichar2));
688 if ( GetColorDirectoryW( NULL, pathBuf, &pathSize ) ) {
689 gchar * utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
690 if ( !g_utf8_validate(utf8Path, -1, NULL) ) {
691 g_warning( "GetColorDirectoryW() resulted in invalid UTF-8" );
692 } else {
693 sources.push_back(utf8Path);
694 }
695 g_free( utf8Path );
696 }
697 #endif // WIN32
699 return sources;
700 }
702 static bool isIccFile( gchar const *filepath )
703 {
704 bool isIccFile = false;
705 struct stat st;
706 if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
707 //0-3 == size
708 //36-39 == 'acsp' 0x61637370
709 int fd = g_open( filepath, O_RDONLY, S_IRWXU);
710 if ( fd != -1 ) {
711 guchar scratch[40] = {0};
712 size_t len = sizeof(scratch);
714 //size_t left = 40;
715 ssize_t got = read(fd, scratch, len);
716 if ( got != -1 ) {
717 size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
718 if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
719 isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
720 }
721 }
723 close(fd);
724 #if ENABLE_LCMS
725 if (isIccFile) {
726 cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
727 if ( prof ) {
728 icProfileClassSignature profClass = cmsGetDeviceClass(prof);
729 if ( profClass == icSigNamedColorClass ) {
730 isIccFile = false; // Ignore named color profiles for now.
731 }
732 cmsCloseProfile( prof );
733 }
734 }
735 #endif // ENABLE_LCMS
736 }
737 }
738 return isIccFile;
739 }
741 std::list<Glib::ustring> ColorProfile::getProfileFiles()
742 {
743 std::list<Glib::ustring> files;
745 std::list<Glib::ustring> sources = ColorProfile::getBaseProfileDirs();
746 for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
747 if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
748 GError *err = 0;
749 GDir *dir = g_dir_open(it->c_str(), 0, &err);
751 if (dir) {
752 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
753 gchar *filepath = g_build_filename(it->c_str(), file, NULL);
754 if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
755 sources.push_back(g_strdup(filepath));
756 } else {
757 if ( isIccFile( filepath ) ) {
758 files.push_back( filepath );
759 }
760 }
762 g_free(filepath);
763 }
764 g_dir_close(dir);
765 dir = 0;
766 } else {
767 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
768 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
769 g_free(safeDir);
770 }
771 }
772 }
774 return files;
775 }
777 #if ENABLE_LCMS
778 static void findThings() {
779 std::list<Glib::ustring> files = ColorProfile::getProfileFiles();
781 for ( std::list<Glib::ustring>::const_iterator it = files.begin(); it != files.end(); ++it ) {
782 cmsHPROFILE prof = cmsOpenProfileFromFile( it->c_str(), "r" );
783 if ( prof ) {
784 ProfileInfo info( prof, Glib::filename_to_utf8( it->c_str() ) );
785 cmsCloseProfile( prof );
787 bool sameName = false;
788 for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
789 if ( it->getName() == info.getName() ) {
790 sameName = true;
791 break;
792 }
793 }
795 if ( !sameName ) {
796 knownProfiles.push_back(info);
797 }
798 }
799 }
800 }
802 int errorHandlerCB(int ErrorCode, const char *ErrorText)
803 {
804 g_message("lcms: Error %d; %s", ErrorCode, ErrorText);
806 return 1;
807 }
809 static bool gamutWarn = false;
810 static Gdk::Color lastGamutColor("#808080");
811 static bool lastBPC = false;
812 #if defined(cmsFLAGS_PRESERVEBLACK)
813 static bool lastPreserveBlack = false;
814 #endif // defined(cmsFLAGS_PRESERVEBLACK)
815 static int lastIntent = INTENT_PERCEPTUAL;
816 static int lastProofIntent = INTENT_PERCEPTUAL;
817 static cmsHTRANSFORM transf = 0;
819 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
820 {
821 static cmsHPROFILE theOne = 0;
822 static Glib::ustring lastURI;
824 static bool init = false;
825 if ( !init ) {
826 cmsSetErrorHandler(errorHandlerCB);
828 findThings();
829 init = true;
830 }
832 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
833 Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
835 if ( !uri.empty() ) {
836 if ( uri != lastURI ) {
837 lastURI.clear();
838 if ( theOne ) {
839 cmsCloseProfile( theOne );
840 }
841 if ( transf ) {
842 cmsDeleteTransform( transf );
843 transf = 0;
844 }
845 theOne = cmsOpenProfileFromFile( uri.data(), "r" );
846 if ( theOne ) {
847 // a display profile must have the proper stuff
848 icColorSpaceSignature space = cmsGetColorSpace(theOne);
849 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
851 if ( profClass != icSigDisplayClass ) {
852 g_warning("Not a display profile");
853 cmsCloseProfile( theOne );
854 theOne = 0;
855 } else if ( space != icSigRgbData ) {
856 g_warning("Not an RGB profile");
857 cmsCloseProfile( theOne );
858 theOne = 0;
859 } else {
860 lastURI = uri;
861 }
862 }
863 }
864 } else if ( theOne ) {
865 cmsCloseProfile( theOne );
866 theOne = 0;
867 lastURI.clear();
868 if ( transf ) {
869 cmsDeleteTransform( transf );
870 transf = 0;
871 }
872 }
874 return theOne;
875 }
878 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
879 {
880 static cmsHPROFILE theOne = 0;
881 static Glib::ustring lastURI;
883 static bool init = false;
884 if ( !init ) {
885 cmsSetErrorHandler(errorHandlerCB);
887 findThings();
888 init = true;
889 }
891 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
892 bool which = prefs->getBool( "/options/softproof/enable");
893 Glib::ustring uri = prefs->getString("/options/softproof/uri");
895 if ( which && !uri.empty() ) {
896 if ( lastURI != uri ) {
897 lastURI.clear();
898 if ( theOne ) {
899 cmsCloseProfile( theOne );
900 }
901 if ( transf ) {
902 cmsDeleteTransform( transf );
903 transf = 0;
904 }
905 theOne = cmsOpenProfileFromFile( uri.data(), "r" );
906 if ( theOne ) {
907 // a display profile must have the proper stuff
908 icColorSpaceSignature space = cmsGetColorSpace(theOne);
909 icProfileClassSignature profClass = cmsGetDeviceClass(theOne);
911 (void)space;
912 (void)profClass;
913 /*
914 if ( profClass != icSigDisplayClass ) {
915 g_warning("Not a display profile");
916 cmsCloseProfile( theOne );
917 theOne = 0;
918 } else if ( space != icSigRgbData ) {
919 g_warning("Not an RGB profile");
920 cmsCloseProfile( theOne );
921 theOne = 0;
922 } else {
923 */
924 lastURI = uri;
925 /*
926 }
927 */
928 }
929 }
930 } else if ( theOne ) {
931 cmsCloseProfile( theOne );
932 theOne = 0;
933 lastURI.clear();
934 if ( transf ) {
935 cmsDeleteTransform( transf );
936 transf = 0;
937 }
938 }
940 return theOne;
941 }
943 static void free_transforms();
945 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
946 {
947 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
948 bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
949 if ( fromDisplay ) {
950 if ( transf ) {
951 cmsDeleteTransform(transf);
952 transf = 0;
953 }
954 return 0;
955 }
957 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
958 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
959 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
960 bool bpc = prefs->getBool( "/options/softproof/bpc");
961 #if defined(cmsFLAGS_PRESERVEBLACK)
962 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
963 #endif //defined(cmsFLAGS_PRESERVEBLACK)
964 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
965 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
967 if ( (warn != gamutWarn)
968 || (lastIntent != intent)
969 || (lastProofIntent != proofIntent)
970 || (bpc != lastBPC)
971 #if defined(cmsFLAGS_PRESERVEBLACK)
972 || (preserveBlack != lastPreserveBlack)
973 #endif // defined(cmsFLAGS_PRESERVEBLACK)
974 || (gamutColor != lastGamutColor)
975 ) {
976 gamutWarn = warn;
977 free_transforms();
978 lastIntent = intent;
979 lastProofIntent = proofIntent;
980 lastBPC = bpc;
981 #if defined(cmsFLAGS_PRESERVEBLACK)
982 lastPreserveBlack = preserveBlack;
983 #endif // defined(cmsFLAGS_PRESERVEBLACK)
984 lastGamutColor = gamutColor;
985 }
987 // Fetch these now, as they might clear the transform as a side effect.
988 cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
989 cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
991 if ( !transf ) {
992 if ( hprof && proofProf ) {
993 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
994 if ( gamutWarn ) {
995 dwFlags |= cmsFLAGS_GAMUTCHECK;
996 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
997 }
998 if ( bpc ) {
999 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1000 }
1001 #if defined(cmsFLAGS_PRESERVEBLACK)
1002 if ( preserveBlack ) {
1003 dwFlags |= cmsFLAGS_PRESERVEBLACK;
1004 }
1005 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1006 transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
1007 } else if ( hprof ) {
1008 transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, intent, 0 );
1009 }
1010 }
1012 return transf;
1013 }
1016 class MemProfile {
1017 public:
1018 MemProfile();
1019 ~MemProfile();
1021 std::string id;
1022 cmsHPROFILE hprof;
1023 cmsHTRANSFORM transf;
1024 };
1026 MemProfile::MemProfile() :
1027 id(),
1028 hprof(0),
1029 transf(0)
1030 {
1031 }
1033 MemProfile::~MemProfile()
1034 {
1035 }
1037 static std::vector< std::vector<MemProfile> > perMonitorProfiles;
1039 void free_transforms()
1040 {
1041 if ( transf ) {
1042 cmsDeleteTransform(transf);
1043 transf = 0;
1044 }
1046 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
1047 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
1048 if ( it2->transf ) {
1049 cmsDeleteTransform(it2->transf);
1050 it2->transf = 0;
1051 }
1052 }
1053 }
1054 }
1056 Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
1057 {
1058 Glib::ustring id;
1060 if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
1061 std::vector<MemProfile>& row = perMonitorProfiles[screen];
1062 if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
1063 MemProfile& item = row[monitor];
1064 id = item.id;
1065 }
1066 }
1068 return id;
1069 }
1071 Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
1072 {
1073 Glib::ustring id;
1075 while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
1076 std::vector<MemProfile> tmp;
1077 perMonitorProfiles.push_back(tmp);
1078 }
1079 std::vector<MemProfile>& row = perMonitorProfiles[screen];
1080 while ( static_cast<int>(row.size()) <= monitor ) {
1081 MemProfile tmp;
1082 row.push_back(tmp);
1083 }
1084 MemProfile& item = row[monitor];
1086 if ( item.hprof ) {
1087 cmsCloseProfile( item.hprof );
1088 item.hprof = 0;
1089 }
1090 id.clear();
1092 if ( buf && bufLen ) {
1093 id = Digest::hashHex(Digest::HASH_MD5,
1094 reinterpret_cast<unsigned char*>(buf), bufLen);
1096 // Note: if this is not a valid profile, item.hprof will be set to null.
1097 item.hprof = cmsOpenProfileFromMem(buf, bufLen);
1098 }
1099 item.id = id;
1101 return id;
1102 }
1104 cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
1105 {
1106 cmsHTRANSFORM result = 0;
1107 if ( id.empty() ) {
1108 return 0;
1109 }
1111 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1112 bool found = false;
1113 for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
1114 for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
1115 if ( id == it2->id ) {
1116 MemProfile& item = *it2;
1118 bool warn = prefs->getBool( "/options/softproof/gamutwarn");
1119 int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
1120 int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
1121 bool bpc = prefs->getBool( "/options/softproof/bpc");
1122 #if defined(cmsFLAGS_PRESERVEBLACK)
1123 bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
1124 #endif //defined(cmsFLAGS_PRESERVEBLACK)
1125 Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
1126 Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
1128 if ( (warn != gamutWarn)
1129 || (lastIntent != intent)
1130 || (lastProofIntent != proofIntent)
1131 || (bpc != lastBPC)
1132 #if defined(cmsFLAGS_PRESERVEBLACK)
1133 || (preserveBlack != lastPreserveBlack)
1134 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1135 || (gamutColor != lastGamutColor)
1136 ) {
1137 gamutWarn = warn;
1138 free_transforms();
1139 lastIntent = intent;
1140 lastProofIntent = proofIntent;
1141 lastBPC = bpc;
1142 #if defined(cmsFLAGS_PRESERVEBLACK)
1143 lastPreserveBlack = preserveBlack;
1144 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1145 lastGamutColor = gamutColor;
1146 }
1148 // Fetch these now, as they might clear the transform as a side effect.
1149 cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
1151 if ( !item.transf ) {
1152 if ( item.hprof && proofProf ) {
1153 DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
1154 if ( gamutWarn ) {
1155 dwFlags |= cmsFLAGS_GAMUTCHECK;
1156 cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
1157 }
1158 if ( bpc ) {
1159 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
1160 }
1161 #if defined(cmsFLAGS_PRESERVEBLACK)
1162 if ( preserveBlack ) {
1163 dwFlags |= cmsFLAGS_PRESERVEBLACK;
1164 }
1165 #endif // defined(cmsFLAGS_PRESERVEBLACK)
1166 item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
1167 } else if ( item.hprof ) {
1168 item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, intent, 0 );
1169 }
1170 }
1172 result = item.transf;
1173 found = true;
1174 }
1175 }
1176 }
1178 return result;
1179 }
1183 #endif // ENABLE_LCMS
1185 /*
1186 Local Variables:
1187 mode:c++
1188 c-file-style:"stroustrup"
1189 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1190 indent-tabs-mode:nil
1191 fill-column:99
1192 End:
1193 */
1194 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :