OpenShot Library | libopenshot  0.2.6
Blur.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Blur 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 "Blur.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 /// Blank constructor, useful when using Json to load the effect properties
37 Blur::Blur() : horizontal_radius(6.0), vertical_radius(6.0), sigma(3.0), iterations(3.0) {
38  // Init effect properties
39  init_effect_details();
40 }
41 
42 // Default constructor
43 Blur::Blur(Keyframe new_horizontal_radius, Keyframe new_vertical_radius, Keyframe new_sigma, Keyframe new_iterations) :
44  horizontal_radius(new_horizontal_radius), vertical_radius(new_vertical_radius),
45  sigma(new_sigma), iterations(new_iterations)
46 {
47  // Init effect properties
48  init_effect_details();
49 }
50 
51 // Init effect settings
52 void Blur::init_effect_details()
53 {
54  /// Initialize the values of the EffectInfo struct.
56 
57  /// Set the effect info
58  info.class_name = "Blur";
59  info.name = "Blur";
60  info.description = "Adjust the blur of the frame's image.";
61  info.has_audio = false;
62  info.has_video = true;
63 }
64 
65 // This method is required for all derived classes of EffectBase, and returns a
66 // modified openshot::Frame object
67 std::shared_ptr<openshot::Frame> Blur::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
68 {
69  // Get the frame's image
70  std::shared_ptr<QImage> frame_image = frame->GetImage();
71 
72  // Get the current blur radius
73  int horizontal_radius_value = horizontal_radius.GetValue(frame_number);
74  int vertical_radius_value = vertical_radius.GetValue(frame_number);
75  float sigma_value = sigma.GetValue(frame_number);
76  int iteration_value = iterations.GetInt(frame_number);
77 
78  int w = frame_image->width();
79  int h = frame_image->height();
80 
81  // Grab two copies of the image pixel data
82  QImage image_copy = frame_image->copy();
83  std::shared_ptr<QImage> frame_image_2 = std::make_shared<QImage>(image_copy);
84 
85  // Loop through each iteration
86  for (int iteration = 0; iteration < iteration_value; ++iteration)
87  {
88  // HORIZONTAL BLUR (if any)
89  if (horizontal_radius_value > 0.0) {
90  // Apply horizontal blur to target RGBA channels
91  boxBlurH(frame_image->bits(), frame_image_2->bits(), w, h, horizontal_radius_value);
92 
93  // Swap output image back to input
94  frame_image.swap(frame_image_2);
95  }
96 
97  // VERTICAL BLUR (if any)
98  if (vertical_radius_value > 0.0) {
99  // Apply vertical blur to target RGBA channels
100  boxBlurT(frame_image->bits(), frame_image_2->bits(), w, h, vertical_radius_value);
101 
102  // Swap output image back to input
103  frame_image.swap(frame_image_2);
104  }
105  }
106 
107  // return the modified frame
108  return frame;
109 }
110 
111 // Credit: http://blog.ivank.net/fastest-gaussian-blur.html (MIT License)
112 // Modified to process all four channels in a pixel array
113 void Blur::boxBlurH(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
114  float iarr = 1.0 / (r + r + 1);
115 
116  #pragma omp parallel for shared (scl, tcl)
117  for (int i = 0; i < h; ++i) {
118  for (int ch = 0; ch < 4; ++ch) {
119  int ti = i * w, li = ti, ri = ti + r;
120  int fv = scl[ti * 4 + ch], lv = scl[(ti + w - 1) * 4 + ch], val = (r + 1) * fv;
121  for (int j = 0; j < r; ++j) {
122  val += scl[(ti + j) * 4 + ch];
123  }
124  for (int j = 0; j <= r; ++j) {
125  val += scl[ri++ * 4 + ch] - fv;
126  tcl[ti++ * 4 + ch] = round(val * iarr);
127  }
128  for (int j = r + 1; j < w - r; ++j) {
129  val += scl[ri++ * 4 + ch] - scl[li++ * 4 + ch];
130  tcl[ti++ * 4 + ch] = round(val * iarr);
131  }
132  for (int j = w - r; j < w; ++j) {
133  val += lv - scl[li++ * 4 + ch];
134  tcl[ti++ * 4 + ch] = round(val * iarr);
135  }
136  }
137  }
138 }
139 
140 void Blur::boxBlurT(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
141  float iarr = 1.0 / (r + r + 1);
142 
143  #pragma omp parallel for shared (scl, tcl)
144  for (int i = 0; i < w; i++) {
145  for (int ch = 0; ch < 4; ++ch) {
146  int ti = i, li = ti, ri = ti + r * w;
147  int fv = scl[ti * 4 + ch], lv = scl[(ti + w * (h - 1)) * 4 + ch], val = (r + 1) * fv;
148  for (int j = 0; j < r; j++) val += scl[(ti + j * w) * 4 + ch];
149  for (int j = 0; j <= r; j++) {
150  val += scl[ri * 4 + ch] - fv;
151  tcl[ti * 4 + ch] = round(val * iarr);
152  ri += w;
153  ti += w;
154  }
155  for (int j = r + 1; j < h - r; j++) {
156  val += scl[ri * 4 + ch] - scl[li * 4 + ch];
157  tcl[ti * 4 + ch] = round(val * iarr);
158  li += w;
159  ri += w;
160  ti += w;
161  }
162  for (int j = h - r; j < h; j++) {
163  val += lv - scl[li * 4 + ch];
164  tcl[ti * 4 + ch] = round(val * iarr);
165  li += w;
166  ti += w;
167  }
168  }
169  }
170 }
171 
172 // Generate JSON string of this object
173 std::string Blur::Json() const {
174 
175  // Return formatted string
176  return JsonValue().toStyledString();
177 }
178 
179 // Generate Json::Value for this object
180 Json::Value Blur::JsonValue() const {
181 
182  // Create root json object
183  Json::Value root = EffectBase::JsonValue(); // get parent properties
184  root["type"] = info.class_name;
185  root["horizontal_radius"] = horizontal_radius.JsonValue();
186  root["vertical_radius"] = vertical_radius.JsonValue();
187  root["sigma"] = sigma.JsonValue();
188  root["iterations"] = iterations.JsonValue();
189 
190  // return JsonValue
191  return root;
192 }
193 
194 // Load JSON string into this object
195 void Blur::SetJson(const std::string value) {
196 
197  // Parse JSON string into JSON objects
198  try
199  {
200  const Json::Value root = openshot::stringToJson(value);
201  // Set all values that match
202  SetJsonValue(root);
203  }
204  catch (const std::exception& e)
205  {
206  // Error parsing JSON (or missing keys)
207  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
208  }
209 }
210 
211 // Load Json::Value into this object
212 void Blur::SetJsonValue(const Json::Value root) {
213 
214  // Set parent data
216 
217  // Set data from Json (if key is found)
218  if (!root["horizontal_radius"].isNull())
219  horizontal_radius.SetJsonValue(root["horizontal_radius"]);
220  if (!root["vertical_radius"].isNull())
221  vertical_radius.SetJsonValue(root["vertical_radius"]);
222  if (!root["sigma"].isNull())
223  sigma.SetJsonValue(root["sigma"]);
224  if (!root["iterations"].isNull())
225  iterations.SetJsonValue(root["iterations"]);
226 }
227 
228 // Get all properties for a specific frame
229 std::string Blur::PropertiesJSON(int64_t requested_frame) const {
230 
231  // Generate JSON properties list
232  Json::Value root;
233  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
234  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
235  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
236  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
237  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
238  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
239 
240  // Keyframes
241  root["horizontal_radius"] = add_property_json("Horizontal Radius", horizontal_radius.GetValue(requested_frame), "float", "", &horizontal_radius, 0, 100, false, requested_frame);
242  root["vertical_radius"] = add_property_json("Vertical Radius", vertical_radius.GetValue(requested_frame), "float", "", &vertical_radius, 0, 100, false, requested_frame);
243  root["sigma"] = add_property_json("Sigma", sigma.GetValue(requested_frame), "float", "", &sigma, 0, 100, false, requested_frame);
244  root["iterations"] = add_property_json("Iterations", iterations.GetValue(requested_frame), "float", "", &iterations, 0, 100, false, requested_frame);
245 
246  // Set the parent effect which properties this effect will inherit
247  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
248 
249  // Return formatted string
250  return root.toStyledString();
251 }
Header file for Blur effect class.
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Blur.cpp:195
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
Keyframe iterations
Iterations keyframe. The # of blur iterations per pixel. 3 iterations = Gaussian. ...
Definition: Blur.h:69
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Blur.cpp:212
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:292
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
Header file for all Exception classes.
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: Blur.h:89
Keyframe vertical_radius
Vertical blur radius keyframe. The size of the vertical blur operation in pixels. ...
Definition: Blur.h:67
std::string Json() const override
Generate JSON string of this object.
Definition: Blur.cpp:173
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
Blur()
Blank constructor, useful when using Json to load the effect properties.
Definition: Blur.cpp:37
std::string name
The name of the effect.
Definition: EffectBase.h:55
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Blur.cpp:229
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
Exception for invalid JSON.
Definition: Exceptions.h:205
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:72
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
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Blur.cpp:180
Keyframe sigma
Sigma keyframe. The amount of spread in the blur operation. Should be larger than radius...
Definition: Blur.h:68
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
Keyframe horizontal_radius
Horizontal blur radius keyframe. The size of the horizontal blur operation in pixels.
Definition: Blur.h:66
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87