OpenShot Library | libopenshot 0.2.7
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 "Bars.h"
32#include "Exceptions.h"
33
34using namespace openshot;
35
36/// Blank constructor, useful when using Json to load the effect properties
37Bars::Bars() : color("#000000"), left(0.0), top(0.1), right(0.0), bottom(0.1) {
38 // Init effect properties
39 init_effect_details();
40}
41
42// Default constructor
43Bars::Bars(Color color, Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
44 color(color), left(left), top(top), right(right), bottom(bottom)
45{
46 // Init effect properties
47 init_effect_details();
48}
49
50// Init effect settings
51void Bars::init_effect_details()
52{
53 /// Initialize the values of the EffectInfo struct.
55
56 /// Set the effect info
57 info.class_name = "Bars";
58 info.name = "Bars";
59 info.description = "Add colored bars around your video.";
60 info.has_audio = false;
61 info.has_video = true;
62}
63
64// This method is required for all derived classes of EffectBase, and returns a
65// modified openshot::Frame object
66std::shared_ptr<openshot::Frame> Bars::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
67{
68 // Get the frame's image
69 std::shared_ptr<QImage> frame_image = frame->GetImage();
70
71 // Get bar color (and create small color image)
72 auto tempColor = std::make_shared<QImage>(
73 frame_image->width(), 1, QImage::Format_RGBA8888_Premultiplied);
74 tempColor->fill(QColor(QString::fromStdString(color.GetColorHex(frame_number))));
75
76 // Get current keyframe values
77 double left_value = left.GetValue(frame_number);
78 double top_value = top.GetValue(frame_number);
79 double right_value = right.GetValue(frame_number);
80 double bottom_value = bottom.GetValue(frame_number);
81
82 // Get pixel array pointer
83 unsigned char *pixels = (unsigned char *) frame_image->bits();
84 unsigned char *color_pixels = (unsigned char *) tempColor->bits();
85
86 // Get pixels sizes of all bars
87 int top_bar_height = top_value * frame_image->height();
88 int bottom_bar_height = bottom_value * frame_image->height();
89 int left_bar_width = left_value * frame_image->width();
90 int right_bar_width = right_value * frame_image->width();
91
92 // Loop through rows
93 for (int row = 0; row < frame_image->height(); row++) {
94
95 // Top & Bottom Bar
96 if ((top_bar_height > 0.0 && row <= top_bar_height) || (bottom_bar_height > 0.0 && row >= frame_image->height() - bottom_bar_height)) {
97 memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * frame_image->width() * 4);
98 } else {
99 // Left Bar
100 if (left_bar_width > 0.0) {
101 memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * left_bar_width * 4);
102 }
103
104 // Right Bar
105 if (right_bar_width > 0.0) {
106 memcpy(&pixels[((row * frame_image->width()) + (frame_image->width() - right_bar_width)) * 4], color_pixels, sizeof(char) * right_bar_width * 4);
107 }
108 }
109 }
110
111 // Cleanup colors and arrays
112 tempColor.reset();
113
114 // return the modified frame
115 return frame;
116}
117
118// Generate JSON string of this object
119std::string Bars::Json() const {
120
121 // Return formatted string
122 return JsonValue().toStyledString();
123}
124
125// Generate Json::Value for this object
126Json::Value Bars::JsonValue() const {
127
128 // Create root json object
129 Json::Value root = EffectBase::JsonValue(); // get parent properties
130 root["type"] = info.class_name;
131 root["color"] = color.JsonValue();
132 root["left"] = left.JsonValue();
133 root["top"] = top.JsonValue();
134 root["right"] = right.JsonValue();
135 root["bottom"] = bottom.JsonValue();
136
137 // return JsonValue
138 return root;
139}
140
141// Load JSON string into this object
142void Bars::SetJson(const std::string value) {
143
144 // Parse JSON string into JSON objects
145 try
146 {
147 const Json::Value root = openshot::stringToJson(value);
148 // Set all values that match
149 SetJsonValue(root);
150 }
151 catch (const std::exception& e)
152 {
153 // Error parsing JSON (or missing keys)
154 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
155 }
156}
157
158// Load Json::Value into this object
159void Bars::SetJsonValue(const Json::Value root) {
160
161 // Set parent data
163
164 // Set data from Json (if key is found)
165 if (!root["color"].isNull())
166 color.SetJsonValue(root["color"]);
167 if (!root["left"].isNull())
168 left.SetJsonValue(root["left"]);
169 if (!root["top"].isNull())
170 top.SetJsonValue(root["top"]);
171 if (!root["right"].isNull())
172 right.SetJsonValue(root["right"]);
173 if (!root["bottom"].isNull())
174 bottom.SetJsonValue(root["bottom"]);
175}
176
177// Get all properties for a specific frame
178std::string Bars::PropertiesJSON(int64_t requested_frame) const {
179
180 // Generate JSON properties list
181 Json::Value root;
182 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
183 root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
184 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
185 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
186 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
187 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
188
189 // Keyframes
190 root["color"] = add_property_json("Bar Color", 0.0, "color", "", &color.red, 0, 255, false, requested_frame);
191 root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
192 root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
193 root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
194 root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 0.5, false, requested_frame);
195 root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 0.5, false, requested_frame);
196 root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 0.5, false, requested_frame);
197 root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 0.5, false, requested_frame);
198
199 // Set the parent effect which properties this effect will inherit
200 root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
201
202 // Return formatted string
203 return root.toStyledString();
204}
Header file for Bars effect class.
Header file for all Exception classes.
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Bars.cpp:178
Keyframe right
Size of right bar.
Definition: Bars.h:65
Keyframe top
Size of top bar.
Definition: Bars.h:64
std::string Json() const override
Generate JSON string of this object.
Definition: Bars.cpp:119
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Bars.cpp:142
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Bars.cpp:159
Bars()
Blank constructor, useful when using Json to load the effect properties.
Definition: Bars.cpp:37
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Bars.cpp:126
Keyframe bottom
Size of bottom bar.
Definition: Bars.h:66
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:86
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:111
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
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:68
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:138
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:107
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87
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:72
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56