Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / src / color-profile.cpp
index b2487a6a9ec28eb787b07dd6370b9738189fb13e..1189a7c29701f1886b7687d6de5105b652e04b28 100644 (file)
@@ -1,10 +1,13 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-
-//#define DEBUG_LCMS
+#define noDEBUG_LCMS
 
 #include <glib/gstdio.h>
 #include <sys/fcntl.h>
 #include <gdkmm/color.h>
+#include <glib/gi18n.h>
 
 #ifdef DEBUG_LCMS
 #include <gtk/gtkmessagedialog.h>
 
 #include <cstring>
 #include <string>
-// #ifdef WIN32
-// #include <windows.h>
-// #include <Icm.h>
-// #endif
+#include <io/sys.h>
+
+#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"
@@ -27,6 +36,9 @@
 #include "dom/uri.h"
 #include "dom/util/digest.h"
 
+#ifdef WIN32
+#include <icm.h>
+#endif // WIN32
 
 using Inkscape::ColorProfile;
 using Inkscape::ColorProfileClass;
@@ -41,7 +53,7 @@ static cmsHPROFILE colorprofile_get_proof_profile_handle();
 
 #ifdef DEBUG_LCMS
 extern guint update_in_progress;
-#define DEBUG_MESSAGE(key, ...) \
+#define DEBUG_MESSAGE_SCISLAC(key, ...) \
 {\
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();\
     bool dump = prefs->getBool(Glib::ustring("/options/scislac/") + #key);\
@@ -67,6 +79,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;
@@ -82,6 +101,15 @@ cmsHPROFILE ColorProfile::getSRGBProfile() {
     return _sRGBProf;
 }
 
+cmsHPROFILE ColorProfile::_NullProf = 0;
+
+cmsHPROFILE ColorProfile::getNULLProfile() {
+    if ( !_NullProf ) {
+        _NullProf = cmsCreateNULLProfile();
+    }
+    return _NullProf;
+}
+
 #endif // ENABLE_LCMS
 
 /**
@@ -142,6 +170,7 @@ void ColorProfile::init( ColorProfile *cprof )
     cprof->_profileSpace = icSigRgbData;
     cprof->_transf = 0;
     cprof->_revTransf = 0;
+    cprof->_gamutTransf = 0;
 #endif // ENABLE_LCMS
 }
 
@@ -151,7 +180,7 @@ void ColorProfile::init( ColorProfile *cprof )
 void ColorProfile::release( SPObject *object )
 {
     // Unregister ourselves
-    Document* document = SP_OBJECT_DOCUMENT(object);
+    SPDocument* document = SP_OBJECT_DOCUMENT(object);
     if ( document ) {
         sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "iccprofile", SP_OBJECT (object));
     }
@@ -195,6 +224,10 @@ void ColorProfile::_clearProfile()
         cmsDeleteTransform( _revTransf );
         _revTransf = 0;
     }
+    if ( _gamutTransf ) {
+        cmsDeleteTransform( _gamutTransf );
+        _gamutTransf = 0;
+    }
     if ( profHandle ) {
         cmsCloseProfile( profHandle );
         profHandle = 0;
@@ -205,7 +238,7 @@ void ColorProfile::_clearProfile()
 /**
  * Callback: set attributes from associated repr.
  */
-void ColorProfile::build( SPObject *object, Document *document, Inkscape::XML::Node *repr )
+void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
 {
     ColorProfile *cprof = COLORPROFILE(object);
     g_assert(cprof->href == 0);
@@ -251,7 +284,7 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
 
                     // Try to open relative
-                    Document *doc = SP_OBJECT_DOCUMENT(object);
+                    SPDocument *doc = SP_OBJECT_DOCUMENT(object);
                     if (!doc) {
                         doc = SP_ACTIVE_DOCUMENT;
                         g_warning("object has no document.  using active");
@@ -263,14 +296,17 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
                         // 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 = g_strdup((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 ) {
@@ -280,6 +316,8 @@ 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
                 }
@@ -436,7 +474,7 @@ static int getLcmsIntent( guint svgIntent )
     return intent;
 }
 
-static SPObject* bruteFind( Document* document, gchar const* name )
+static SPObject* bruteFind( SPDocument* document, gchar const* name )
 {
     SPObject* result = 0;
     const GSList * current = sp_document_get_resource_list(document, "iccprofile");
@@ -456,7 +494,7 @@ static SPObject* bruteFind( Document* document, gchar const* name )
     return result;
 }
 
-cmsHPROFILE Inkscape::colorprofile_get_handle( Document* document, guint* intent, gchar const* name )
+cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar const* name )
 {
     cmsHPROFILE prof = 0;
 
@@ -478,7 +516,7 @@ cmsHPROFILE Inkscape::colorprofile_get_handle( Document* document, guint* intent
 
 cmsHTRANSFORM ColorProfile::getTransfToSRGB8()
 {
-    if ( !_transf ) {
+    if ( !_transf && profHandle ) {
         int intent = getLcmsIntent(rendering_intent);
         _transf = cmsCreateTransform( profHandle, _getInputFormat(_profileSpace), getSRGBProfile(), TYPE_RGBA_8, intent, 0 );
     }
@@ -487,15 +525,38 @@ 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;
+}
 
-#include <io/sys.h>
+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);
+}
 
 class ProfileInfo
 {
@@ -568,7 +629,14 @@ Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
 }
 #endif // ENABLE_LCMS
 
-std::list<Glib::ustring> ColorProfile::getProfileDirs() {
+std::list<Glib::ustring> ColorProfile::getBaseProfileDirs() {
+#if ENABLE_LCMS
+    static bool warnSet = false;
+    if (!warnSet) {
+        cmsErrorAction( LCMS_ERROR_SHOW );
+        warnSet = true;
+    }
+#endif // ENABLE_LCMS
     std::list<Glib::ustring> sources;
 
     gchar* base = profile_path("XXX");
@@ -593,40 +661,77 @@ std::list<Glib::ustring> ColorProfile::getProfileDirs() {
     }
 
     // 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");
+    {
+        bool onOSX = false;
+        std::list<Glib::ustring> possible;
+        possible.push_back("/System/Library/ColorSync/Profiles");
+        possible.push_back("/Library/ColorSync/Profiles");
+        for ( std::list<Glib::ustring>::const_iterator it = possible.begin(); it != possible.end(); ++it ) {
+            if ( g_file_test(it->c_str(), G_FILE_TEST_EXISTS)  && g_file_test(it->c_str(), G_FILE_TEST_IS_DIR) ) {
+                sources.push_back(it->c_str());
+                onOSX = true;
+            }
+        }
+        if ( onOSX ) {
+            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);
+        }
+    }
 
-        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);
+#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(path);
+        g_free( utf8Path );
     }
+#endif // WIN32
 
+    return sources;
+}
 
-// #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.pushback(utf8Path);
-//         }
-//         g_free( utf8Path );
-//     }
-// #endif // WIN32
+static bool isIccFile( gchar const *filepath )
+{
+    bool isIccFile = false;
+    struct stat st;
+    if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
+        //0-3 == size
+        //36-39 == 'acsp' 0x61637370
+        int fd = g_open( filepath, O_RDONLY, S_IRWXU);
+        if ( fd != -1 ) {
+            guchar scratch[40] = {0};
+            size_t len = sizeof(scratch);
+
+            //size_t left = 40;
+            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 <= static_cast<size_t>(st.st_size) ) {
+                    isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
+                }
+            }
 
-    return sources;
+            close(fd);
+        }
+    }
+    return isIccFile;
 }
 
-#if ENABLE_LCMS
-static void findThings() {
-    std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
+std::list<Glib::ustring> ColorProfile::getProfileFiles()
+{
+    std::list<Glib::ustring> files;
 
+    std::list<Glib::ustring> sources = ColorProfile::getBaseProfileDirs();
     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;
@@ -635,57 +740,49 @@ static void findThings() {
             if (dir) {
                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
                     gchar *filepath = g_build_filename(it->c_str(), file, NULL);
-
-
                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
                         sources.push_back(g_strdup(filepath));
                     } else {
-                        bool isIccFile = false;
-                        struct stat st;
-                        if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
-                            //0-3 == size
-                            //36-39 == 'acsp' 0x61637370
-                            int fd = g_open( filepath, O_RDONLY, S_IRWXU);
-                            if ( fd != -1 ) {
-                                guchar scratch[40] = {0};
-                                size_t len = sizeof(scratch);
-
-                                //size_t left = 40;
-                                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 <= static_cast<size_t>(st.st_size) ) {
-                                        isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
-                                    }
-                                }
-
-                                close(fd);
-                            }
-                        }
-
-                        if ( isIccFile ) {
-                            cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
-                            if ( prof ) {
-                                ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
-                                cmsCloseProfile( prof );
-
-                                bool sameName = false;
-                                for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
-                                    if ( it->getName() == info.getName() ) {
-                                        sameName = true;
-                                        break;
-                                    }
-                                }
-
-                                if ( !sameName ) {
-                                    knownProfiles.push_back(info);
-                                }
-                            }
+                        if ( isIccFile( filepath ) ) {
+                            files.push_back( filepath );
                         }
                     }
 
                     g_free(filepath);
                 }
+                g_dir_close(dir);
+                dir = 0;
+            } else {
+                gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
+                g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
+                g_free(safeDir);
+            }
+        }
+    }
+
+    return files;
+}
+
+#if ENABLE_LCMS
+static void findThings() {
+    std::list<Glib::ustring> files = ColorProfile::getProfileFiles();
+
+    for ( std::list<Glib::ustring>::const_iterator it = files.begin(); it != files.end(); ++it ) {
+        cmsHPROFILE prof = cmsOpenProfileFromFile( it->c_str(), "r" );
+        if ( prof ) {
+            ProfileInfo info( prof, Glib::filename_to_utf8( it->c_str() ) );
+            cmsCloseProfile( prof );
+
+            bool sameName = false;
+            for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
+                if ( it->getName() == info.getName() ) {
+                    sameName = true;
+                    break;
+                }
+            }
+
+            if ( !sameName ) {
+                knownProfiles.push_back(info);
             }
         }
     }
@@ -1083,4 +1180,4 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :