Loading...
Searching...
No Matches
EffectBase.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <iostream>
#include <iomanip>
#include "EffectBase.h"
#include "Exceptions.h"
#include "Timeline.h"
using namespace openshot;
// Initialize the values of the EffectInfo struct
void EffectBase::InitEffectInfo()
{
// Init clip settings
Position(0.0);
Layer(0);
Start(0.0);
End(0.0);
Order(0);
ParentClip(NULL);
parentEffect = NULL;
info.has_video = false;
info.has_audio = false;
info.has_tracked_object = false;
info.name = "";
info.description = "";
info.parent_effect_id = "";
}
// Display file information
void EffectBase::DisplayInfo(std::ostream* out) {
*out << std::fixed << std::setprecision(2) << std::boolalpha;
*out << "----------------------------" << std::endl;
*out << "----- Effect Information -----" << std::endl;
*out << "----------------------------" << std::endl;
*out << "--> Name: " << info.name << std::endl;
*out << "--> Description: " << info.description << std::endl;
*out << "--> Has Video: " << info.has_video << std::endl;
*out << "--> Has Audio: " << info.has_audio << std::endl;
*out << "----------------------------" << std::endl;
}
// Constrain a color value from 0 to 255
int EffectBase::constrain(int color_value)
{
// Constrain new color from 0 to 255
if (color_value < 0)
color_value = 0;
else if (color_value > 255)
color_value = 255;
return color_value;
}
// Generate JSON string of this object
std::string EffectBase::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value EffectBase::JsonValue() const {
// Create root json object
Json::Value root = ClipBase::JsonValue(); // get parent properties
root["name"] = info.name;
root["class_name"] = info.class_name;
root["description"] = info.description;
root["parent_effect_id"] = info.parent_effect_id;
root["has_video"] = info.has_video;
root["has_audio"] = info.has_audio;
root["has_tracked_object"] = info.has_tracked_object;
root["order"] = Order();
// return JsonValue
return root;
}
// Load JSON string into this object
void EffectBase::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void EffectBase::SetJsonValue(const Json::Value root) {
if (ParentTimeline()){
// Get parent timeline
Timeline* parentTimeline = static_cast<Timeline *>(ParentTimeline());
// Get the list of effects on the timeline
std::list<EffectBase*> effects = parentTimeline->ClipEffects();
// TODO: Fix recursive call for Object Detection
// // Loop through the effects and check if we have a child effect linked to this effect
for (auto const& effect : effects){
// Set the properties of all effects which parentEffect points to this
if ((effect->info.parent_effect_id == this->Id()) && (effect->Id() != this->Id()))
effect->SetJsonValue(root);
}
}
// Set this effect properties with the parent effect properties (except the id and parent_effect_id)
Json::Value my_root;
if (parentEffect){
my_root = parentEffect->JsonValue();
my_root["id"] = this->Id();
my_root["parent_effect_id"] = this->info.parent_effect_id;
} else {
my_root = root;
}
// Set parent data
ClipBase::SetJsonValue(my_root);
// Set data from Json (if key is found)
if (!my_root["order"].isNull())
Order(my_root["order"].asInt());
if (!my_root["parent_effect_id"].isNull()){
info.parent_effect_id = my_root["parent_effect_id"].asString();
if (info.parent_effect_id.size() > 0 && info.parent_effect_id != "" && parentEffect == NULL)
SetParentEffect(info.parent_effect_id);
else
parentEffect = NULL;
}
}
// Generate Json::Value for this object
Json::Value EffectBase::JsonInfo() const {
// Create root json object
Json::Value root;
root["name"] = info.name;
root["class_name"] = info.class_name;
root["description"] = info.description;
root["has_video"] = info.has_video;
root["has_audio"] = info.has_audio;
// return JsonValue
return root;
}
openshot::ClipBase* EffectBase::ParentClip() {
return clip;
}
void EffectBase::ParentClip(openshot::ClipBase* new_clip) {
clip = new_clip;
}
// Set the parent effect from which this properties will be set to
void EffectBase::SetParentEffect(std::string parentEffect_id) {
// Get parent Timeline
Timeline* parentTimeline = static_cast<Timeline *>(ParentTimeline());
if (parentTimeline){
// Get a pointer to the parentEffect
EffectBase* parentEffectPtr = parentTimeline->GetClipEffect(parentEffect_id);
if (parentEffectPtr){
// Set the parent Effect
parentEffect = parentEffectPtr;
// Set the properties of this effect with the parent effect's properties
Json::Value EffectJSON = parentEffect->JsonValue();
EffectJSON["id"] = this->Id();
EffectJSON["parent_effect_id"] = this->info.parent_effect_id;
this->SetJsonValue(EffectJSON);
}
}
return;
}
// Return the ID of this effect's parent clip
std::string EffectBase::ParentClipId() const{
if(clip)
return clip->Id();
else
return "";
}
EffectBase.h
Header file for EffectBase class.
Exceptions.h
Header file for all Exception classes.
Timeline.h
Header file for Timeline class.
openshot::ClipBase
This abstract class is the base class, used by all clips in libopenshot.
Definition ClipBase.h:33
openshot::ClipBase::Start
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition ClipBase.h:88
openshot::ClipBase::End
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition ClipBase.h:89
openshot::ClipBase::Id
std::string Id() const
Get the Id of this clip object.
Definition ClipBase.h:85
openshot::ClipBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition ClipBase.cpp:64
openshot::ClipBase::Layer
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition ClipBase.h:87
openshot::ClipBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition ClipBase.cpp:80
openshot::ClipBase::Position
float Position() const
Get position on timeline (in seconds)
Definition ClipBase.h:86
openshot::ClipBase::ParentTimeline
virtual openshot::TimelineBase * ParentTimeline()
Get the associated Timeline pointer (if any)
Definition ClipBase.h:91
openshot::EffectBase
This abstract class is the base class, used by all effects in libopenshot.
Definition EffectBase.h:53
openshot::EffectBase::SetJson
virtual void SetJson(const std::string value)
Load JSON string into this object.
Definition EffectBase.cpp:95
openshot::EffectBase::parentEffect
EffectBase * parentEffect
Parent effect (which properties will set this effect properties)
Definition EffectBase.h:63
openshot::EffectBase::JsonInfo
Json::Value JsonInfo() const
Generate JSON object of meta data / info.
Definition EffectBase.cpp:158
openshot::EffectBase::ParentClipId
std::string ParentClipId() const
Return the ID of this effect's parent clip.
Definition EffectBase.cpp:208
openshot::EffectBase::SetParentEffect
void SetParentEffect(std::string parentEffect_id)
Set the parent effect from which this properties will be set to.
Definition EffectBase.cpp:183
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:77
openshot::EffectBase::ParentClip
openshot::ClipBase * ParentClip()
Parent clip object of this effect (which can be unparented and NULL)
Definition EffectBase.cpp:173
openshot::EffectBase::constrain
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition EffectBase.cpp:58
openshot::EffectBase::Json
virtual std::string Json() const
Generate JSON string of this object.
Definition EffectBase.cpp:70
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:112
openshot::EffectBase::Order
int Order() const
Get the order that this effect should be executed.
Definition EffectBase.h:112
openshot::EffectBase::clip
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition EffectBase.h:58
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
openshot::EffectBase::DisplayInfo
void DisplayInfo(std::ostream *out=&std::cout)
Display effect information in the standard output stream (stdout)
Definition EffectBase.cpp:45
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::Timeline
This class represents a timeline.
Definition Timeline.h:150
openshot::Timeline::GetClipEffect
openshot::EffectBase * GetClipEffect(const std::string &id)
Look up a clip effect by ID.
Definition Timeline.cpp:431
openshot::Timeline::ClipEffects
std::list< openshot::EffectBase * > ClipEffects() const
Return the list of effects on all clips.
Definition Timeline.cpp:444
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
openshot::EffectInfoStruct::parent_effect_id
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition EffectBase.h:39
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition EffectBase.h:37
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38
openshot::EffectInfoStruct::has_tracked_object
bool has_tracked_object
Determines if this effect track objects through the clip.
Definition EffectBase.h:42