Added support for autodetecting the system monitor color profile also on Linux

Code borrowed from Geeqie
This commit is contained in:
Alberto Griggio
2017-04-08 15:18:50 +02:00
parent 73e14702dd
commit b01899783d
5 changed files with 87 additions and 104 deletions

View File

@@ -304,57 +304,6 @@ public:
}
}
void findDefaultMonitorProfile()
{
// Determine the first monitor default profile of operating system, if selected
defaultMonitorProfile.clear();
#ifdef WIN32
// Get current main monitor. Could be fine tuned to get the current windows monitor(multi monitor setup),
// but problem is that we live in RTEngine with no GUI window to query around
HDC hDC = GetDC(nullptr);
if (hDC != nullptr) {
if (SetICMMode(hDC, ICM_ON)) {
char profileName[MAX_PATH + 1];
DWORD profileLength = MAX_PATH;
if (GetICMProfileA(hDC, &profileLength, profileName)) {
defaultMonitorProfile = Glib::ustring(profileName);
defaultMonitorProfile = Glib::path_get_basename(defaultMonitorProfile);
size_t pos = defaultMonitorProfile.rfind(".");
if (pos != Glib::ustring::npos) {
defaultMonitorProfile = defaultMonitorProfile.substr(0, pos);
}
}
// might fail if e.g. the monitor has no profile
}
ReleaseDC(NULL, hDC);
}
#else
// TODO: Add other OS specific code here
#endif
if (options.rtSettings.verbose) {
printf("Default monitor profile is: %s\n", defaultMonitorProfile.c_str());
}
}
cmsHPROFILE getDefaultMonitorProfile()
{
return getProfile(defaultMonitorProfile);
}
Glib::ustring getDefaultMonitorProfileName() const
{
return defaultMonitorProfile;
}
cmsHPROFILE workingSpace(const Glib::ustring& name) const
{
const ProfileMap::const_iterator r = wProfiles.find(name);
@@ -628,21 +577,6 @@ void rtengine::ICCStore::init(const Glib::ustring& usrICCDir, const Glib::ustrin
implementation->init(usrICCDir, stdICCDir, loadAll);
}
void rtengine::ICCStore::findDefaultMonitorProfile()
{
implementation->findDefaultMonitorProfile();
}
cmsHPROFILE rtengine::ICCStore::getDefaultMonitorProfile() const
{
return implementation->getDefaultMonitorProfile();
}
Glib::ustring rtengine::ICCStore::getDefaultMonitorProfileName() const
{
return implementation->getDefaultMonitorProfileName();
}
cmsHPROFILE rtengine::ICCStore::workingSpace(const Glib::ustring& name) const
{
return implementation->workingSpace(name);

View File

@@ -68,11 +68,6 @@ public:
void init(const Glib::ustring& usrICCDir, const Glib::ustring& stdICCDir, bool loadAll);
// Main monitors standard profile name, from OS
void findDefaultMonitorProfile();
cmsHPROFILE getDefaultMonitorProfile() const;
Glib::ustring getDefaultMonitorProfileName() const;
cmsHPROFILE workingSpace(const Glib::ustring& name) const;
cmsHPROFILE workingSpaceGamma(const Glib::ustring& name) const;
TMatrix workingSpaceMatrix(const Glib::ustring& name) const;

View File

@@ -41,7 +41,6 @@ int init (const Settings* s, Glib::ustring baseDir, Glib::ustring userSettingsDi
{
settings = s;
ICCStore::getInstance()->init (s->iccDirectory, Glib::build_filename (baseDir, "iccprofiles"), loadAll);
ICCStore::getInstance()->findDefaultMonitorProfile();
DCPStore::getInstance()->init (Glib::build_filename (baseDir, "dcpprofiles"), loadAll);
CameraConstantsStore::getInstance ()->init (baseDir, userSettingsDir);

View File

@@ -71,6 +71,67 @@ int setprogressStrUI ( void *p )
return FALSE;
}
bool find_default_monitor_profile(GdkWindow *rootwin, Glib::ustring &defprof, Glib::ustring &defprofname)
{
#ifdef WIN32
HDC hDC = GetDC(nullptr);
if (hDC != nullptr) {
if (SetICMMode(hDC, ICM_ON)) {
char profileName[MAX_PATH + 1];
DWORD profileLength = MAX_PATH;
if (GetICMProfileA(hDC, &profileLength, profileName)) {
defprof = Glib::ustring(profileName);
defprofname = Glib::path_get_basename(defprof);
size_t pos = defprofname.rfind(".");
if (pos != Glib::ustring::npos) {
defprofname = defprofname.substr(0, pos);
}
defprof = Glib::ustring("file:") + defprof;
return true;
}
// might fail if e.g. the monitor has no profile
}
ReleaseDC(NULL, hDC);
}
#elif !defined(__APPLE__)
// taken from geeqie (image.c) and adapted
// Originally licensed as GPL v2+, with the following copyright:
// * Copyright (C) 2006 John Ellis
// * Copyright (C) 2008 - 2016 The Geeqie Team
//
guchar *prof;
gint proflen;
GdkAtom type = GDK_NONE;
gint format = 0;
if (gdk_property_get(rootwin, gdk_atom_intern("_ICC_PROFILE", FALSE), GDK_NONE, 0, 64 * 1024 * 1024, FALSE, &type, &format, &proflen, &prof) && proflen > 0) {
cmsHPROFILE p = cmsOpenProfileFromMem(prof, proflen);
if (p) {
defprofname = "GDK_ICC_PROFILE";
defprof = Glib::build_filename(Options::rtdir, "GDK_ICC_PROFILE.icc");
if (cmsSaveProfileToFile(p, defprof.c_str())) {
cmsCloseProfile(p);
if (prof) {
g_free(prof);
}
defprof = Glib::ustring("file:") + defprof;
return true;
}
}
}
if (prof) {
g_free(prof);
}
#endif
return false;
}
}
class EditorPanel::ColorManagementToolbar
@@ -84,6 +145,7 @@ private:
Gtk::ToggleButton spGamutCheck;
sigc::connection profileConn, intentConn, softproofConn;
bool canSProof;
Glib::ustring defprof;
rtengine::StagedImageProcessor* const& processor;
@@ -95,12 +157,13 @@ private:
setExpandAlignProperties (&profileBox, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_FILL);
profileBox.append (M ("PREFERENCES_PROFILE_NONE"));
#ifdef WIN32
profileBox.append (M ("MONITOR_PROFILE_SYSTEM") + " (" + rtengine::ICCStore::getInstance()->getDefaultMonitorProfileName() + ")");
profileBox.set_active (options.rtSettings.autoMonitorProfile ? 1 : 0);
#else
profileBox.set_active (0);
#endif
Glib::ustring defprofname;
if (find_default_monitor_profile(profileBox.get_root_window()->gobj(), defprof, defprofname)) {
profileBox.append (M ("MONITOR_PROFILE_SYSTEM") + " (" + defprofname + ")");
profileBox.set_active (options.rtSettings.autoMonitorProfile ? 1 : 0);
} else {
profileBox.set_active (0);
}
const std::vector<Glib::ustring> profiles = rtengine::ICCStore::getInstance()->getProfiles (rtengine::ICCStore::ProfileType::MONITOR);
for (const auto profile: profiles) {
profileBox.append (profile);
@@ -174,21 +237,17 @@ private:
Glib::ustring profile;
#if !defined(__APPLE__) // monitor profile not supported on apple
#ifdef WIN32
if (profileBox.get_active_row_number () == 1) {
profile = rtengine::ICCStore::getInstance()->getDefaultMonitorProfileName ();
if (!defprof.empty() && profileBox.get_active_row_number () == 1) {
profile = defprof;
if (profile.empty ()) {
profile = options.rtSettings.monitorProfile;
}
if (profile.empty ()) {
profile = "sRGB IEC61966-2.1";
}
} else if (profileBox.get_active_row_number () > 1) {
} else if (profileBox.get_active_row_number () > 0) {
profile = profileBox.get_active_text ();
}
#else
profile = profileBox.get_active_row_number () > 0 ? profileBox.get_active_text () : Glib::ustring ();
#endif
#else
profile = "RT_sRGB";
#endif
@@ -342,15 +401,11 @@ public:
#if !defined(__APPLE__) // monitor profile not supported on apple
ConnectionBlocker profileBlocker (profileConn);
#ifdef WIN32
if (options.rtSettings.autoMonitorProfile) {
if (!defprof.empty() && options.rtSettings.autoMonitorProfile) {
setActiveTextOrIndex (profileBox, options.rtSettings.monitorProfile, 1);
} else {
setActiveTextOrIndex (profileBox, options.rtSettings.monitorProfile, 0);
}
#else
setActiveTextOrIndex (profileBox, options.rtSettings.monitorProfile, 0);
#endif
#endif
switch (options.rtSettings.monitorIntent) {

View File

@@ -756,11 +756,11 @@ Gtk::Widget* Preferences::getColorManagementPanel ()
setExpandAlignProperties(monBPC, false, false, Gtk::ALIGN_START, Gtk::ALIGN_CENTER);
monBPC->set_active (true);
#if defined(WIN32) // Auto-detection not implemented for Linux, see issue 851
//#if defined(WIN32) // Auto-detection not implemented for Linux, see issue 851
cbAutoMonProfile = Gtk::manage (new Gtk::CheckButton (M("PREFERENCES_AUTOMONPROFILE")));
setExpandAlignProperties(cbAutoMonProfile, false, false, Gtk::ALIGN_START, Gtk::ALIGN_CENTER);
autoMonProfileConn = cbAutoMonProfile->signal_toggled().connect (sigc::mem_fun(*this, &Preferences::autoMonProfileToggled));
#endif
//#endif
int row = 0;
gmonitor->attach (*mplabel, 0, row, 1, 1);
@@ -772,18 +772,18 @@ Gtk::Widget* Preferences::getColorManagementPanel ()
gmonitor->attach (*monProfile, 1, row, 1, 1);
#endif
++row;
#if defined(WIN32)
//#if defined(WIN32)
gmonitor->attach (*cbAutoMonProfile, 1, row, 1, 1);
++row;
#endif
//#endif
gmonitor->attach (*milabel, 0, row, 1, 1);
gmonitor->attach (*monIntent, 1, row, 1, 1);
++row;
gmonitor->attach (*monBPC, 0, row, 2, 1);
#if defined(WIN32)
//#if defined(WIN32)
autoMonProfileToggled();
#endif
//#endif
fmonitor->add(*gmonitor);
@@ -830,9 +830,9 @@ Gtk::Widget* Preferences::getColorManagementPanel ()
++row;
gprinter->attach (*prtBPC, 0, row, 2, 1);
#if defined(WIN32)
//#if defined(WIN32)
autoMonProfileToggled();
#endif
//#endif
fprinter->add(*gprinter);
@@ -1677,9 +1677,9 @@ void Preferences::storePreferences ()
break;
}
moptions.rtSettings.monitorBPC = monBPC->get_active ();
#if defined(WIN32)
//#if defined(WIN32)
moptions.rtSettings.autoMonitorProfile = cbAutoMonProfile->get_active ();
#endif
//#endif
#endif
moptions.rtSettings.iccDirectory = iccDir->get_filename ();
@@ -1825,9 +1825,9 @@ void Preferences::fillPreferences ()
break;
}
monBPC->set_active (moptions.rtSettings.monitorBPC);
#if defined(WIN32)
//#if defined(WIN32)
cbAutoMonProfile->set_active(moptions.rtSettings.autoMonitorProfile);
#endif
//#endif
#endif
if (Glib::file_test (moptions.rtSettings.iccDirectory, Glib::FILE_TEST_IS_DIR)) {
@@ -2033,12 +2033,12 @@ void Preferences::savePressed () {
}
*/
#if defined(WIN32)
//#if defined(WIN32)
void Preferences::autoMonProfileToggled ()
{
monProfile->set_sensitive(!cbAutoMonProfile->get_active());
}
#endif
//#endif
/*
void Preferences::autocielabToggled () {
// cbAutocielab->set_sensitive(cbAutocielab->get_active());