forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add color management module for ICC support.
This module adds Color Management to engine settings. It depends on LittleCMS for ICC profile handling, and LUT support in Rasterizer for rendering. This completes godotengine#26826.
- Loading branch information
Showing
43 changed files
with
39,216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python | ||
|
||
Import('env') | ||
Import('env_modules') | ||
|
||
env_cm = env_modules.Clone() | ||
|
||
# Thirdparty source files | ||
if env['builtin_lcms']: | ||
thirdparty_dir = "#thirdparty/lcms2/" | ||
thirdparty_sources = [ | ||
"cmscnvrt.c", | ||
"cmserr.c", | ||
"cmsgamma.c", | ||
"cmsgmt.c", | ||
"cmsintrp.c", | ||
"cmsio0.c", | ||
"cmsio1.c", | ||
"cmslut.c", | ||
"cmsplugin.c", | ||
"cmssm.c", | ||
"cmsmd5.c", | ||
"cmsmtrx.c", | ||
"cmspack.c", | ||
"cmspcs.c", | ||
"cmswtpnt.c", | ||
"cmsxform.c", | ||
"cmssamp.c", | ||
"cmsnamed.c", | ||
"cmscam02.c", | ||
"cmsvirt.c", | ||
"cmstypes.c", | ||
"cmscgats.c", | ||
"cmsps2.c", | ||
"cmsopt.c", | ||
"cmshalf.c", | ||
"cmsalpha.c", | ||
] | ||
|
||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources] | ||
|
||
env_cm.Append(CPPPATH=[thirdparty_dir]) | ||
if not env.msvc: | ||
# cl.exe does not understand -isystem | ||
env_cm.Append(CPPFLAGS=["-isystem", Dir(thirdparty_dir)]) | ||
|
||
env_thirdparty = env_cm.Clone() | ||
|
||
if not env.msvc: | ||
env_thirdparty.Append(CFLAGS=['-w', '-std=c99', '-O3']) | ||
env_thirdparty.Append(CXXFLAGS=['-w', '-std=c99', '-O3']) | ||
else: | ||
# msvc doesn't understand c99 & looks like /w is not needed | ||
env_thirdparty.Append(CFLAGS=['/O2']) | ||
env_thirdparty.Append(CXXFLAGS=['/O2']) | ||
|
||
env_thirdparty.add_source_files(env.modules_sources, thirdparty_sources) | ||
|
||
# Godot source files | ||
env_cm.add_source_files(env.modules_sources, "*.cpp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/*************************************************************************/ | ||
/* color_profile.cpp */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#include "color_management_plugin.h" | ||
#include "color_profile.h" | ||
#include "color_transform.h" | ||
#include "editor/editor_settings.h" | ||
#include "servers/visual_server.h" | ||
|
||
void ColorManagementPlugin::_clear_lut() { | ||
VS::get_singleton()->set_screen_lut(Ref<Image>(), 0, 0); | ||
} | ||
|
||
void ColorManagementPlugin::_on_settings_change() { | ||
String profile = EDITOR_GET("interface/color_management/screen_profile"); | ||
if (profile.empty()) { | ||
return _clear_lut(); | ||
} | ||
|
||
Ref<ColorProfile> screen_profile = memnew(ColorProfile(profile)); | ||
if (screen_profile.is_null() || !screen_profile->is_valid()) { | ||
ERR_PRINTS("Failed to load profile from: " + profile + "."); | ||
return _clear_lut(); | ||
} | ||
|
||
Ref<ColorTransform> transform = memnew(ColorTransform()); | ||
if (transform.is_null()) { | ||
ERR_FAIL_V(_clear_lut()); | ||
} | ||
|
||
transform->set_src_profile(memnew(ColorProfile(ColorProfile::PREDEF_SRGB))); | ||
transform->set_dst_profile(screen_profile); | ||
|
||
String intent = EDITOR_GET("interface/color_management/intent"); | ||
if (intent == "Perceptual") { | ||
transform->set_intent(ColorTransform::CM_INTENT_PERCEPTUAL); | ||
} else if (intent == "Relative Colorimetric") { | ||
transform->set_intent(ColorTransform::CM_INTENT_RELATIVE); | ||
} else if (intent == "Saturation") { | ||
transform->set_intent(ColorTransform::CM_INTENT_SATURATION); | ||
} else if (intent == "Absolute Colorimetric") { | ||
transform->set_intent(ColorTransform::CM_INTENT_ABSOLUTE); | ||
} else { | ||
WARN_PRINTS("Unexpected color management intent: " + intent + ". Falling back to Perceptual."); | ||
transform->set_intent(ColorTransform::CM_INTENT_PERCEPTUAL); | ||
} | ||
|
||
transform->set_black_point_compensation(EDITOR_GET("interface/color_management/black_point_compensation")); | ||
|
||
if (!transform->apply_screen_lut()) { | ||
ERR_PRINTS("Failed to apply screen LUT, falling back to identity"); | ||
_clear_lut(); | ||
} | ||
} | ||
|
||
void ColorManagementPlugin::_register_settings() { | ||
EditorSettings *es = EditorSettings::get_singleton(); | ||
|
||
EDITOR_DEF("interface/color_management/screen_profile", ""); | ||
es->add_property_hint(PropertyInfo(Variant::STRING, "interface/color_management/screen_profile", PROPERTY_HINT_GLOBAL_FILE, "*.icc,*.icm", PROPERTY_USAGE_DEFAULT)); | ||
|
||
EDITOR_DEF("interface/color_management/intent", "Perceptual"); | ||
es->add_property_hint(PropertyInfo(Variant::STRING, "interface/color_management/intent", PROPERTY_HINT_ENUM, | ||
"Perceptual" | ||
"," | ||
"Relative Colorimetric" | ||
"," | ||
"Saturation" | ||
"," | ||
"Absolute Colorimetric")); | ||
|
||
EDITOR_DEF("interface/color_management/black_point_compensation", true); | ||
es->add_property_hint(PropertyInfo(Variant::BOOL, "interface/color_management/black_point_compensation")); | ||
} | ||
|
||
void ColorManagementPlugin::_bind_methods() { | ||
ClassDB::bind_method("_editor_settings_changed", &ColorManagementPlugin::_on_settings_change); | ||
} | ||
|
||
ColorManagementPlugin::ColorManagementPlugin() { | ||
EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed"); | ||
_register_settings(); | ||
_on_settings_change(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*************************************************************************/ | ||
/* color_management_plugin.h */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#ifndef COLOR_MANAGEMENT_PLUGIN_H | ||
#define COLOR_MANAGEMENT_PLUGIN_H | ||
|
||
#include "editor/editor_plugin.h" | ||
|
||
class ColorManagementPlugin : public EditorPlugin { | ||
GDCLASS(ColorManagementPlugin, EditorPlugin) | ||
|
||
void _on_settings_change(); | ||
void _register_settings(); | ||
void _clear_lut(); | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
public: | ||
ColorManagementPlugin(); | ||
virtual ~ColorManagementPlugin() {} | ||
}; | ||
|
||
#endif // COLOR_MANAGEMENT_PLUGIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*************************************************************************/ | ||
/* color_profile.cpp */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#include "color_profile.h" | ||
|
||
bool ColorProfile::is_valid() { | ||
|
||
return profile_valid && profile != NULL; | ||
} | ||
|
||
void ColorProfile::_set_profile(cmsHPROFILE p_profile) { | ||
|
||
if (is_valid()) { | ||
cmsCloseProfile(profile); | ||
} | ||
|
||
profile = p_profile; | ||
profile_valid = p_profile != NULL; | ||
} | ||
|
||
ColorProfile::Handle ColorProfile::get_profile_handle() { | ||
|
||
if (!is_valid()) { | ||
ERR_PRINT("Trying to call get_profile_handle() on an invalid ColorProfile instance."); | ||
return Handle(); | ||
} | ||
return Handle(profile); | ||
} | ||
|
||
void ColorProfile::load_predef(Predef p_predef) { | ||
|
||
switch (p_predef) { | ||
case PREDEF_SRGB: | ||
_set_profile(cmsCreate_sRGBProfile()); | ||
break; | ||
default: | ||
ERR_PRINT("ColorProfile creation failure: Unknown predefined profile\n"); | ||
break; | ||
} | ||
} | ||
|
||
void ColorProfile::load_from_file(const String &p_file) { | ||
|
||
CharString utf_path = p_file.utf8(); | ||
cmsHPROFILE p = cmsOpenProfileFromFile(utf_path.ptr(), "r"); | ||
if (p == NULL) { | ||
ERR_PRINTS("Failed to load ICC profile from file: " + p_file + "."); | ||
} | ||
_set_profile(p); | ||
} | ||
|
||
ColorProfile::ColorProfile() { | ||
|
||
profile_valid = false; | ||
} | ||
|
||
ColorProfile::ColorProfile(Predef p_predef) { | ||
|
||
profile_valid = false; | ||
load_predef(p_predef); | ||
} | ||
|
||
ColorProfile::ColorProfile(const String &p_file) { | ||
|
||
profile_valid = false; | ||
load_from_file(p_file); | ||
} | ||
|
||
ColorProfile::~ColorProfile() { | ||
|
||
if (is_valid()) { | ||
cmsCloseProfile(profile); | ||
} | ||
} |
Oops, something went wrong.