OpenShot Library | libopenshot  0.2.4
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() {
78 
79  // Return formatted string
80  return JsonValue().toStyledString();
81 }
82 
83 // Generate Json::JsonValue for this object
84 Json::Value EffectBase::JsonValue() {
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(std::string value) {
101 
102  // Parse JSON string into JSON objects
103  Json::Value root;
104  Json::CharReaderBuilder rbuilder;
105  Json::CharReader* reader(rbuilder.newCharReader());
106 
107  std::string errors;
108  bool success = reader->parse( value.c_str(),
109  value.c_str() + value.size(), &root, &errors );
110  delete reader;
111 
112  if (!success)
113  // Raise exception
114  throw InvalidJSON("JSON could not be parsed (or is invalid)");
115 
116  try
117  {
118  // Set all values that match
119  SetJsonValue(root);
120  }
121  catch (const std::exception& e)
122  {
123  // Error parsing JSON (or missing keys)
124  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
125  }
126 }
127 
128 // Load Json::JsonValue into this object
129 void EffectBase::SetJsonValue(Json::Value root) {
130 
131  // Set parent data
133 
134  // Set data from Json (if key is found)
135  if (!root["order"].isNull())
136  Order(root["order"].asInt());
137 }
138 
139 // Generate Json::JsonValue for this object
140 Json::Value EffectBase::JsonInfo() {
141 
142  // Create root json object
143  Json::Value root;
144  root["name"] = info.name;
145  root["class_name"] = info.class_name;
146  root["description"] = info.description;
147  root["has_video"] = info.has_video;
148  root["has_audio"] = info.has_audio;
149 
150  // return JsonValue
151  return root;
152 }
void DisplayInfo()
Display effect information in the standard output stream (stdout)
Definition: EffectBase.cpp:52
float End()
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
Json::Value JsonInfo()
Generate JSON object of meta data / info.
Definition: EffectBase.cpp:140
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:84
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ClipBase.cpp:52
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:129
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
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
virtual void SetJson(std::string value)=0
Load JSON string into this object.
Definition: EffectBase.cpp:100
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ClipBase.cpp:36
int Order()
Get the order that this effect should be executed.
Definition: EffectBase.h:104
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
virtual std::string Json()=0
Get and Set JSON methods.
Definition: EffectBase.cpp:77