Code

now that selection description includes style (filtered, clipped), we need to update...
[inkscape.git] / src / color-profile.cpp
index f676ceacf035bb30aac5728e00f8cc761b3a5881..a562d756c7967c5d0abf7fcc111362db25384075 100644 (file)
@@ -1,15 +1,5 @@
 
 
-#include "xml/repr.h"
-#include "color-profile.h"
-#include "color-profile-fns.h"
-#include "attributes.h"
-#include "inkscape.h"
-#include "document.h"
-#include "prefs-utils.h"
-
-#include "dom/uri.h"
-
 //#define DEBUG_LCMS
 
 #include <glib/gstdio.h>
 #include <gtk/gtkmessagedialog.h>
 #endif // DEBUG_LCMS
 
+#include <cstring>
+#include <string>
+#include "xml/repr.h"
+#include "color-profile.h"
+#include "color-profile-fns.h"
+#include "attributes.h"
+#include "inkscape.h"
+#include "document.h"
+#include "preferences.h"
+
+#include "dom/uri.h"
+#include "dom/util/digest.h"
+
 using Inkscape::ColorProfile;
 using Inkscape::ColorProfileClass;
 
@@ -35,9 +38,10 @@ static cmsHPROFILE colorprofile_get_proof_profile_handle();
 extern guint update_in_progress;
 #define DEBUG_MESSAGE(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 )\
     {\
@@ -250,10 +254,10 @@ 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 = "";
-                        }
+                    }
                     //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
@@ -261,7 +265,7 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
                     //# 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_strdup((gchar *)cprofUri.getNativePath().c_str());
                     cprof->_clearProfile();
                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
                     if ( cprof->profHandle ) {
@@ -271,7 +275,7 @@ 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(fullname);
 #endif // ENABLE_LCMS
                 }
             }
@@ -339,12 +343,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");
     }
 
@@ -365,7 +368,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;
@@ -558,9 +561,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");
     {
@@ -581,15 +585,21 @@ static void findThings() {
         sources.push_back(g_build_filename(dataDirs[i], "color", "icc", NULL));
     }
 
-    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 ) ) {
+    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 ) ) {
@@ -609,7 +619,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');
                                     }
                                 }
@@ -643,10 +653,6 @@ static void findThings() {
                 }
             }
         }
-
-        // toss the dirname
-        g_free(dirname);
-        sources.pop_front();
     }
 }
 
@@ -670,7 +676,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 ) {
@@ -680,11 +686,11 @@ cmsHPROFILE Inkscape::colorprofile_get_system_profile_handle()
         init = true;
     }
 
-    long long int which = prefs_get_int_attribute_limited( "options.displayprofile", "enable", 0, 0, 1 );
-    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 ( which && uri && *uri ) {
-        if ( lastURI != std::string(uri) ) {
+    if ( !uri.empty() ) {
+        if ( uri != lastURI ) {
             lastURI.clear();
             if ( theOne ) {
                 cmsCloseProfile( theOne );
@@ -693,7 +699,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);
@@ -729,7 +735,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 ) {
@@ -739,11 +745,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 );
@@ -752,7 +759,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);
@@ -790,17 +797,29 @@ cmsHPROFILE Inkscape::colorprofile_get_proof_profile_handle()
     return theOne;
 }
 
+static void free_transforms();
+
 cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
 {
-    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 );
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
+    if ( fromDisplay ) {
+        if ( transf ) {
+            cmsDeleteTransform(transf);
+            transf = 0;
+        }
+        return 0;
+    }
+
+    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)
@@ -812,10 +831,7 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
          || (gamutColor != lastGamutColor)
         ) {
         gamutWarn = warn;
-        if ( transf ) {
-            cmsDeleteTransform(transf);
-            transf = 0;
-        }
+        free_transforms();
         lastIntent = intent;
         lastProofIntent = proofIntent;
         lastBPC = bpc;
@@ -825,7 +841,7 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_transform()
         lastGamutColor = gamutColor;
     }
 
-    // Fecth these now, as they might clear the transform as a side effect.
+    // Fetch these now, as they might clear the transform as a side effect.
     cmsHPROFILE hprof = Inkscape::colorprofile_get_system_profile_handle();
     cmsHPROFILE proofProf = hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
 
@@ -844,15 +860,183 @@ 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_ARGB_8, hprof, TYPE_ARGB_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_ARGB_8, hprof, TYPE_ARGB_8, intent, 0 );
         }
     }
 
     return transf;
 }
 
+
+class MemProfile {
+public:
+    MemProfile();
+    ~MemProfile();
+
+    std::string id;
+    cmsHPROFILE hprof;
+    cmsHTRANSFORM transf;
+};
+
+MemProfile::MemProfile() :
+    id(),
+    hprof(0),
+    transf(0)
+{
+}
+
+MemProfile::~MemProfile()
+{
+}
+
+static std::vector< std::vector<MemProfile> > perMonitorProfiles;
+
+void free_transforms()
+{
+    if ( transf ) {
+        cmsDeleteTransform(transf);
+        transf = 0;
+    }
+
+    for ( std::vector< std::vector<MemProfile> >::iterator it = perMonitorProfiles.begin(); it != perMonitorProfiles.end(); ++it ) {
+        for ( std::vector<MemProfile>::iterator it2 = it->begin(); it2 != it->end(); ++it2 ) {
+            if ( it2->transf ) {
+                cmsDeleteTransform(it2->transf);
+                it2->transf = 0;
+            }
+        }
+    }
+}
+
+Glib::ustring Inkscape::colorprofile_get_display_id( int screen, int monitor )
+{
+    Glib::ustring id;
+
+    if ( screen >= 0 && screen < static_cast<int>(perMonitorProfiles.size()) ) {
+        std::vector<MemProfile>& row = perMonitorProfiles[screen];
+        if ( monitor >= 0 && monitor < static_cast<int>(row.size()) ) {
+            MemProfile& item = row[monitor];
+            id = item.id;
+        }
+    }
+
+    return id;
+}
+
+Glib::ustring Inkscape::colorprofile_set_display_per( gpointer buf, guint bufLen, int screen, int monitor )
+{
+    Glib::ustring id;
+
+    while ( static_cast<int>(perMonitorProfiles.size()) <= screen ) {
+        std::vector<MemProfile> tmp;
+        perMonitorProfiles.push_back(tmp);
+    }
+    std::vector<MemProfile>& row = perMonitorProfiles[screen];
+    while ( static_cast<int>(row.size()) <= monitor ) {
+        MemProfile tmp;
+        row.push_back(tmp);
+    }
+    MemProfile& item = row[monitor];
+
+    if ( item.hprof ) {
+        cmsCloseProfile( item.hprof );
+        item.hprof = 0;
+    }
+    id.clear();
+
+    if ( buf && bufLen ) {
+        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);
+    }
+    item.id = id;
+
+    return id;
+}
+
+cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
+{
+    cmsHTRANSFORM result = 0;
+    if ( id.empty() ) {
+        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->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->getBool( "/options/softproof/preserveblack");
+#endif //defined(cmsFLAGS_PRESERVEBLACK)
+                Glib::ustring colorStr = prefs->getString("/options/softproof/gamutcolor");
+                Gdk::Color gamutColor( colorStr.empty() ? "#808080" : colorStr );
+
+                if ( (warn != gamutWarn)
+                     || (lastIntent != intent)
+                     || (lastProofIntent != proofIntent)
+                     || (bpc != lastBPC)
+#if defined(cmsFLAGS_PRESERVEBLACK)
+                     || (preserveBlack != lastPreserveBlack)
+#endif // defined(cmsFLAGS_PRESERVEBLACK)
+                     || (gamutColor != lastGamutColor)
+                    ) {
+                    gamutWarn = warn;
+                    free_transforms();
+                    lastIntent = intent;
+                    lastProofIntent = proofIntent;
+                    lastBPC = bpc;
+#if defined(cmsFLAGS_PRESERVEBLACK)
+                    lastPreserveBlack = preserveBlack;
+#endif // defined(cmsFLAGS_PRESERVEBLACK)
+                    lastGamutColor = gamutColor;
+                }
+
+                // Fetch these now, as they might clear the transform as a side effect.
+                cmsHPROFILE proofProf = item.hprof ? Inkscape::colorprofile_get_proof_profile_handle() : 0;
+
+                if ( !item.transf ) {
+                    if ( item.hprof && proofProf ) {
+                        DWORD dwFlags = cmsFLAGS_SOFTPROOFING;
+                        if ( gamutWarn ) {
+                            dwFlags |= cmsFLAGS_GAMUTCHECK;
+                            cmsSetAlarmCodes(gamutColor.get_red() >> 8, gamutColor.get_green() >> 8, gamutColor.get_blue() >> 8);
+                        }
+                        if ( bpc ) {
+                            dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
+                        }
+#if defined(cmsFLAGS_PRESERVEBLACK)
+                        if ( preserveBlack ) {
+                            dwFlags |= cmsFLAGS_PRESERVEBLACK;
+                        }
+#endif // defined(cmsFLAGS_PRESERVEBLACK)
+                        item.transf = cmsCreateProofingTransform( ColorProfile::getSRGBProfile(), TYPE_ARGB_8, item.hprof, TYPE_ARGB_8, proofProf, intent, proofIntent, dwFlags );
+                    } else if ( item.hprof ) {
+                        item.transf = cmsCreateTransform( ColorProfile::getSRGBProfile(), TYPE_ARGB_8, item.hprof, TYPE_ARGB_8, intent, 0 );
+                    }
+                }
+
+                result = item.transf;
+                found = true;
+            }
+        }
+    }
+
+    return result;
+}
+
+
+
 #endif // ENABLE_LCMS
 
 /*