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