OpenShot Library | libopenshot  0.2.5
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 "../include/EffectBase.h"
32 
33 using namespace openshot;
34 
35 // Initialize the values of the EffectInfo struct
37 {
38  // Init clip settings
39  Position(0.0);
40  Layer(0);
41  Start(0.0);
42  End(0.0);
43  Order(0);
44 
45  info.has_video = false;
46  info.has_audio = false;
47  info.name = "";
48  info.description = "";
49 }
50 
51 // Display file information
53  std::cout << std::fixed << std::setprecision(2) << std::boolalpha;
54  std::cout << "----------------------------" << std::endl;
55  std::cout << "----- Effect Information -----" << std::endl;
56  std::cout << "----------------------------" << std::endl;
57  std::cout << "--> Name: " << info.name << std::endl;
58  std::cout << "--> Description: " << info.description << std::endl;
59  std::cout << "--> Has Video: " << info.has_video << std::endl;
60  std::cout << "--> Has Audio: " << info.has_audio << std::endl;
61  std::cout << "----------------------------" << std::endl;
62 }
63 
64 // Constrain a color value from 0 to 255
65 int EffectBase::constrain(int color_value)
66 {
67  // Constrain new color from 0 to 255
68  if (color_value < 0)
69  color_value = 0;
70  else if (color_value > 255)
71  color_value = 255;
72 
73  return color_value;
74 }
75 
76 // Generate JSON string of this object
77 std::string EffectBase::Json() const {
78 
79  // Return formatted string
80  return JsonValue().toStyledString();
81 }
82 
83 // Generate Json::Value for this object
84 Json::Value EffectBase::JsonValue() const {
85 
86  // Create root json object
87  Json::Value root = ClipBase::JsonValue(); // get parent properties
88  root["name"] = info.name;
89  root["class_name"] = info.class_name;
90  root["description"] = info.description;
91  root["has_video"] = info.has_video;
92  root["has_audio"] = info.has_audio;
93  root["order"] = Order();
94 
95  // return JsonValue
96  return root;
97 }
98 
99 // Load JSON string into this object
100 void EffectBase::SetJson(const std::string value) {
101 
102  // Parse JSON string into JSON objects
103  try
104  {
105  const Json::Value root = openshot::stringToJson(value);
106  // Set all values that match
107  SetJsonValue(root);
108  }
109  catch (const std::exception& e)
110  {
111  // Error parsing JSON (or missing keys)
112  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
113  }
114 }
115 
116 // Load Json::Value into this object
117 void EffectBase::SetJsonValue(const Json::Value root) {
118 
119  // Set parent data
121 
122  // Set data from Json (if key is found)
123  if (!root["order"].isNull())
124  Order(root["order"].asInt());
125 }
126 
127 // Generate Json::Value for this object
128 Json::Value EffectBase::JsonInfo() const {
129 
130  // Create root json object
131  Json::Value root;
132  root["name"] = info.name;
133  root["class_name"] = info.class_name;
134  root["description"] = info.description;
135  root["has_video"] = info.has_video;
136  root["has_audio"] = info.has_audio;
137 
138  // return JsonValue
139  return root;
140 }
virtual std::string Json() const =0
Get and Set JSON methods.
Definition: EffectBase.cpp:77
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
void DisplayInfo()
Display effect information in the standard output stream (stdout)
Definition: EffectBase.cpp:52
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
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:128
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:56
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
virtual void SetJson(const std::string value)=0
Load JSON string into this object.
Definition: EffectBase.cpp:100
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
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:104
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
This namespace is the default namespace for all code in the openshot library.
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
Exception for invalid JSON.
Definition: Exceptions.h:205
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:65
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73