OpenShot Library | libopenshot  0.3.2
Bars.cpp
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 #include "Bars.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
19 Bars::Bars() : color("#000000"), left(0.0), top(0.1), right(0.0), bottom(0.1) {
20  // Init effect properties
21  init_effect_details();
22 }
23 
24 // Default constructor
25 Bars::Bars(Color color, Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
26  color(color), left(left), top(top), right(right), bottom(bottom)
27 {
28  // Init effect properties
29  init_effect_details();
30 }
31 
32 // Init effect settings
33 void Bars::init_effect_details()
34 {
37 
39  info.class_name = "Bars";
40  info.name = "Bars";
41  info.description = "Add colored bars around your video.";
42  info.has_audio = false;
43  info.has_video = true;
44 }
45 
46 // This method is required for all derived classes of EffectBase, and returns a
47 // modified openshot::Frame object
48 std::shared_ptr<openshot::Frame> Bars::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
49 {
50  // Get the frame's image
51  std::shared_ptr<QImage> frame_image = frame->GetImage();
52 
53  // Get bar color (and create small color image)
54  auto tempColor = std::make_shared<QImage>(
55  frame_image->width(), 1, QImage::Format_RGBA8888_Premultiplied);
56  tempColor->fill(QColor(QString::fromStdString(color.GetColorHex(frame_number))));
57 
58  // Get current keyframe values
59  double left_value = left.GetValue(frame_number);
60  double top_value = top.GetValue(frame_number);
61  double right_value = right.GetValue(frame_number);
62  double bottom_value = bottom.GetValue(frame_number);
63 
64  // Get pixel array pointer
65  unsigned char *pixels = (unsigned char *) frame_image->bits();
66  unsigned char *color_pixels = (unsigned char *) tempColor->bits();
67 
68  // Get pixels sizes of all bars
69  int top_bar_height = top_value * frame_image->height();
70  int bottom_bar_height = bottom_value * frame_image->height();
71  int left_bar_width = left_value * frame_image->width();
72  int right_bar_width = right_value * frame_image->width();
73 
74  // Loop through rows
75  for (int row = 0; row < frame_image->height(); row++) {
76 
77  // Top & Bottom Bar
78  if ((top_bar_height > 0.0 && row <= top_bar_height) || (bottom_bar_height > 0.0 && row >= frame_image->height() - bottom_bar_height)) {
79  memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * frame_image->width() * 4);
80  } else {
81  // Left Bar
82  if (left_bar_width > 0.0) {
83  memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * left_bar_width * 4);
84  }
85 
86  // Right Bar
87  if (right_bar_width > 0.0) {
88  memcpy(&pixels[((row * frame_image->width()) + (frame_image->width() - right_bar_width)) * 4], color_pixels, sizeof(char) * right_bar_width * 4);
89  }
90  }
91  }
92 
93  // Cleanup colors and arrays
94  tempColor.reset();
95 
96  // return the modified frame
97  return frame;
98 }
99 
100 // Generate JSON string of this object
101 std::string Bars::Json() const {
102 
103  // Return formatted string
104  return JsonValue().toStyledString();
105 }
106 
107 // Generate Json::Value for this object
108 Json::Value Bars::JsonValue() const {
109 
110  // Create root json object
111  Json::Value root = EffectBase::JsonValue(); // get parent properties
112  root["type"] = info.class_name;
113  root["color"] = color.JsonValue();
114  root["left"] = left.JsonValue();
115  root["top"] = top.JsonValue();
116  root["right"] = right.JsonValue();
117  root["bottom"] = bottom.JsonValue();
118 
119  // return JsonValue
120  return root;
121 }
122 
123 // Load JSON string into this object
124 void Bars::SetJson(const std::string value) {
125 
126  // Parse JSON string into JSON objects
127  try
128  {
129  const Json::Value root = openshot::stringToJson(value);
130  // Set all values that match
131  SetJsonValue(root);
132  }
133  catch (const std::exception& e)
134  {
135  // Error parsing JSON (or missing keys)
136  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
137  }
138 }
139 
140 // Load Json::Value into this object
141 void Bars::SetJsonValue(const Json::Value root) {
142 
143  // Set parent data
145 
146  // Set data from Json (if key is found)
147  if (!root["color"].isNull())
148  color.SetJsonValue(root["color"]);
149  if (!root["left"].isNull())
150  left.SetJsonValue(root["left"]);
151  if (!root["top"].isNull())
152  top.SetJsonValue(root["top"]);
153  if (!root["right"].isNull())
154  right.SetJsonValue(root["right"]);
155  if (!root["bottom"].isNull())
156  bottom.SetJsonValue(root["bottom"]);
157 }
158 
159 // Get all properties for a specific frame
160 std::string Bars::PropertiesJSON(int64_t requested_frame) const {
161 
162  // Generate JSON properties list
163  Json::Value root = BasePropertiesJSON(requested_frame);
164 
165  // Keyframes
166  root["color"] = add_property_json("Bar Color", 0.0, "color", "", &color.red, 0, 255, false, requested_frame);
167  root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
168  root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
169  root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
170  root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 0.5, false, requested_frame);
171  root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 0.5, false, requested_frame);
172  root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 0.5, false, requested_frame);
173  root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 0.5, false, requested_frame);
174 
175  // Return formatted string
176  return root.toStyledString();
177 }
openshot::ClipBase::add_property_json
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition: ClipBase.cpp:96
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Bars::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Bars.cpp:141
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Bars::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Bars.cpp:101
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Bars::left
Keyframe left
Size of left bar.
Definition: Bars.h:45
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::Color
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
openshot::Bars::Bars
Bars()
Blank constructor, useful when using Json to load the effect properties.
Definition: Bars.cpp:19
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:179
Bars.h
Header file for Bars effect class.
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::Bars::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Bars.cpp:124
openshot::Color::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Color.cpp:117
openshot::Bars::bottom
Keyframe bottom
Size of bottom bar.
Definition: Bars.h:48
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::Color::green
openshot::Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:31
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
openshot::Bars::right
Keyframe right
Size of right bar.
Definition: Bars.h:47
openshot::Bars::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition: Bars.h:68
openshot::Bars::color
Color color
Color of bars.
Definition: Bars.h:44
openshot::Bars::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Bars.cpp:160
openshot::Bars::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Bars.cpp:108
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::Color::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:86
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Bars::top
Keyframe top
Size of top bar.
Definition: Bars.h:46
openshot::Color::red
openshot::Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:30
openshot::Color::GetColorHex
std::string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition: Color.cpp:47
openshot::Color::blue
openshot::Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:32
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258