OpenShot Library | libopenshot 0.2.7
ChromaKey.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for ChromaKey class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 *
6 * @ref License
7 */
8
9/* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#include "ChromaKey.h"
32#include "Exceptions.h"
33
34using namespace openshot;
35
36/// Blank constructor, useful when using Json to load the effect properties
37ChromaKey::ChromaKey() : fuzz(5.0) {
38 // Init default color
39 color = Color();
40
41 // Init effect properties
42 init_effect_details();
43}
44
45// Default constructor, which takes an openshot::Color object and a 'fuzz' factor, which
46// is used to determine how similar colored pixels are matched. The higher the fuzz, the
47// more colors are matched.
48ChromaKey::ChromaKey(Color color, Keyframe fuzz) : color(color), fuzz(fuzz)
49{
50 // Init effect properties
51 init_effect_details();
52}
53
54// Init effect settings
55void ChromaKey::init_effect_details()
56{
57 /// Initialize the values of the EffectInfo struct.
59
60 /// Set the effect info
61 info.class_name = "ChromaKey";
62 info.name = "Chroma Key (Greenscreen)";
63 info.description = "Replaces the color (or chroma) of the frame with transparency (i.e. keys out the color).";
64 info.has_audio = false;
65 info.has_video = true;
66}
67
68// This method is required for all derived classes of EffectBase, and returns a
69// modified openshot::Frame object
70std::shared_ptr<openshot::Frame> ChromaKey::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
71{
72 // Determine the current HSL (Hue, Saturation, Lightness) for the Chrome
73 int threshold = fuzz.GetInt(frame_number);
74 long mask_R = color.red.GetInt(frame_number);
75 long mask_G = color.green.GetInt(frame_number);
76 long mask_B = color.blue.GetInt(frame_number);
77
78 // Get source image's pixels
79 std::shared_ptr<QImage> image = frame->GetImage();
80 unsigned char *pixels = (unsigned char *) image->bits();
81
82 // Loop through pixels
83 for (int pixel = 0, byte_index=0; pixel < image->width() * image->height(); pixel++, byte_index+=4)
84 {
85 // Get the RGB values from the pixel
86 // Remove the premultiplied alpha values from R,G,B
87 float A = float(pixels[byte_index + 3]);
88 unsigned char R = (pixels[byte_index] / A) * 255.0;
89 unsigned char G = (pixels[byte_index + 1] / A) * 255.0;
90 unsigned char B = (pixels[byte_index + 2] / A) * 255.0;
91
92 // Get distance between mask color and pixel color
93 long distance = Color::GetDistance((long)R, (long)G, (long)B, mask_R, mask_G, mask_B);
94
95 if (distance <= threshold) {
96 // MATCHED - Make pixel transparent
97 // Due to premultiplied alpha, we must also zero out
98 // the individual color channels (or else artifacts are left behind)
99 pixels[byte_index] = 0;
100 pixels[byte_index + 1] = 0;
101 pixels[byte_index + 2] = 0;
102 pixels[byte_index + 3] = 0;
103 }
104 }
105
106 // return the modified frame
107 return frame;
108}
109
110// Generate JSON string of this object
111std::string ChromaKey::Json() const {
112
113 // Return formatted string
114 return JsonValue().toStyledString();
115}
116
117// Generate Json::Value for this object
118Json::Value ChromaKey::JsonValue() const {
119
120 // Create root json object
121 Json::Value root = EffectBase::JsonValue(); // get parent properties
122 root["type"] = info.class_name;
123 root["color"] = color.JsonValue();
124 root["fuzz"] = fuzz.JsonValue();
125
126 // return JsonValue
127 return root;
128}
129
130// Load JSON string into this object
131void ChromaKey::SetJson(const std::string value) {
132
133 // Parse JSON string into JSON objects
134 try
135 {
136 const Json::Value root = openshot::stringToJson(value);
137 // Set all values that match
138 SetJsonValue(root);
139 }
140 catch (const std::exception& e)
141 {
142 // Error parsing JSON (or missing keys)
143 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
144 }
145}
146
147// Load Json::Value into this object
148void ChromaKey::SetJsonValue(const Json::Value root) {
149
150 // Set parent data
152
153 // Set data from Json (if key is found)
154 if (!root["color"].isNull())
155 color.SetJsonValue(root["color"]);
156 if (!root["fuzz"].isNull())
157 fuzz.SetJsonValue(root["fuzz"]);
158}
159
160// Get all properties for a specific frame
161std::string ChromaKey::PropertiesJSON(int64_t requested_frame) const {
162
163 // Generate JSON properties list
164 Json::Value root;
165 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
166 root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
167 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
168 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
169 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
170 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
171
172 // Keyframes
173 root["color"] = add_property_json("Key Color", 0.0, "color", "", &color.red, 0, 255, false, requested_frame);
174 root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
175 root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
176 root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
177 root["fuzz"] = add_property_json("Fuzz", fuzz.GetValue(requested_frame), "float", "", &fuzz, 0, 125, false, requested_frame);
178
179 // Set the parent effect which properties this effect will inherit
180 root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
181
182 // Return formatted string
183 return root.toStyledString();
184}
Header file for ChromaKey class.
Header file for all Exception classes.
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: ChromaKey.cpp:131
ChromaKey()
Blank constructor, useful when using Json to load the effect properties.
Definition: ChromaKey.cpp:37
std::string Json() const override
Generate JSON string of this object.
Definition: ChromaKey.cpp:111
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: ChromaKey.cpp:161
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: ChromaKey.cpp:118
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition: ChromaKey.h:81
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: ChromaKey.cpp:148
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition: ClipBase.cpp:68
This class represents a color (used on the timeline and clips)
Definition: Color.h:45
openshot::Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:50
openshot::Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:48
openshot::Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:49
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Color.cpp:138
static long GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
Get the distance between 2 RGB pairs. (0=identical colors, 10=very close colors, 760=very different c...
Definition: Color.cpp:90
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:107
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87
Exception for invalid JSON.
Definition: Exceptions.h:206
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:72
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:292
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56