26 double clamp_margin(
double value) {
36 Blur::Blur() : horizontal_radius(6.0), vertical_radius(6.0), sigma(3.0), iterations(3.0),
37 left(0.0), top(0.0), right(0.0), bottom(0.0),
40 init_effect_details();
45 horizontal_radius(new_horizontal_radius), vertical_radius(new_vertical_radius),
46 sigma(new_sigma), iterations(new_iterations),
47 left(0.0), top(0.0), right(0.0), bottom(0.0),
51 init_effect_details();
57 horizontal_radius(new_horizontal_radius), vertical_radius(new_vertical_radius),
58 sigma(new_sigma), iterations(new_iterations),
59 left(new_left), top(new_top), right(new_right), bottom(new_bottom),
63 init_effect_details();
67 void Blur::init_effect_details()
82 std::shared_ptr<openshot::Frame>
Blur::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
85 std::shared_ptr<QImage> frame_image = frame->GetImage();
94 int w = frame_image->width();
95 int h = frame_image->height();
96 if (w <= 0 || h <= 0 || iteration_value <= 0)
100 QRect area(QPoint(0, 0), frame_image->size());
101 area = area.marginsRemoved({
107 area = area.intersected(QRect(QPoint(0, 0), frame_image->size()));
112 QImage image_copy = frame_image->copy(area);
113 std::shared_ptr<QImage> blur_image = std::make_shared<QImage>(image_copy);
114 std::shared_ptr<QImage> frame_image_2 = std::make_shared<QImage>(image_copy);
115 const int area_w = blur_image->width();
116 const int area_h = blur_image->height();
117 const int horizontal_area_radius = std::min(std::max(0, horizontal_radius_value), std::max(0, area_w - 1));
118 const int vertical_area_radius = std::min(std::max(0, vertical_radius_value), std::max(0, area_h - 1));
119 bool blurred =
false;
122 for (
int iteration = 0; iteration < iteration_value; ++iteration)
125 if (horizontal_area_radius > 0.0) {
127 boxBlurH(blur_image->bits(), frame_image_2->bits(), area_w, area_h, horizontal_area_radius);
130 blur_image.swap(frame_image_2);
135 if (vertical_area_radius > 0.0) {
137 boxBlurT(blur_image->bits(), frame_image_2->bits(), area_w, area_h, vertical_area_radius);
140 blur_image.swap(frame_image_2);
146 QPainter painter(frame_image.get());
147 painter.drawImage(area, *blur_image);
161 std::shared_ptr<QImage> mask_image, int64_t frame_number)
const {
163 if (!original_image || !effected_image || !mask_image)
165 if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
168 unsigned char* original_pixels =
reinterpret_cast<unsigned char*
>(original_image->bits());
169 unsigned char* effected_pixels =
reinterpret_cast<unsigned char*
>(effected_image->bits());
170 unsigned char* mask_pixels =
reinterpret_cast<unsigned char*
>(mask_image->bits());
171 const int pixel_count = effected_image->width() * effected_image->height();
173 #pragma omp parallel for schedule(static)
174 for (
int i = 0; i < pixel_count; ++i) {
175 const int idx = i * 4;
176 float factor =
static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
178 factor = 1.0f - factor;
180 factor = factor * factor;
181 const float inverse = 1.0f - factor;
184 effected_pixels[idx] =
static_cast<unsigned char>(
185 (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
186 effected_pixels[idx + 1] =
static_cast<unsigned char>(
187 (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
188 effected_pixels[idx + 2] =
static_cast<unsigned char>(
189 (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
190 effected_pixels[idx + 3] = original_pixels[idx + 3];
196 void Blur::boxBlurH(
unsigned char *scl,
unsigned char *tcl,
int w,
int h,
int r) {
197 const float iarr = 1.0f / (r + r + 1);
199 #pragma omp parallel for shared(scl, tcl)
200 for (
int i = 0; i < h; ++i) {
201 const unsigned char* src = scl + i * w * 4;
202 unsigned char* dst = tcl + i * w * 4;
204 int val[4] = {0, 0, 0, 0};
205 for (
int j = -r; j <= r; ++j) {
206 const int sample_x = std::min(std::max(j, 0), w - 1);
207 const unsigned char* p = src + sample_x * 4;
208 for (
int c = 0; c < 4; ++c)
212 for (
int j = 0; j < w; ++j) {
213 unsigned char* out = dst + j * 4;
214 for (
int c = 0; c < 4; ++c) {
215 out[c] = (
unsigned char)(val[c] * iarr + 0.5f);
218 const int remove_x = std::min(std::max(j - r, 0), w - 1);
219 const int add_x = std::min(std::max(j + r + 1, 0), w - 1);
220 const unsigned char* remove = src + remove_x * 4;
221 const unsigned char* add = src + add_x * 4;
222 for (
int c = 0; c < 4; ++c) {
223 val[c] += add[c] - remove[c];
229 void Blur::boxBlurT(
unsigned char *scl,
unsigned char *tcl,
int w,
int h,
int r) {
230 const float iarr = 1.0f / (r + r + 1);
231 const int stride = w * 4;
233 #pragma omp parallel for shared(scl, tcl)
234 for (
int i = 0; i < w; ++i) {
235 const unsigned char* col_src = scl + i * 4;
236 unsigned char* col_dst = tcl + i * 4;
238 int val[4] = {0, 0, 0, 0};
239 for (
int j = -r; j <= r; ++j) {
240 const int sample_y = std::min(std::max(j, 0), h - 1);
241 const unsigned char* p = col_src + sample_y * stride;
242 for (
int c = 0; c < 4; ++c)
246 for (
int j = 0; j < h; ++j) {
247 unsigned char* out = col_dst + j * stride;
248 for (
int c = 0; c < 4; ++c) {
249 out[c] = (
unsigned char)(val[c] * iarr + 0.5f);
252 const int remove_y = std::min(std::max(j - r, 0), h - 1);
253 const int add_y = std::min(std::max(j + r + 1, 0), h - 1);
254 const unsigned char* remove = col_src + remove_y * stride;
255 const unsigned char* add = col_src + add_y * stride;
256 for (
int c = 0; c < 4; ++c) {
257 val[c] += add[c] - remove[c];
300 catch (
const std::exception& e)
303 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
314 if (!root[
"horizontal_radius"].isNull())
316 if (!root[
"vertical_radius"].isNull())
318 if (!root[
"sigma"].isNull())
320 if (!root[
"iterations"].isNull())
322 if (!root[
"left"].isNull())
324 if (!root[
"top"].isNull())
326 if (!root[
"right"].isNull())
328 if (!root[
"bottom"].isNull())
330 if (!root[
"mask_mode"].isNull())
354 return root.toStyledString();
Header file for Blur effect class.
Header file for all Exception classes.
Keyframe bottom
Size of bottom margin.
Keyframe vertical_radius
Vertical blur radius keyframe. The size of the vertical blur operation in pixels.
Keyframe left
Size of left margin.
Keyframe top
Size of top margin.
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.
int mask_mode
How to apply common masks to blur (post-blend or drive-amount).
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
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...
std::string Json() const override
Generate JSON string of this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
Blur()
Blank constructor, useful when using Json to load the effect properties.
Keyframe iterations
Iterations keyframe. The # of blur iterations per pixel. 3 iterations = Gaussian.
std::string PropertiesJSON(int64_t requested_frame) const override
void SetJson(const std::string value) override
Load JSON string into this object.
Keyframe sigma
Sigma keyframe. The amount of spread in the blur operation. Should be larger than radius.
Keyframe right
Size of right margin.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Keyframe horizontal_radius
Horizontal blur radius keyframe. The size of the horizontal blur operation in pixels.
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
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.
bool mask_invert
Invert grayscale mask values before blending.
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
Exception for invalid JSON.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
double GetValue(int64_t index) const
Get the value at a specific index.
Json::Value JsonValue() const
Generate Json::Value for this object.
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
bool has_video
Determines if this effect manipulates the image of a frame.
bool has_audio
Determines if this effect manipulates the audio of a frame.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
std::string description
The description of this effect and what it does.