OpenShot Library | libopenshot  0.2.5
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 "../../include/effects/ChromaKey.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 ChromaKey::ChromaKey() : fuzz(5.0) {
37  // Init default color
38  color = Color();
39 
40  // Init effect properties
41  init_effect_details();
42 }
43 
44 // Default constructor, which takes an openshot::Color object and a 'fuzz' factor, which
45 // is used to determine how similar colored pixels are matched. The higher the fuzz, the
46 // more colors are matched.
47 ChromaKey::ChromaKey(Color color, Keyframe fuzz) : color(color), fuzz(fuzz)
48 {
49  // Init effect properties
50  init_effect_details();
51 }
52 
53 // Init effect settings
54 void ChromaKey::init_effect_details()
55 {
56  /// Initialize the values of the EffectInfo struct.
58 
59  /// Set the effect info
60  info.class_name = "ChromaKey";
61  info.name = "Chroma Key (Greenscreen)";
62  info.description = "Replaces the color (or chroma) of the frame with transparency (i.e. keys out the color).";
63  info.has_audio = false;
64  info.has_video = true;
65 }
66 
67 // This method is required for all derived classes of EffectBase, and returns a
68 // modified openshot::Frame object
69 std::shared_ptr<Frame> ChromaKey::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
70 {
71  // Determine the current HSL (Hue, Saturation, Lightness) for the Chrome
72  int threshold = fuzz.GetInt(frame_number);
73  long mask_R = color.red.GetInt(frame_number);
74  long mask_G = color.green.GetInt(frame_number);
75  long mask_B = color.blue.GetInt(frame_number);
76 
77  // Get source image's pixels
78  std::shared_ptr<QImage> image = frame->GetImage();
79  unsigned char *pixels = (unsigned char *) image->bits();
80 
81  // Loop through pixels
82  for (int pixel = 0, byte_index=0; pixel < image->width() * image->height(); pixel++, byte_index+=4)
83  {
84  // Get the RGB values from the pixel
85  unsigned char R = pixels[byte_index];
86  unsigned char G = pixels[byte_index + 1];
87  unsigned char B = pixels[byte_index + 2];
88  unsigned char A = pixels[byte_index + 3];
89 
90  // Get distance between mask color and pixel color
91  long distance = Color::GetDistance((long)R, (long)G, (long)B, mask_R, mask_G, mask_B);
92 
93  // Alpha out the pixel (if color similar)
94  if (distance <= threshold)
95  // MATCHED - Make pixel transparent
96  pixels[byte_index + 3] = 0;
97  }
98 
99  // return the modified frame
100  return frame;
101 }
102 
103 // Generate JSON string of this object
104 std::string ChromaKey::Json() const {
105 
106  // Return formatted string
107  return JsonValue().toStyledString();
108 }
109 
110 // Generate Json::Value for this object
111 Json::Value ChromaKey::JsonValue() const {
112 
113  // Create root json object
114  Json::Value root = EffectBase::JsonValue(); // get parent properties
115  root["type"] = info.class_name;
116  root["color"] = color.JsonValue();
117  root["fuzz"] = fuzz.JsonValue();
118 
119  // return JsonValue
120  return root;
121 }
122 
123 // Load JSON string into this object
124 void ChromaKey::SetJson(const std::string value) {
125 
126  // Parse JSON string into JSON objects
127  try
128  {
129  const Json::Value root = openshot::stringToJson(value);
130  // Set all values that match
131  SetJsonValue(root);
132  }
133  catch (const std::exception& e)
134  {
135  // Error parsing JSON (or missing keys)
136  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
137  }
138 }
139 
140 // Load Json::Value into this object
141 void ChromaKey::SetJsonValue(const Json::Value root) {
142 
143  // Set parent data
145 
146  // Set data from Json (if key is found)
147  if (!root["color"].isNull())
148  color.SetJsonValue(root["color"]);
149  if (!root["fuzz"].isNull())
150  fuzz.SetJsonValue(root["fuzz"]);
151 }
152 
153 // Get all properties for a specific frame
154 std::string ChromaKey::PropertiesJSON(int64_t requested_frame) const {
155 
156  // Generate JSON properties list
157  Json::Value root;
158  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
159  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
160  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
161  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
162  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
163  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
164 
165  // Keyframes
166  root["color"] = add_property_json("Key Color", 0.0, "color", "", NULL, 0, 255, false, requested_frame);
167  root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
168  root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
169  root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
170  root["fuzz"] = add_property_json("Fuzz", fuzz.GetValue(requested_frame), "float", "", &fuzz, 0, 25, false, requested_frame);
171 
172  // Return formatted string
173  return root.toStyledString();
174 }
ChromaKey()
Blank constructor, useful when using Json to load the effect properties.
Definition: ChromaKey.cpp:36
std::string Json() const override
Get and Set JSON methods.
Definition: ChromaKey.cpp:104
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number)
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Definition: ChromaKey.cpp:69
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: ChromaKey.cpp:154
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: ChromaKey.cpp:111
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: ChromaKey.cpp:141
void SetJson(const std::string value)
Load JSON string into this object.
Definition: ChromaKey.cpp:124
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
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:126
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:78
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:95
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
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:64
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:286
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
std::string name
The name of the effect.
Definition: EffectBase.h:53
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54