OpenShot Library | libopenshot  0.7.0
EffectBase.h
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 #ifndef OPENSHOT_EFFECT_BASE_H
14 #define OPENSHOT_EFFECT_BASE_H
15 
16 #include "ClipBase.h"
17 
18 #include "Json.h"
19 #include "TrackedObjectBase.h"
20 
21 #include <QImage>
22 #include <memory>
23 #include <map>
24 #include <string>
25 
26 namespace openshot
27 {
28  class ReaderBase;
29 
38  {
39  std::string class_name;
40  std::string name;
41  std::string description;
42  std::string parent_effect_id;
43  bool has_video;
44  bool has_audio;
47  };
48 
56  class EffectBase : public ClipBase
57  {
58  private:
59  int order;
60  ReaderBase* mask_reader = nullptr;
61  std::string mask_source_id;
62  std::shared_ptr<QImage> cached_single_mask_image;
63  int cached_single_mask_width = 0;
64  int cached_single_mask_height = 0;
65 
67  std::shared_ptr<QImage> GetMaskImage(std::shared_ptr<QImage> target_image, int64_t frame_number);
68 
70  void BlendWithMask(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
71  std::shared_ptr<QImage> mask_image) const;
72 
74  EffectBase* ResolveMaskSourceEffect();
75 
76  protected:
78 
80  ReaderBase* CreateReaderFromJson(const Json::Value& reader_json) const;
81 
83  int64_t MapMaskFrameNumber(int64_t frame_number);
84 
86  double ResolveMaskHostFps();
87 
89  double ResolveMaskSourceDuration() const;
90 
92  std::shared_ptr<QImage> ResolveMaskImage(std::shared_ptr<QImage> target_image, int64_t frame_number) {
93  return GetMaskImage(target_image, frame_number);
94  }
95 
97  virtual bool UseCustomMaskBlend(int64_t frame_number) const { return false; }
98 
100  virtual void ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
101  std::shared_ptr<QImage> mask_image, int64_t frame_number) const {}
102 
104  virtual bool HandlesMaskInternally() const { return false; }
105 
106  public:
109 
111  std::map<int, std::shared_ptr<openshot::TrackedObjectBase> > trackedObjects;
112 
115  bool mask_invert = false;
116 
120  };
121 
126  };
127 
130 
132  void DisplayInfo(std::ostream* out=&std::cout);
133 
135  int constrain(int color_value);
136 
139  void InitEffectInfo();
140 
143 
145  void ParentClip(openshot::ClipBase* new_clip);
146 
148  void SetParentEffect(std::string parentEffect_id);
149 
151  std::string ParentClipId() const;
152 
154  virtual std::string GetVisibleObjects(int64_t frame_number) const {return {}; };
155 
157  virtual std::shared_ptr<QImage> TrackedObjectMask(std::shared_ptr<QImage> target_image, int64_t frame_number) const;
158 
159  // Get and Set JSON methods
160  virtual std::string Json() const;
161  virtual void SetJson(const std::string value);
162  virtual Json::Value JsonValue() const;
163  virtual void SetJsonValue(const Json::Value root);
164 
165  virtual std::string Json(int64_t requested_frame) const{
166  return "";
167  };
168  virtual void SetJson(int64_t requested_frame, const std::string value) {
169  return;
170  };
171 
173  Json::Value JsonInfo() const;
174 
176  Json::Value BasePropertiesJSON(int64_t requested_frame) const;
177 
179  std::shared_ptr<openshot::Frame> ProcessFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number);
180 
182  ReaderBase* MaskReader() { return mask_reader; }
183  const ReaderBase* MaskReader() const { return mask_reader; }
184 
186  void MaskReader(ReaderBase* new_reader);
187 
189  std::string MaskSourceId() const { return mask_source_id; }
190  void MaskSourceId(const std::string& new_mask_source_id);
191 
193  int Order() const { return order; }
194 
196  void Order(int new_order) { order = new_order; }
197 
198  virtual ~EffectBase();
199  };
200 
201 }
202 
203 #endif
openshot::EffectBase::~EffectBase
virtual ~EffectBase()
Definition: EffectBase.cpp:698
openshot::EffectBase
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:56
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:114
openshot::EffectBase::JsonInfo
Json::Value JsonInfo() const
Generate JSON object of meta data / info.
Definition: EffectBase.cpp:230
openshot::EffectInfoStruct::apply_before_clip
bool apply_before_clip
Apply effect to source before we evaluate the clip's keyframes.
Definition: EffectBase.h:46
openshot::EffectBase::mask_invert
bool mask_invert
Invert grayscale mask values before blending.
Definition: EffectBase.h:115
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::EffectBase::ParentClip
openshot::ClipBase * ParentClip()
Parent clip object of this effect (which can be unparented and NULL)
Definition: EffectBase.cpp:654
openshot::EffectBase::MaskSourceId
std::string MaskSourceId() const
Get/Set effect ID used as a generated mask source.
Definition: EffectBase.h:189
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:102
openshot::EffectBase::mask_time_mode
int mask_time_mode
How effect frames map to mask source frames.
Definition: EffectBase.h:128
openshot::EffectInfoStruct
This struct contains info about an effect, such as the name, video or audio effect,...
Definition: EffectBase.h:37
openshot::EffectBase::DisplayInfo
void DisplayInfo(std::ostream *out=&std::cout)
Display effect information in the standard output stream (stdout)
Definition: EffectBase.cpp:68
openshot::EffectBase::CreateReaderFromJson
ReaderBase * CreateReaderFromJson(const Json::Value &reader_json) const
Create a reader instance from reader JSON.
Definition: EffectBase.cpp:288
openshot::EffectBase::trackedObjects
std::map< int, std::shared_ptr< openshot::TrackedObjectBase > > trackedObjects
Map of Tracked Object's by their indices (used by Effects that track objects on clips)
Definition: EffectBase.h:111
openshot::EffectBase::Json
virtual std::string Json() const
Generate JSON string of this object.
Definition: EffectBase.cpp:95
openshot::EffectBase::MaskTimeMode
MaskTimeMode
Definition: EffectBase.h:117
openshot::EffectBase::UseCustomMaskBlend
virtual bool UseCustomMaskBlend(int64_t frame_number) const
Optional override for effects that need custom mask behavior.
Definition: EffectBase.h:97
openshot::EffectBase::ResolveMaskImage
std::shared_ptr< QImage > ResolveMaskImage(std::shared_ptr< QImage > target_image, int64_t frame_number)
Resolve a cached/scaled mask image for the target frame dimensions.
Definition: EffectBase.h:92
openshot::EffectBase::SetParentEffect
void SetParentEffect(std::string parentEffect_id)
Set the parent effect from which this properties will be set to.
Definition: EffectBase.cpp:666
openshot::EffectBase::MaskReader
const ReaderBase * MaskReader() const
Definition: EffectBase.h:183
openshot::EffectBase::MASK_TIME_TIMELINE
@ MASK_TIME_TIMELINE
Definition: EffectBase.h:118
openshot::EffectBase::ProcessFrame
std::shared_ptr< openshot::Frame > ProcessFrame(std::shared_ptr< openshot::Frame > frame, int64_t frame_number)
Apply effect processing with common mask support (if enabled).
Definition: EffectBase.cpp:619
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
Definition: EffectBase.cpp:245
openshot::EffectBase::Json
virtual std::string Json(int64_t requested_frame) const
Definition: EffectBase.h:165
openshot::EffectBase::MASK_LOOP_PLAY_ONCE
@ MASK_LOOP_PLAY_ONCE
Definition: EffectBase.h:123
openshot::EffectBase::parentEffect
EffectBase * parentEffect
Parent effect (which properties will set this effect properties)
Definition: EffectBase.h:108
openshot::EffectBase::Order
void Order(int new_order)
Set the order that this effect should be executed.
Definition: EffectBase.h:196
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:42
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:44
openshot::EffectInfoStruct::has_tracked_object
bool has_tracked_object
Determines if this effect track objects through the clip.
Definition: EffectBase.h:45
openshot::EffectBase::ResolveMaskSourceDuration
double ResolveMaskSourceDuration() const
Determine mask source duration in seconds.
Definition: EffectBase.cpp:383
openshot::EffectBase::TrackedObjectMask
virtual std::shared_ptr< QImage > TrackedObjectMask(std::shared_ptr< QImage > target_image, int64_t frame_number) const
Generate a black/white mask from tracked object data.
Definition: EffectBase.cpp:538
openshot::EffectBase::MaskReader
ReaderBase * MaskReader()
Get the common mask reader.
Definition: EffectBase.h:182
openshot::EffectBase::ParentClipId
std::string ParentClipId() const
Return the ID of this effect's parent clip.
Definition: EffectBase.cpp:691
openshot::EffectBase::ResolveMaskHostFps
double ResolveMaskHostFps()
Determine host FPS used to convert timeline frames to mask source FPS.
Definition: EffectBase.cpp:366
openshot::EffectBase::Order
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:193
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:39
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:41
openshot::EffectBase::MaskLoopMode
MaskLoopMode
Definition: EffectBase.h:122
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:43
openshot::EffectBase::MASK_LOOP_REPEAT
@ MASK_LOOP_REPEAT
Definition: EffectBase.h:124
openshot::EffectBase::MASK_LOOP_PING_PONG
@ MASK_LOOP_PING_PONG
Definition: EffectBase.h:125
openshot::EffectBase::constrain
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:83
openshot::EffectInfoStruct::parent_effect_id
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:42
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::EffectBase::mask_loop_mode
int mask_loop_mode
Behavior when mask range reaches the end.
Definition: EffectBase.h:129
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:40
openshot::EffectBase::MASK_TIME_SOURCE_FPS
@ MASK_TIME_SOURCE_FPS
Definition: EffectBase.h:119
openshot::EffectBase::SetJson
virtual void SetJson(int64_t requested_frame, const std::string value)
Definition: EffectBase.h:168
openshot::EffectBase::HandlesMaskInternally
virtual bool HandlesMaskInternally() const
Optional override for effects that apply mask processing inside GetFrame().
Definition: EffectBase.h:104
Json.h
Header file for JSON class.
openshot::EffectBase::MapMaskFrameNumber
int64_t MapMaskFrameNumber(int64_t frame_number)
Convert an effect frame number to a mask source frame number.
Definition: EffectBase.cpp:398
openshot::ClipBase
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:32
openshot::EffectBase::SetJson
virtual void SetJson(const std::string value)
Load JSON string into this object.
Definition: EffectBase.cpp:129
TrackedObjectBase.h
Header file for the TrackedObjectBase class.
ClipBase.h
Header file for ClipBase class.
openshot::EffectBase::ApplyCustomMaskBlend
virtual void ApplyCustomMaskBlend(std::shared_ptr< QImage > original_image, std::shared_ptr< QImage > effected_image, std::shared_ptr< QImage > mask_image, int64_t frame_number) const
Optional override for effects with custom mask implementation.
Definition: EffectBase.h:100
openshot::EffectBase::GetVisibleObjects
virtual std::string GetVisibleObjects(int64_t frame_number) const
Get the indexes and IDs of all visible objects in the given frame.
Definition: EffectBase.h:154
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:146
openshot::EffectBase::clip
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition: EffectBase.h:77