Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Commit

Permalink
Add color management module for ICC support.
Browse files Browse the repository at this point in the history
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
toasteater authored and fire committed Jul 23, 2019
1 parent 7df56b4 commit 662924b
Show file tree
Hide file tree
Showing 43 changed files with 39,191 additions and 0 deletions.
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ opts.Add(BoolVariable('builtin_bullet', "Use the built-in Bullet library", True)
opts.Add(BoolVariable('builtin_certs', "Bundle default SSL certificates to be used if you don't specify an override in the project settings", True))
opts.Add(BoolVariable('builtin_enet', "Use the built-in ENet library", True))
opts.Add(BoolVariable('builtin_freetype', "Use the built-in FreeType library", True))
opts.Add(BoolVariable('builtin_lcms', "Use the built-in lcms library", True))
opts.Add(BoolVariable('builtin_libogg', "Use the built-in libogg library", True))
opts.Add(BoolVariable('builtin_libpng', "Use the built-in libpng library", True))
opts.Add(BoolVariable('builtin_libtheora', "Use the built-in libtheora library", True))
Expand Down
53 changes: 53 additions & 0 deletions modules/cm/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/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 env["platform"] not in ["windows"]:
# cl.exe does not understand -isystem
env_cm.Append(CPPFLAGS=["-isystem", Dir(thirdparty_dir)])

env_thirdparty = env_cm.Clone()
env_thirdparty.Append(CFLAGS=['-w', '-std=c99', '-O3'])
env_thirdparty.Append(CXXFLAGS=['-w', '-std=c99', '-O3'])
env_thirdparty.add_source_files(env.modules_sources, thirdparty_sources)

# Godot source files
env_cm.add_source_files(env.modules_sources, "*.cpp")
108 changes: 108 additions & 0 deletions modules/cm/color_management_plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*************************************************************************/
/* 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"));

transform->apply_screen_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();
}
51 changes: 51 additions & 0 deletions modules/cm/color_management_plugin.h
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
101 changes: 101 additions & 0 deletions modules/cm/color_profile.cpp
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);
}
}
72 changes: 72 additions & 0 deletions modules/cm/color_profile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*************************************************************************/
/* color_profile.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_PROFILE_H
#define COLOR_PROFILE_H

#include "core/reference.h"
#include "lcms2.h"

class ColorProfile : public Reference {
GDCLASS(ColorProfile, Reference)

bool profile_valid;
cmsHPROFILE profile;

void _set_profile(cmsHPROFILE p_profile);

public:
enum Predef {
PREDEF_SRGB,
};

class Handle {
friend class ColorProfile;
friend class ColorTransform;

cmsHPROFILE profile;

Handle() { profile = NULL; }
Handle(cmsHPROFILE p_profile) { profile = p_profile; }
};

bool is_valid();
Handle get_profile_handle();

void load_predef(Predef p_predef);
void load_from_file(const String &p_file);

ColorProfile();
ColorProfile(Predef p_predef);
ColorProfile(const String &p_file);
~ColorProfile();
};

#endif // COLOR_PROFILE_H
Loading

0 comments on commit 662924b

Please sign in to comment.