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