OpenShot Library | libopenshot  0.3.0
EffectBase.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include <iostream>
14 #include <iomanip>
15 
16 #include "EffectBase.h"
17 
18 #include "Exceptions.h"
19 #include "Timeline.h"
20 
21 using namespace openshot;
22 
23 // Initialize the values of the EffectInfo struct
25 {
26  // Init clip settings
27  Position(0.0);
28  Layer(0);
29  Start(0.0);
30  End(0.0);
31  Order(0);
32  ParentClip(NULL);
33 
34  parentEffect = NULL;
35 
36  info.has_video = false;
37  info.has_audio = false;
38  info.has_tracked_object = false;
39  info.name = "";
40  info.description = "";
42 }
43 
44 // Display file information
45 void EffectBase::DisplayInfo(std::ostream* out) {
46  *out << std::fixed << std::setprecision(2) << std::boolalpha;
47  *out << "----------------------------" << std::endl;
48  *out << "----- Effect Information -----" << std::endl;
49  *out << "----------------------------" << std::endl;
50  *out << "--> Name: " << info.name << std::endl;
51  *out << "--> Description: " << info.description << std::endl;
52  *out << "--> Has Video: " << info.has_video << std::endl;
53  *out << "--> Has Audio: " << info.has_audio << std::endl;
54  *out << "----------------------------" << std::endl;
55 }
56 
57 // Constrain a color value from 0 to 255
58 int EffectBase::constrain(int color_value)
59 {
60  // Constrain new color from 0 to 255
61  if (color_value < 0)
62  color_value = 0;
63  else if (color_value > 255)
64  color_value = 255;
65 
66  return color_value;
67 }
68 
69 // Generate JSON string of this object
70 std::string EffectBase::Json() const {
71 
72  // Return formatted string
73  return JsonValue().toStyledString();
74 }
75 
76 // Generate Json::Value for this object
77 Json::Value EffectBase::JsonValue() const {
78 
79  // Create root json object
80  Json::Value root = ClipBase::JsonValue(); // get parent properties
81  root["name"] = info.name;
82  root["class_name"] = info.class_name;
83  root["description"] = info.description;
84  root["parent_effect_id"] = info.parent_effect_id;
85  root["has_video"] = info.has_video;
86  root["has_audio"] = info.has_audio;
87  root["has_tracked_object"] = info.has_tracked_object;
88  root["order"] = Order();
89 
90  // return JsonValue
91  return root;
92 }
93 
94 // Load JSON string into this object
95 void EffectBase::SetJson(const std::string value) {
96 
97  // Parse JSON string into JSON objects
98  try
99  {
100  Json::Value root = openshot::stringToJson(value);
101  // Set all values that match
102  SetJsonValue(root);
103  }
104  catch (const std::exception& e)
105  {
106  // Error parsing JSON (or missing keys)
107  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
108  }
109 }
110 
111 // Load Json::Value into this object
112 void EffectBase::SetJsonValue(const Json::Value root) {
113 
114  if (ParentTimeline()){
115  // Get parent timeline
116  Timeline* parentTimeline = (Timeline *) ParentTimeline();
117 
118  // Get the list of effects on the timeline
119  std::list<EffectBase*> effects = parentTimeline->ClipEffects();
120 
121  // TODO: Fix recursive call for Object Detection
122 
123  // // Loop through the effects and check if we have a child effect linked to this effect
124  for (auto const& effect : effects){
125  // Set the properties of all effects which parentEffect points to this
126  if ((effect->info.parent_effect_id == this->Id()) && (effect->Id() != this->Id()))
127  effect->SetJsonValue(root);
128  }
129  }
130 
131  // Set this effect properties with the parent effect properties (except the id and parent_effect_id)
132  Json::Value my_root;
133  if (parentEffect){
134  my_root = parentEffect->JsonValue();
135  my_root["id"] = this->Id();
136  my_root["parent_effect_id"] = this->info.parent_effect_id;
137  } else {
138  my_root = root;
139  }
140 
141  // Set parent data
142  ClipBase::SetJsonValue(my_root);
143 
144  // Set data from Json (if key is found)
145  if (!my_root["order"].isNull())
146  Order(my_root["order"].asInt());
147 
148  if (!my_root["parent_effect_id"].isNull()){
149  info.parent_effect_id = my_root["parent_effect_id"].asString();
150  if (info.parent_effect_id.size() > 0 && info.parent_effect_id != "" && parentEffect == NULL)
152  else
153  parentEffect = NULL;
154  }
155 }
156 
157 // Generate Json::Value for this object
158 Json::Value EffectBase::JsonInfo() const {
159 
160  // Create root json object
161  Json::Value root;
162  root["name"] = info.name;
163  root["class_name"] = info.class_name;
164  root["description"] = info.description;
165  root["has_video"] = info.has_video;
166  root["has_audio"] = info.has_audio;
167 
168  // return JsonValue
169  return root;
170 }
171 
174  return clip;
175 }
176 
179  clip = new_clip;
180 }
181 
182 // Set the parent effect from which this properties will be set to
183 void EffectBase::SetParentEffect(std::string parentEffect_id) {
184 
185  // Get parent Timeline
186  Timeline* parentTimeline = (Timeline *) ParentTimeline();
187 
188  if (parentTimeline){
189 
190  // Get a pointer to the parentEffect
191  EffectBase* parentEffectPtr = parentTimeline->GetClipEffect(parentEffect_id);
192 
193  if (parentEffectPtr){
194  // Set the parent Effect
195  parentEffect = parentEffectPtr;
196 
197  // Set the properties of this effect with the parent effect's properties
198  Json::Value EffectJSON = parentEffect->JsonValue();
199  EffectJSON["id"] = this->Id();
200  EffectJSON["parent_effect_id"] = this->info.parent_effect_id;
201  this->SetJsonValue(EffectJSON);
202  }
203  }
204  return;
205 }
206 
207 // Return the ID of this effect's parent clip
208 std::string EffectBase::ParentClipId() const{
209  if(clip)
210  return clip->Id();
211  else
212  return "";
213 }
Header file for EffectBase class.
Header file for all Exception classes.
Header file for Timeline class.
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:33
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:88
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:89
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:85
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ClipBase.cpp:64
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
openshot::TimelineBase * ParentTimeline()
Get the associated Timeline pointer (if any)
Definition: ClipBase.h:91
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ClipBase.cpp:80
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:86
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:53
virtual void SetJson(const std::string value)
Load JSON string into this object.
Definition: EffectBase.cpp:95
EffectBase * parentEffect
Parent effect (which properties will set this effect properties)
Definition: EffectBase.h:63
Json::Value JsonInfo() const
Generate JSON object of meta data / info.
Definition: EffectBase.cpp:158
std::string ParentClipId() const
Return the ID of this effect's parent clip.
Definition: EffectBase.cpp:208
void SetParentEffect(std::string parentEffect_id)
Set the parent effect from which this properties will be set to.
Definition: EffectBase.cpp:183
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:77
openshot::ClipBase * ParentClip()
Parent clip object of this effect (which can be unparented and NULL)
Definition: EffectBase.cpp:173
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:58
virtual std::string Json() const
Generate JSON string of this object.
Definition: EffectBase.cpp:70
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:112
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:112
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition: EffectBase.h:58
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
void DisplayInfo(std::ostream *out=&std::cout)
Display effect information in the standard output stream (stdout)
Definition: EffectBase.cpp:45
Exception for invalid JSON.
Definition: Exceptions.h:218
This class represents a timeline.
Definition: Timeline.h:150
openshot::EffectBase * GetClipEffect(const std::string &id)
Look up a clip effect by ID.
Definition: Timeline.cpp:431
std::list< openshot::EffectBase * > ClipEffects() const
Return the list of effects on all clips.
Definition: Timeline.cpp:444
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:39
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
std::string name
The name of the effect.
Definition: EffectBase.h:37
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
bool has_tracked_object
Determines if this effect track objects through the clip.
Definition: EffectBase.h:42