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