OpenShot Library | libopenshot  0.2.5
Bars.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Bars effect 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/effects/Bars.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Bars::Bars() : color("#000000"), left(0.0), top(0.1), right(0.0), bottom(0.1) {
37  // Init effect properties
38  init_effect_details();
39 }
40 
41 // Default constructor
42 Bars::Bars(Color color, Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
43  color(color), left(left), top(top), right(right), bottom(bottom)
44 {
45  // Init effect properties
46  init_effect_details();
47 }
48 
49 // Init effect settings
50 void Bars::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Bars";
57  info.name = "Bars";
58  info.description = "Add colored bars around your video.";
59  info.has_audio = false;
60  info.has_video = true;
61 }
62 
63 // This method is required for all derived classes of EffectBase, and returns a
64 // modified openshot::Frame object
65 std::shared_ptr<Frame> Bars::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
66 {
67  // Get the frame's image
68  std::shared_ptr<QImage> frame_image = frame->GetImage();
69 
70  // Get bar color (and create small color image)
71  std::shared_ptr<QImage> tempColor = std::shared_ptr<QImage>(new QImage(frame_image->width(), 1, QImage::Format_RGBA8888));
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 = top_value * frame_image->height();
86  int bottom_bar_height = bottom_value * frame_image->height();
87  int left_bar_width = left_value * frame_image->width();
88  int right_bar_width = right_value * frame_image->width();
89 
90  // Loop through rows
91  for (int row = 0; row < frame_image->height(); row++) {
92 
93  // Top & Bottom Bar
94  if ((top_bar_height > 0.0 && row <= top_bar_height) || (bottom_bar_height > 0.0 && row >= frame_image->height() - bottom_bar_height)) {
95  memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * frame_image->width() * 4);
96  } else {
97  // Left Bar
98  if (left_bar_width > 0.0) {
99  memcpy(&pixels[row * frame_image->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 * frame_image->width()) + (frame_image->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;
180  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
181  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
182  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
183  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
184  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
185  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
186 
187  // Keyframes
188  root["color"] = add_property_json("Bar Color", 0.0, "color", "", NULL, 0, 255, false, requested_frame);
189  root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
190  root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
191  root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
192  root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 0.5, false, requested_frame);
193  root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 0.5, false, requested_frame);
194  root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 0.5, false, requested_frame);
195  root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 0.5, false, requested_frame);
196 
197  // Return formatted string
198  return root.toStyledString();
199 }
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Bars.cpp:157
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Bars.cpp:176
Keyframe right
Size of right bar.
Definition: Bars.h:65
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Bars.cpp:140
Keyframe top
Size of top bar.
Definition: Bars.h:64
std::string Json() const override
Get and Set JSON methods.
Definition: Bars.cpp:117
Bars()
Blank constructor, useful when using Json to load the effect properties.
Definition: Bars.cpp:36
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number)
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Definition: Bars.cpp:65
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Bars.cpp:124
Keyframe bottom
Size of bottom bar.
Definition: Bars.h:66
Keyframe left
Size of left bar.
Definition: Bars.h:63
Color color
Color of bars.
Definition: Bars.h:62
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
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:68
This class represents a color (used on the timeline and clips)
Definition: Color.h:45
std::string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition: Color.cpp:67
openshot::Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:50
openshot::Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:48
openshot::Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:49
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Color.cpp:126
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:95
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
Exception for invalid JSON.
Definition: Exceptions.h:206
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:64
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
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
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54