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