OpenShot Library | libopenshot  0.7.0
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  mask_mode(BLUR_MASK_POST_BLEND) {
21  // Init effect properties
22  init_effect_details();
23 }
24 
25 // Default constructor
26 Blur::Blur(Keyframe new_horizontal_radius, Keyframe new_vertical_radius, Keyframe new_sigma, Keyframe new_iterations) :
27  horizontal_radius(new_horizontal_radius), vertical_radius(new_vertical_radius),
28  sigma(new_sigma), iterations(new_iterations), mask_mode(BLUR_MASK_POST_BLEND)
29 {
30  // Init effect properties
31  init_effect_details();
32 }
33 
34 // Init effect settings
35 void Blur::init_effect_details()
36 {
39 
41  info.class_name = "Blur";
42  info.name = "Blur";
43  info.description = "Adjust the blur of the frame's image.";
44  info.has_audio = false;
45  info.has_video = true;
46 }
47 
48 // This method is required for all derived classes of EffectBase, and returns a
49 // modified openshot::Frame object
50 std::shared_ptr<openshot::Frame> Blur::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
51 {
52  // Get the frame's image
53  std::shared_ptr<QImage> frame_image = frame->GetImage();
54 
55  // Get the current blur radius
56  int horizontal_radius_value = horizontal_radius.GetValue(frame_number);
57  int vertical_radius_value = vertical_radius.GetValue(frame_number);
58  float sigma_value = sigma.GetValue(frame_number);
59  int iteration_value = iterations.GetInt(frame_number);
60  (void) sigma_value;
61 
62  int w = frame_image->width();
63  int h = frame_image->height();
64 
65  // Grab two copies of the image pixel data
66  QImage image_copy = frame_image->copy();
67  std::shared_ptr<QImage> frame_image_2 = std::make_shared<QImage>(image_copy);
68 
69  // Loop through each iteration
70  for (int iteration = 0; iteration < iteration_value; ++iteration)
71  {
72  // HORIZONTAL BLUR (if any)
73  if (horizontal_radius_value > 0.0) {
74  // Apply horizontal blur to target RGBA channels
75  boxBlurH(frame_image->bits(), frame_image_2->bits(), w, h, horizontal_radius_value);
76 
77  // Swap output image back to input
78  frame_image.swap(frame_image_2);
79  }
80 
81  // VERTICAL BLUR (if any)
82  if (vertical_radius_value > 0.0) {
83  // Apply vertical blur to target RGBA channels
84  boxBlurT(frame_image->bits(), frame_image_2->bits(), w, h, vertical_radius_value);
85 
86  // Swap output image back to input
87  frame_image.swap(frame_image_2);
88  }
89  }
90 
91  // return the modified frame
92  return frame;
93 }
94 
95 bool Blur::UseCustomMaskBlend(int64_t frame_number) const {
96  (void) frame_number;
98 }
99 
100 void Blur::ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
101  std::shared_ptr<QImage> mask_image, int64_t frame_number) const {
102  (void) frame_number;
103  if (!original_image || !effected_image || !mask_image)
104  return;
105  if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
106  return;
107 
108  unsigned char* original_pixels = reinterpret_cast<unsigned char*>(original_image->bits());
109  unsigned char* effected_pixels = reinterpret_cast<unsigned char*>(effected_image->bits());
110  unsigned char* mask_pixels = reinterpret_cast<unsigned char*>(mask_image->bits());
111  const int pixel_count = effected_image->width() * effected_image->height();
112 
113  #pragma omp parallel for schedule(static)
114  for (int i = 0; i < pixel_count; ++i) {
115  const int idx = i * 4;
116  float factor = static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
117  if (mask_invert)
118  factor = 1.0f - factor;
119  // Use a non-linear response curve for custom blur drive mode.
120  factor = factor * factor;
121  const float inverse = 1.0f - factor;
122 
123  // Drive blur amount with the grayscale mask while preserving source alpha.
124  effected_pixels[idx] = static_cast<unsigned char>(
125  (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
126  effected_pixels[idx + 1] = static_cast<unsigned char>(
127  (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
128  effected_pixels[idx + 2] = static_cast<unsigned char>(
129  (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
130  effected_pixels[idx + 3] = original_pixels[idx + 3];
131  }
132 }
133 
134 // Credit: http://blog.ivank.net/fastest-gaussian-blur.html (MIT License)
135 // Modified to process all four channels in a pixel array
136 void Blur::boxBlurH(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
137  const float iarr = 1.0f / (r + r + 1);
138 
139  #pragma omp parallel for shared(scl, tcl)
140  for (int i = 0; i < h; ++i) {
141  const unsigned char* src = scl + i * w * 4;
142  unsigned char* dst = tcl + i * w * 4;
143 
144  const unsigned char* first = src;
145  const unsigned char* last = src + (w - 1) * 4;
146 
147  int val[4];
148  for (int c = 0; c < 4; ++c)
149  val[c] = (r + 1) * first[c];
150  for (int j = 0; j < r; ++j) {
151  const unsigned char* p = src + j * 4;
152  for (int c = 0; c < 4; ++c)
153  val[c] += p[c];
154  }
155 
156  int li = 0, ri = r;
157  for (int j = 0; j <= r; ++j, ++ri) {
158  const unsigned char* add = src + ri * 4;
159  unsigned char* out = dst + j * 4;
160  for (int c = 0; c < 4; ++c) {
161  val[c] += add[c] - first[c];
162  out[c] = (unsigned char)(val[c] * iarr + 0.5f);
163  }
164  }
165  for (int j = r + 1; j < w - r; ++j, ++li, ++ri) {
166  const unsigned char* add = src + ri * 4;
167  const unsigned char* sub = src + li * 4;
168  unsigned char* out = dst + j * 4;
169  for (int c = 0; c < 4; ++c) {
170  val[c] += add[c] - sub[c];
171  out[c] = (unsigned char)(val[c] * iarr + 0.5f);
172  }
173  }
174  for (int j = w - r; j < w; ++j, ++li) {
175  const unsigned char* sub = src + li * 4;
176  unsigned char* out = dst + j * 4;
177  for (int c = 0; c < 4; ++c) {
178  val[c] += last[c] - sub[c];
179  out[c] = (unsigned char)(val[c] * iarr + 0.5f);
180  }
181  }
182  }
183 }
184 
185 void Blur::boxBlurT(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
186  const float iarr = 1.0f / (r + r + 1);
187  const int stride = w * 4;
188 
189  #pragma omp parallel for shared(scl, tcl)
190  for (int i = 0; i < w; ++i) {
191  const unsigned char* col_src = scl + i * 4;
192  unsigned char* col_dst = tcl + i * 4;
193 
194  const unsigned char* first = col_src;
195  const unsigned char* last = col_src + (h - 1) * stride;
196 
197  int val[4];
198  for (int c = 0; c < 4; ++c)
199  val[c] = (r + 1) * first[c];
200  for (int j = 0; j < r; ++j) {
201  const unsigned char* p = col_src + j * stride;
202  for (int c = 0; c < 4; ++c)
203  val[c] += p[c];
204  }
205 
206  int li = 0, ri = r;
207  for (int j = 0; j <= r; ++j, ++ri) {
208  const unsigned char* add = col_src + ri * stride;
209  unsigned char* out = col_dst + j * stride;
210  for (int c = 0; c < 4; ++c) {
211  val[c] += add[c] - first[c];
212  out[c] = (unsigned char)(val[c] * iarr + 0.5f);
213  }
214  }
215  for (int j = r + 1; j < h - r; ++j, ++li, ++ri) {
216  const unsigned char* add = col_src + ri * stride;
217  const unsigned char* sub = col_src + li * stride;
218  unsigned char* out = col_dst + j * stride;
219  for (int c = 0; c < 4; ++c) {
220  val[c] += add[c] - sub[c];
221  out[c] = (unsigned char)(val[c] * iarr + 0.5f);
222  }
223  }
224  for (int j = h - r; j < h; ++j, ++li) {
225  const unsigned char* sub = col_src + li * stride;
226  unsigned char* out = col_dst + j * stride;
227  for (int c = 0; c < 4; ++c) {
228  val[c] += last[c] - sub[c];
229  out[c] = (unsigned char)(val[c] * iarr + 0.5f);
230  }
231  }
232  }
233 }
234 
235 // Generate JSON string of this object
236 std::string Blur::Json() const {
237 
238  // Return formatted string
239  return JsonValue().toStyledString();
240 }
241 
242 // Generate Json::Value for this object
243 Json::Value Blur::JsonValue() const {
244 
245  // Create root json object
246  Json::Value root = EffectBase::JsonValue(); // get parent properties
247  root["type"] = info.class_name;
248  root["horizontal_radius"] = horizontal_radius.JsonValue();
249  root["vertical_radius"] = vertical_radius.JsonValue();
250  root["sigma"] = sigma.JsonValue();
251  root["iterations"] = iterations.JsonValue();
252  root["mask_mode"] = mask_mode;
253 
254  // return JsonValue
255  return root;
256 }
257 
258 // Load JSON string into this object
259 void Blur::SetJson(const std::string value) {
260 
261  // Parse JSON string into JSON objects
262  try
263  {
264  const Json::Value root = openshot::stringToJson(value);
265  // Set all values that match
266  SetJsonValue(root);
267  }
268  catch (const std::exception& e)
269  {
270  // Error parsing JSON (or missing keys)
271  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
272  }
273 }
274 
275 // Load Json::Value into this object
276 void Blur::SetJsonValue(const Json::Value root) {
277 
278  // Set parent data
280 
281  // Set data from Json (if key is found)
282  if (!root["horizontal_radius"].isNull())
283  horizontal_radius.SetJsonValue(root["horizontal_radius"]);
284  if (!root["vertical_radius"].isNull())
285  vertical_radius.SetJsonValue(root["vertical_radius"]);
286  if (!root["sigma"].isNull())
287  sigma.SetJsonValue(root["sigma"]);
288  if (!root["iterations"].isNull())
289  iterations.SetJsonValue(root["iterations"]);
290  if (!root["mask_mode"].isNull())
291  mask_mode = root["mask_mode"].asInt();
292 }
293 
294 // Get all properties for a specific frame
295 std::string Blur::PropertiesJSON(int64_t requested_frame) const {
296 
297  // Generate JSON properties list
298  Json::Value root = BasePropertiesJSON(requested_frame);
299 
300  // Keyframes
301  root["horizontal_radius"] = add_property_json("Horizontal Radius", horizontal_radius.GetValue(requested_frame), "float", "", &horizontal_radius, 0, 100, false, requested_frame);
302  root["vertical_radius"] = add_property_json("Vertical Radius", vertical_radius.GetValue(requested_frame), "float", "", &vertical_radius, 0, 100, false, requested_frame);
303  root["sigma"] = add_property_json("Sigma", sigma.GetValue(requested_frame), "float", "", &sigma, 0, 100, false, requested_frame);
304  root["iterations"] = add_property_json("Iterations", iterations.GetValue(requested_frame), "float", "", &iterations, 0, 100, false, requested_frame);
305  root["mask_mode"] = add_property_json("Mask Mode", mask_mode, "int", "", NULL, 0, 1, false, requested_frame);
306  root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", BLUR_MASK_POST_BLEND, mask_mode));
307  root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", BLUR_MASK_DRIVE_AMOUNT, mask_mode));
308 
309  // Return formatted string
310  return root.toStyledString();
311 }
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::Blur::UseCustomMaskBlend
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
Definition: Blur.cpp:95
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:110
openshot::Blur::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Blur.cpp:295
openshot::Blur::iterations
Keyframe iterations
Iterations keyframe. The # of blur iterations per pixel. 3 iterations = Gaussian.
Definition: Blur.h:59
openshot::Blur::ApplyCustomMaskBlend
void ApplyCustomMaskBlend(std::shared_ptr< QImage > original_image, std::shared_ptr< QImage > effected_image, std::shared_ptr< QImage > mask_image, int64_t frame_number) const override
Optional override for effects with custom mask implementation.
Definition: Blur.cpp:100
openshot::EffectBase::mask_invert
bool mask_invert
Invert grayscale mask values before blending.
Definition: EffectBase.h:111
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::ClipBase::add_property_choice_json
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition: ClipBase.cpp:132
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:96
openshot::Blur::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Blur.cpp:236
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:80
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:236
openshot::Blur::mask_mode
int mask_mode
How to apply common masks to blur (post-blend or drive-amount).
Definition: Blur.h:60
openshot::Blur::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Blur.cpp:276
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:223
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:37
openshot::BLUR_MASK_DRIVE_AMOUNT
@ BLUR_MASK_DRIVE_AMOUNT
Definition: Blur.h:29
openshot::Blur::vertical_radius
Keyframe vertical_radius
Vertical blur radius keyframe. The size of the vertical blur operation in pixels.
Definition: Blur.h:57
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:44
Blur.h
Header file for Blur effect class.
openshot::BLUR_MASK_POST_BLEND
@ BLUR_MASK_POST_BLEND
Definition: Blur.h:28
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:39
openshot::Blur::horizontal_radius
Keyframe horizontal_radius
Horizontal blur radius keyframe. The size of the horizontal blur operation in pixels.
Definition: Blur.h:56
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:41
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:43
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:40
openshot::Blur::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Blur.cpp:243
openshot::Blur::sigma
Keyframe sigma
Sigma keyframe. The amount of spread in the blur operation. Should be larger than radius.
Definition: Blur.h:58
openshot::Blur::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Blur.cpp:259
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:139
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258