Skip to content

Commit

Permalink
Color-separated Saturation
Browse files Browse the repository at this point in the history
Signed-off-by: Markus KARG <markus@headcrashing.eu>
  • Loading branch information
mkarg committed Nov 19, 2019
1 parent d539e46 commit adeb452
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/effects/Saturation.h
Expand Up @@ -65,14 +65,17 @@ namespace openshot

public:
Keyframe saturation; ///< The color saturation: 0.0 = black and white, 1.0 = normal, 2.0 = double saturation
Keyframe saturation_R; ///< Red color saturation
Keyframe saturation_G; ///< Green color saturation
Keyframe saturation_B; ///< Blue color saturation

/// Blank constructor, useful when using Json to load the effect properties
Saturation();

/// Default constructor, which takes 1 curve, to adjust the color saturation over time.
///
/// @param new_saturation The curve to adjust the saturation of the frame's image (0.0 = black and white, 1.0 = normal, 2.0 = double saturation)
Saturation(Keyframe new_saturation);
Saturation(Keyframe new_saturation, Keyframe new_saturation_R, Keyframe new_saturation_G, Keyframe new_saturation_B);

/// @brief This method is required for all derived classes of EffectBase, and returns a
/// modified openshot::Frame object
Expand Down
75 changes: 73 additions & 2 deletions src/effects/Saturation.cpp
Expand Up @@ -33,13 +33,13 @@
using namespace openshot;

/// Blank constructor, useful when using Json to load the effect properties
Saturation::Saturation() : saturation(1.0) {
Saturation::Saturation() : saturation(1.0), saturation_R(1.0), saturation_G(1.0), saturation_B(1.0) {
// Init effect properties
init_effect_details();
}

// Default constructor
Saturation::Saturation(Keyframe new_saturation) : saturation(new_saturation)
Saturation::Saturation(Keyframe new_saturation, Keyframe new_saturation_R, Keyframe new_saturation_G, Keyframe new_saturation_B) : saturation(new_saturation), saturation_R(new_saturation_R), saturation_G(new_saturation_G), saturation_B(new_saturation_B)
{
// Init effect properties
init_effect_details();
Expand Down Expand Up @@ -71,6 +71,9 @@ std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_

// Get keyframe values for this frame
float saturation_value = saturation.GetValue(frame_number);
float saturation_value_R = saturation_R.GetValue(frame_number);
float saturation_value_G = saturation_G.GetValue(frame_number);
float saturation_value_B = saturation_B.GetValue(frame_number);

// Constants used for color saturation formula
double pR = .299;
Expand All @@ -87,6 +90,10 @@ std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_
int B = pixels[byte_index + 2];
int A = pixels[byte_index + 3];

/*
* Common saturation adjustment
*/

// Calculate the saturation multiplier
double p = sqrt( (R * R * pR) +
(G * G * pG) +
Expand All @@ -102,6 +109,58 @@ std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_
G = constrain(G);
B = constrain(B);

/*
* Color-separated saturation adjustment
*
* Splitting each of the three subpixels (R, G and B) into three distincs sub-subpixels (R, G and B in turn)
* which in their optical sum reproduce the original subpixel's color OR produce white light in the brightness
* of the original subpixel (dependening on the color channel's slider value).
*/

// Three subpixels producing either R or white with brightness of R
int Rr = R;
int Gr = 0;
int Br = 0;

// Three subpixels producing either G or white with brightness of G
int Rg = 0;
int Gg = G;
int Bg = 0;

// Three subpixels producing either B or white with brightness of B
int Rb = 0;
int Gb = 0;
int Bb = B;

// Compute the brightness ("saturation multiplier") of the replaced subpixels
// Actually mathematical no-ops mostly, verbosity is kept just for clarification
const double p_r = sqrt( (Rr * Rr * pR) + (Gr * Gr * pG) + (Br * Br * pB) );
const double p_g = sqrt( (Rg * Rg * pR) + (Gg * Gg * pG) + (Bg * Bg * pB) );
const double p_b = sqrt( (Rb * Rb * pR) + (Gb * Gb * pG) + (Bb * Bb * pB) );

// Adjust the saturation
Rr = p_r + (Rr - p_r) * saturation_value_R;
Gr = p_r + (Gr - p_r) * saturation_value_R;
Br = p_r + (Br - p_r) * saturation_value_R;

Rg = p_g + (Rg - p_g) * saturation_value_G;
Gg = p_g + (Gg - p_g) * saturation_value_G;
Bg = p_g + (Bg - p_g) * saturation_value_G;

Rb = p_b + (Rb - p_b) * saturation_value_B;
Gb = p_b + (Gb - p_b) * saturation_value_B;
Bb = p_b + (Bb - p_b) * saturation_value_B;

// Recombine brightness of sub-subpixels (Rx, Gx and Bx) into sub-pixels (R, G and B) again
R = Rr + Rg + Rb;
G = Gr + Gg + Gb;
B = Br + Bg + Bb;

// Constrain the value from 0 to 255
R = constrain(R);
G = constrain(G);
B = constrain(B);

// Set all pixels to new value
pixels[byte_index] = R;
pixels[byte_index + 1] = G;
Expand All @@ -127,6 +186,9 @@ Json::Value Saturation::JsonValue() {
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["saturation"] = saturation.JsonValue();
root["saturation_R"] = saturation_R.JsonValue();
root["saturation_G"] = saturation_G.JsonValue();
root["saturation_B"] = saturation_B.JsonValue();

// return JsonValue
return root;
Expand Down Expand Up @@ -170,6 +232,12 @@ void Saturation::SetJsonValue(Json::Value root) {
// Set data from Json (if key is found)
if (!root["saturation"].isNull())
saturation.SetJsonValue(root["saturation"]);
if (!root["saturation_R"].isNull())
saturation_R.SetJsonValue(root["saturation_R"]);
if (!root["saturation_G"].isNull())
saturation_G.SetJsonValue(root["saturation_G"]);
if (!root["saturation_B"].isNull())
saturation_B.SetJsonValue(root["saturation_B"]);
}

// Get all properties for a specific frame
Expand All @@ -186,6 +254,9 @@ std::string Saturation::PropertiesJSON(int64_t requested_frame) {

// Keyframes
root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", &saturation, 0.0, 4.0, false, requested_frame);
root["saturation_R"] = add_property_json("Saturation (Red)", saturation_R.GetValue(requested_frame), "float", "", &saturation_R, 0.0, 4.0, false, requested_frame);
root["saturation_G"] = add_property_json("Saturation (Green)", saturation_G.GetValue(requested_frame), "float", "", &saturation_G, 0.0, 4.0, false, requested_frame);
root["saturation_B"] = add_property_json("Saturation (Blue)", saturation_B.GetValue(requested_frame), "float", "", &saturation_B, 0.0, 4.0, false, requested_frame);

// Return formatted string
return root.toStyledString();
Expand Down

0 comments on commit adeb452

Please sign in to comment.