Code

Tablet auto-organization and naming.
[inkscape.git] / src / color-profile.cpp
index ecc0bcd4e37520a7eb2f6f9005f3947f540b322f..a8238556c4c3d86ccf79664b0332cab6476622cf 100644 (file)
@@ -1,6 +1,8 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-
-//#define DEBUG_LCMS
+#define noDEBUG_LCMS
 
 #include <glib/gstdio.h>
 #include <sys/fcntl.h>
 
 #include <cstring>
 #include <string>
+
+#ifdef WIN32
+#ifndef _WIN32_WINDOWS         // Allow use of features specific to Windows 98 or later. Required for correctly including icm.h
+#define _WIN32_WINDOWS 0x0410
+#endif
+#include <windows.h>
+#endif
+
 #include "xml/repr.h"
+#include "color.h"
 #include "color-profile.h"
 #include "color-profile-fns.h"
 #include "attributes.h"
 #include "inkscape.h"
 #include "document.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 
 #include "dom/uri.h"
 #include "dom/util/digest.h"
 
+#ifdef WIN32
+#include <icm.h>
+#endif // WIN32
+
 using Inkscape::ColorProfile;
 using Inkscape::ColorProfileClass;
 
@@ -36,11 +51,12 @@ static cmsHPROFILE colorprofile_get_proof_profile_handle();
 
 #ifdef DEBUG_LCMS
 extern guint update_in_progress;
-#define DEBUG_MESSAGE(key, ...) \
+#define DEBUG_MESSAGE_SCISLAC(key, ...) \
 {\
-    gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
-    gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
-    gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
+    bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
+    bool dumpD = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D");\
+    bool dumpD2 = prefs->getBool(Glib::ustring("/options/scislac/") + #key"D2");\
     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
     if ( dump )\
     {\
@@ -61,6 +77,13 @@ extern guint update_in_progress;
         gtk_widget_show_all( dialog );\
     }\
 }
+
+
+#define DEBUG_MESSAGE(key, ...)\
+{\
+    g_message( __VA_ARGS__ );\
+}
+
 #endif // DEBUG_LCMS
 
 static SPObjectClass *cprof_parent_class;
@@ -76,6 +99,15 @@ cmsHPROFILE ColorProfile::getSRGBProfile() {
     return _sRGBProf;
 }
 
+cmsHPROFILE ColorProfile::_NullProf = 0;
+
+cmsHPROFILE ColorProfile::getNULLProfile() {
+    if ( !_NullProf ) {
+        _NullProf = cmsCreateNULLProfile();
+    }
+    return _NullProf;
+}
+
 #endif // ENABLE_LCMS
 
 /**
@@ -136,6 +168,7 @@ void ColorProfile::init( ColorProfile *cprof )
     cprof->_profileSpace = icSigRgbData;
     cprof->_transf = 0;
     cprof->_revTransf = 0;
+    cprof->_gamutTransf = 0;
 #endif // ENABLE_LCMS
 }
 
@@ -189,6 +222,10 @@ void ColorProfile::_clearProfile()
         cmsDeleteTransform( _revTransf );
         _revTransf = 0;
     }
+    if ( _gamutTransf ) {
+        cmsDeleteTransform( _gamutTransf );
+        _gamutTransf = 0;
+    }
     if ( profHandle ) {
         cmsCloseProfile( profHandle );
         profHandle = 0;
@@ -253,18 +290,21 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
                     //# 1.  Get complete URI of document
                     gchar const *docbase = SP_DOCUMENT_URI( doc );
                     if (!docbase)
-                        {
-                        g_warning("null docbase");
+                    {
+                        // Normal for files that have not yet been saved.
                         docbase = "";
-                        }
+                    }
+
+                    gchar* escaped = g_uri_escape_string(cprof->href, "!*'();:@=+$,/?#[]", TRUE);
+
                     //g_message("docbase:%s\n", docbase);
                     org::w3c::dom::URI docUri(docbase);
                     //# 2. Get href of icc file.  we don't care if it's rel or abs
-                    org::w3c::dom::URI hrefUri(cprof->href);
+                    org::w3c::dom::URI hrefUri(escaped);
                     //# 3.  Resolve the href according the docBase.  This follows
                     //      the w3c specs.  All absolute and relative issues are considered
                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
-                    gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
+                    gchar* fullname = g_uri_unescape_string(cprofUri.getNativePath().c_str(), "");
                     cprof->_clearProfile();
                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
                     if ( cprof->profHandle ) {
@@ -274,7 +314,9 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
 #ifdef DEBUG_LCMS
                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
 #endif // DEBUG_LCMS
-
+                    g_free(escaped);
+                    escaped = 0;
+                    g_free(fullname);
 #endif // ENABLE_LCMS
                 }
             }
@@ -342,12 +384,11 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
 /**
  * Callback: write attributes to associated repr.
  */
-Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
+Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags )
 {
     ColorProfile *cprof = COLORPROFILE(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:color-profile");
     }
 
@@ -368,7 +409,7 @@ Inkscape::XML::Node* ColorProfile::write( SPObject *object, Inkscape::XML::Node
     }
 
     if (cprof_parent_class->write) {
-        (* cprof_parent_class->write)(object, repr, flags);
+        (* cprof_parent_class->write)(object, xml_doc, repr, flags);
     }
 
     return repr;
@@ -473,7 +514,7 @@ cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* inte
 
 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
 {
-    if ( !_transf ) {
+    if ( !_transf && profHandle ) {
         int intent = getLcmsIntent(rendering_intent);
         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
     }
@@ -482,13 +523,39 @@ cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
 
 cmsHTRANSFORM ColorProfile::getTransfFromSRGB8()
 {
-    if ( !_revTransf ) {
+    if ( !_revTransf && profHandle ) {
         int intent = getLcmsIntent(rendering_intent);
         _revTransf = cmsCreateTransform( getSRGBProfile(), TYPE_RGBA_8, profHandle, _getInputFormat(_profileSpace), intent, 0 );
     }
     return _revTransf;
 }
 
+cmsHTRANSFORM ColorProfile::getTransfGamutCheck()
+{
+    if ( !_gamutTransf ) {
+        _gamutTransf = cmsCreateProofingTransform(getSRGBProfile(), TYPE_RGBA_8, getNULLProfile(), TYPE_GRAY_8, profHandle, INTENT_RELATIVE_COLORIMETRIC, INTENT_RELATIVE_COLORIMETRIC, (cmsFLAGS_GAMUTCHECK|cmsFLAGS_SOFTPROOFING));
+    }
+    return _gamutTransf;
+}
+
+bool ColorProfile::GamutCheck(SPColor color){
+    BYTE outofgamut = 0;
+    
+    guint32 val = color.toRGBA32(0);
+    guchar check_color[4] = {
+        SP_RGBA32_R_U(val),
+        SP_RGBA32_G_U(val),
+        SP_RGBA32_B_U(val),
+        255};
+
+    int alarm_r, alarm_g, alarm_b;
+    cmsGetAlarmCodes(&alarm_r, &alarm_g, &alarm_b);
+    cmsSetAlarmCodes(255, 255, 255);
+    cmsDoTransform(ColorProfile::getTransfGamutCheck(), &check_color, &outofgamut, 1);
+    cmsSetAlarmCodes(alarm_r, alarm_g, alarm_b);
+    return (outofgamut == 255);
+}
+
 
 #include <io/sys.h>
 
@@ -561,9 +628,10 @@ Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
 
     return result;
 }
+#endif // ENABLE_LCMS
 
-static void findThings() {
-    std::list<gchar *> sources;
+std::list<Glib::ustring> ColorProfile::getProfileDirs() {
+    std::list<Glib::ustring> sources;
 
     gchar* base = profile_path("XXX");
     {
@@ -577,22 +645,58 @@ static void findThings() {
 
     // first try user's local dir
     sources.push_back( g_build_filename(g_get_user_data_dir(), "color", "icc", NULL) );
-    sources.push_back( g_build_filename(base, ".color", "icc", NULL) ); // OpenICC recommends to deprecate this
+
 
     const gchar* const * dataDirs = g_get_system_data_dirs();
     for ( int i = 0; dataDirs[i]; i++ ) {
-        sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
+        gchar* path = g_build_filename(dataDirs[i], "color", "icc", NULL);
+        sources.push_back(path);
+        g_free(path);
     }
 
-    while (!sources.empty()) {
-        gchar *dirname = sources.front();
-        if ( g_file_test( dirname, G_FILE_TEST_EXISTS ) && g_file_test( dirname, G_FILE_TEST_IS_DIR ) ) {
+    // On OS X:
+    if ( g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_EXISTS)  && g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_IS_DIR) ) {
+        sources.push_back("/Library/ColorSync/Profiles");
+
+        gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL);
+        if ( g_file_test(path, G_FILE_TEST_EXISTS)  && g_file_test(path, G_FILE_TEST_IS_DIR) ) {
+            sources.push_back(path);
+        }
+        g_free(path);
+    }
+
+
+#ifdef WIN32
+    wchar_t pathBuf[MAX_PATH + 1];
+    pathBuf[0] = 0;
+    DWORD pathSize = sizeof(pathBuf);
+    g_assert(sizeof(wchar_t) == sizeof(gunichar2));
+    if ( GetColorDirectoryW( NULL, pathBuf, &pathSize ) ) {
+        gchar * utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
+        if ( !g_utf8_validate(utf8Path, -1, NULL) ) {
+            g_warning( "GetColorDirectoryW() resulted in invalid UTF-8" );
+        } else {
+            sources.push_back(utf8Path);
+        }
+        g_free( utf8Path );
+    }
+#endif // WIN32
+
+    return sources;
+}
+
+#if ENABLE_LCMS
+static void findThings() {
+    std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
+
+    for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
+        if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
             GError *err = 0;
-            GDir *dir = g_dir_open(dirname, 0, &err);
+            GDir *dir = g_dir_open(it->c_str(), 0, &err);
 
             if (dir) {
                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
-                    gchar *filepath = g_build_filename(dirname, file, NULL);
+                    gchar *filepath = g_build_filename(it->c_str(), file, NULL);
 
 
                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
@@ -612,7 +716,7 @@ static void findThings() {
                                 ssize_t got = read(fd, scratch, len);
                                 if ( got != -1 ) {
                                     size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
-                                    if ( calcSize > 128 && calcSize <= st.st_size ) {
+                                    if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
                                         isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
                                     }
                                 }
@@ -646,10 +750,6 @@ static void findThings() {
                 }
             }
         }
-
-        // toss the dirname
-        g_free(dirname);
-        sources.pop_front();
     }
 }
 
@@ -673,7 +773,7 @@ static cmsHTRANSFORM transf = 0;
 cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
 {
     static cmsHPROFILE theOne = 0;
-    static std::string lastURI;
+    static Glib::ustring lastURI;
 
     static bool init = false;
     if ( !init ) {
@@ -683,10 +783,11 @@ cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
         init = true;
     }
 
-    gchar const * uri = prefs_get_string_attribute("options.displayprofile", "uri");
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    Glib::ustring uri = prefs->getString("/options/displayprofile/uri");
 
-    if ( uri && *uri ) {
-        if ( lastURI != std::string(uri) ) {
+    if ( !uri.empty() ) {
+        if ( uri != lastURI ) {
             lastURI.clear();
             if ( theOne ) {
                 cmsCloseProfile( theOne );
@@ -695,7 +796,7 @@ cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
                 cmsDeleteTransform( transf );
                 transf = 0;
             }
-            theOne = cmsOpenProfileFromFile( uri, "r" );
+            theOne = cmsOpenProfileFromFile( uri.data(), "r" );
             if ( theOne ) {
                 // a display profile must have the proper stuff
                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
@@ -731,7 +832,7 @@ cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
 cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
 {
     static cmsHPROFILE theOne = 0;
-    static std::string lastURI;
+    static Glib::ustring lastURI;
 
     static bool init = false;
     if ( !init ) {
@@ -741,11 +842,12 @@ cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
         init = true;
     }
 
-    long long int which = prefs_get_int_attribute_limited( "options.softproof", "enable", 0, 0, 1 );
-    gchar const * uri = prefs_get_string_attribute("options.softproof", "uri");
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    bool which = prefs->getBool( "/options/softproof/enable");
+    Glib::ustring uri = prefs->getString("/options/softproof/uri");
 
-    if ( which && uri && *uri ) {
-        if ( lastURI != std::string(uri) ) {
+    if ( which && !uri.empty() ) {
+        if ( lastURI != uri ) {
             lastURI.clear();
             if ( theOne ) {
                 cmsCloseProfile( theOne );
@@ -754,7 +856,7 @@ cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
                 cmsDeleteTransform( transf );
                 transf = 0;
             }
-            theOne = cmsOpenProfileFromFile( uri, "r" );
+            theOne = cmsOpenProfileFromFile( uri.data(), "r" );
             if ( theOne ) {
                 // a display profile must have the proper stuff
                 icColorSpaceSignature space = cmsGetColorSpace(theOne);
@@ -796,7 +898,8 @@ static void free_transforms();
 
 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
 {
-    long long int fromDisplay = prefs_get_int_attribute_limited( "options.displayprofile", "from_display", 0, 0, 1 );
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
     if ( fromDisplay ) {
         if ( transf ) {
             cmsDeleteTransform(transf);
@@ -805,15 +908,15 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
         return 0;
     }
 
-    bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
-    int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
-    int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
-    bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
+    bool warn = prefs->getBool( "/options/softproof/gamutwarn");
+    int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
+    int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
+    bool bpc = prefs->getBool( "/options/softproof/bpc");
 #if defined(cmsFLAGS_PRESERVEBLACK)
-    bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
+    bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
 #endif //defined(cmsFLAGS_PRESERVEBLACK)
-    gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
-    Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
+    Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
+    Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
 
     if ( (warn != gamutWarn)
          || (lastIntent != intent)
@@ -854,9 +957,9 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
                 dwFlags |= cmsFLAGS_PRESERVEBLACK;
             }
 #endif // defined(cmsFLAGS_PRESERVEBLACK)
-            transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
+            transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
         } else if ( hprof ) {
-            transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, hprof, TYPE_RGB_8, intent, 0 );
+            transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, hprof, TYPE_RGBA_8, intent, 0 );
         }
     }
 
@@ -941,11 +1044,8 @@ Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen
     id.clear();
 
     if ( buf && bufLen ) {
-        Md5Digest digest;
-        if ( buf && bufLen ) {
-            digest.append(reinterpret_cast<unsigned char*>(buf), bufLen);
-        }
-        id = digest.finishHex();
+        id = Digest::hashHex(Digest::HASH_MD5,
+                   reinterpret_cast<unsigned char*>(buf), bufLen);
 
         // Note: if this is not a valid profile, item.hprof will be set to null.
         item.hprof = cmsOpenProfileFromMem(buf, bufLen);
@@ -962,21 +1062,22 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
         return 0;
     }
 
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     bool found = false;
     for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end() && !found; ++it ) {
         for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end() && !found; ++it2 ) {
             if ( id == it2->id ) {
                 MemProfile& item = *it2;
 
-                bool warn = prefs_get_int_attribute_limited( "options.softproof", "gamutwarn", 0, 0, 1 );
-                int intent = prefs_get_int_attribute_limited( "options.displayprofile", "intent", 0, 0, 3 );
-                int proofIntent = prefs_get_int_attribute_limited( "options.softproof", "intent", 0, 0, 3 );
-                bool bpc = prefs_get_int_attribute_limited( "options.softproof", "bpc", 0, 0, 1 );
+                bool warn = prefs->getBool( "/options/softproof/gamutwarn");
+                int intent = prefs->getIntLimited( "/options/displayprofile/intent", 0, 0, 3 );
+                int proofIntent = prefs->getIntLimited( "/options/softproof/intent", 0, 0, 3 );
+                bool bpc = prefs->getBool( "/options/softproof/bpc");
 #if defined(cmsFLAGS_PRESERVEBLACK)
-                bool preserveBlack = prefs_get_int_attribute_limited( "options.softproof", "preserveblack", 0, 0, 1 );
+                bool preserveBlack = prefs->getBool( "/options/softproof/preserveblack");
 #endif //defined(cmsFLAGS_PRESERVEBLACK)
-                gchar const* colorStr = prefs_get_string_attribute("options.softproof", "gamutcolor");
-                Gdk::Color gamutColor( (colorStr && colorStr[0]) ? colorStr : "#808080");
+                Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
+                Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
 
                 if ( (warn != gamutWarn)
                      || (lastIntent != intent)
@@ -1016,9 +1117,9 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
                             dwFlags |= cmsFLAGS_PRESERVEBLACK;
                         }
 #endif // defined(cmsFLAGS_PRESERVEBLACK)
-                        item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, proofProf, intent, proofIntent, dwFlags );
+                        item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, proofProf, intent, proofIntent, dwFlags );
                     } else if ( item.hprof ) {
-                        item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGB_8, item.hprof, TYPE_RGB_8, intent, 0 );
+                        item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_RGBA_8, item.hprof, TYPE_RGBA_8, intent, 0 );
                     }
                 }