OpenShot Library | libopenshot  0.5.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  float iarr = 1.0 / (r + r + 1);
138 
139  #pragma omp parallel for shared (scl, tcl)
140  for (int i = 0; i < h; ++i) {
141  for (int ch = 0; ch < 4; ++ch) {
142  int ti = i * w, li = ti, ri = ti + r;
143  int fv = scl[ti * 4 + ch], lv = scl[(ti + w - 1) * 4 + ch], val = (r + 1) * fv;
144  for (int j = 0; j < r; ++j) {
145  val += scl[(ti + j) * 4 + ch];
146  }
147  for (int j = 0; j <= r; ++j) {
148  val += scl[ri++ * 4 + ch] - fv;
149  tcl[ti++ * 4 + ch] = round(val * iarr);
150  }
151  for (int j = r + 1; j < w - r; ++j) {
152  val += scl[ri++ * 4 + ch] - scl[li++ * 4 + ch];
153  tcl[ti++ * 4 + ch] = round(val * iarr);
154  }
155  for (int j = w - r; j < w; ++j) {
156  val += lv - scl[li++ * 4 + ch];
157  tcl[ti++ * 4 + ch] = round(val * iarr);
158  }
159  }
160  }
161 }
162 
163 void Blur::boxBlurT(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
164  float iarr = 1.0 / (r + r + 1);
165 
166  #pragma omp parallel for shared (scl, tcl)
167  for (int i = 0; i < w; i++) {
168  for (int ch = 0; ch < 4; ++ch) {
169  int ti = i, li = ti, ri = ti + r * w;
170  int fv = scl[ti * 4 + ch], lv = scl[(ti + w * (h - 1)) * 4 + ch], val = (r + 1) * fv;
171  for (int j = 0; j < r; j++) val += scl[(ti + j * w) * 4 + ch];
172  for (int j = 0; j <= r; j++) {
173  val += scl[ri * 4 + ch] - fv;
174  tcl[ti * 4 + ch] = round(val * iarr);
175  ri += w;
176  ti += w;
177  }
178  for (int j = r + 1; j < h - r; j++) {
179  val += scl[ri * 4 + ch] - scl[li * 4 + ch];
180  tcl[ti * 4 + ch] = round(val * iarr);
181  li += w;
182  ri += w;
183  ti += w;
184  }
185  for (int j = h - r; j < h; j++) {
186  val += lv - scl[li * 4 + ch];
187  tcl[ti * 4 + ch] = round(val * iarr);
188  li += w;
189  ti += w;
190  }
191  }
192  }
193 }
194 
195 // Generate JSON string of this object
196 std::string Blur::Json() const {
197 
198  // Return formatted string
199  return JsonValue().toStyledString();
200 }
201 
202 // Generate Json::Value for this object
203 Json::Value Blur::JsonValue() const {
204 
205  // Create root json object
206  Json::Value root = EffectBase::JsonValue(); // get parent properties
207  root["type"] = info.class_name;
208  root["horizontal_radius"] = horizontal_radius.JsonValue();
209  root["vertical_radius"] = vertical_radius.JsonValue();
210  root["sigma"] = sigma.JsonValue();
211  root["iterations"] = iterations.JsonValue();
212  root["mask_mode"] = mask_mode;
213 
214  // return JsonValue
215  return root;
216 }
217 
218 // Load JSON string into this object
219 void Blur::SetJson(const std::string value) {
220 
221  // Parse JSON string into JSON objects
222  try
223  {
224  const Json::Value root = openshot::stringToJson(value);
225  // Set all values that match
226  SetJsonValue(root);
227  }
228  catch (const std::exception& e)
229  {
230  // Error parsing JSON (or missing keys)
231  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
232  }
233 }
234 
235 // Load Json::Value into this object
236 void Blur::SetJsonValue(const Json::Value root) {
237 
238  // Set parent data
240 
241  // Set data from Json (if key is found)
242  if (!root["horizontal_radius"].isNull())
243  horizontal_radius.SetJsonValue(root["horizontal_radius"]);
244  if (!root["vertical_radius"].isNull())
245  vertical_radius.SetJsonValue(root["vertical_radius"]);
246  if (!root["sigma"].isNull())
247  sigma.SetJsonValue(root["sigma"]);
248  if (!root["iterations"].isNull())
249  iterations.SetJsonValue(root["iterations"]);
250  if (!root["mask_mode"].isNull())
251  mask_mode = root["mask_mode"].asInt();
252 }
253 
254 // Get all properties for a specific frame
255 std::string Blur::PropertiesJSON(int64_t requested_frame) const {
256 
257  // Generate JSON properties list
258  Json::Value root = BasePropertiesJSON(requested_frame);
259 
260  // Keyframes
261  root["horizontal_radius"] = add_property_json("Horizontal Radius", horizontal_radius.GetValue(requested_frame), "float", "", &horizontal_radius, 0, 100, false, requested_frame);
262  root["vertical_radius"] = add_property_json("Vertical Radius", vertical_radius.GetValue(requested_frame), "float", "", &vertical_radius, 0, 100, false, requested_frame);
263  root["sigma"] = add_property_json("Sigma", sigma.GetValue(requested_frame), "float", "", &sigma, 0, 100, false, requested_frame);
264  root["iterations"] = add_property_json("Iterations", iterations.GetValue(requested_frame), "float", "", &iterations, 0, 100, false, requested_frame);
265  root["mask_mode"] = add_property_json("Mask Mode", mask_mode, "int", "", NULL, 0, 1, false, requested_frame);
266  root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", BLUR_MASK_POST_BLEND, mask_mode));
267  root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", BLUR_MASK_DRIVE_AMOUNT, mask_mode));
268 
269  // Return formatted string
270  return root.toStyledString();
271 }
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:255
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: Compressor.h:28
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:196
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:236
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:203
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:219
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