Blur.cpp
Go to the documentation of this file.
/**
* @file
* @brief Source file for Blur effect class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @section LICENSE
*
* Copyright (c) 2008-2014 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../../include/effects/Blur.h"
using namespace openshot;
/// Blank constructor, useful when using Json to load the effect properties
Blur::Blur() : horizontal_radius(6.0), vertical_radius(6.0), sigma(3.0), iterations(3.0) {
// Init effect properties
init_effect_details();
}
// Default constructor
Blur::Blur(Keyframe new_horizontal_radius, Keyframe new_vertical_radius, Keyframe new_sigma, Keyframe new_iterations) :
horizontal_radius(new_horizontal_radius), vertical_radius(new_vertical_radius),
sigma(new_sigma), iterations(new_iterations)
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Blur::init_effect_details()
{
/// Initialize the values of the EffectInfo struct.
InitEffectInfo();
/// Set the effect info
info.class_name = "Blur";
info.name = "Blur";
info.description = "Adjust the blur of the frame's image.";
info.has_audio = false;
info.has_video = true;
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<Frame> Blur::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
{
// Get the frame's image
std::shared_ptr<QImage> frame_image = frame->GetImage();
// Get the current blur radius
int horizontal_radius_value = horizontal_radius.GetValue(frame_number);
int vertical_radius_value = vertical_radius.GetValue(frame_number);
float sigma_value = sigma.GetValue(frame_number);
int iteration_value = iterations.GetInt(frame_number);
// Declare arrays for each color channel
unsigned char *red = new unsigned char[frame_image->width() * frame_image->height()]();
unsigned char *green = new unsigned char[frame_image->width() * frame_image->height()]();
unsigned char *blue = new unsigned char[frame_image->width() * frame_image->height()]();
unsigned char *alpha = new unsigned char[frame_image->width() * frame_image->height()]();
// Create empty target RGBA arrays (for the results of our blur)
unsigned char *blur_red = new unsigned char[frame_image->width() * frame_image->height()]();
unsigned char *blur_green = new unsigned char[frame_image->width() * frame_image->height()]();
unsigned char *blur_blue = new unsigned char[frame_image->width() * frame_image->height()]();
unsigned char *blur_alpha = new unsigned char[frame_image->width() * frame_image->height()]();
// Loop through pixels and split RGBA channels into separate arrays
unsigned char *pixels = (unsigned char *) frame_image->bits();
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
{
// Get the RGBA values from each pixel
unsigned char R = pixels[byte_index];
unsigned char G = pixels[byte_index + 1];
unsigned char B = pixels[byte_index + 2];
unsigned char A = pixels[byte_index + 3];
// Split channels into their own arrays
red[pixel] = R;
green[pixel] = G;
blue[pixel] = B;
alpha[pixel] = A;
}
// Init target RGBA arrays
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) blur_red[i] = red[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) blur_green[i] = green[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) blur_blue[i] = blue[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) blur_alpha[i] = alpha[i];
// Loop through each iteration
for (int iteration = 0; iteration < iteration_value; iteration++)
{
// HORIZONTAL BLUR (if any)
if (horizontal_radius_value > 0.0) {
// Init boxes for computing blur
int *bxs = initBoxes(sigma_value, horizontal_radius_value);
// Apply horizontal blur to target RGBA channels
boxBlurH(red, blur_red, frame_image->width(), frame_image->height(), horizontal_radius_value);
boxBlurH(green, blur_green, frame_image->width(), frame_image->height(), horizontal_radius_value);
boxBlurH(blue, blur_blue, frame_image->width(), frame_image->height(), horizontal_radius_value);
boxBlurH(alpha, blur_alpha, frame_image->width(), frame_image->height(), horizontal_radius_value);
// Remove boxes
delete[] bxs;
// Copy blur_<chan> back to <chan> for vertical blur or next iteration
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) red[i] = blur_red[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) green[i] = blur_green[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) blue[i] = blur_blue[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) alpha[i] = blur_alpha[i];
}
// VERTICAL BLUR (if any)
if (vertical_radius_value > 0.0) {
// Init boxes for computing blur
int *bxs = initBoxes(sigma_value, vertical_radius_value);
// Apply vertical blur to target RGBA channels
boxBlurT(red, blur_red, frame_image->width(), frame_image->height(), vertical_radius_value);
boxBlurT(green, blur_green, frame_image->width(), frame_image->height(), vertical_radius_value);
boxBlurT(blue, blur_blue, frame_image->width(), frame_image->height(), vertical_radius_value);
boxBlurT(alpha, blur_alpha, frame_image->width(), frame_image->height(), vertical_radius_value);
// Remove boxes
delete[] bxs;
// Copy blur_<chan> back to <chan> for vertical blur or next iteration
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) red[i] = blur_red[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) green[i] = blur_green[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) blue[i] = blur_blue[i];
for (int i = 0; i < (frame_image->width() * frame_image->height()); i++) alpha[i] = blur_alpha[i];
}
}
// Copy RGBA channels back to original image
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
{
// Get the RGB values from the pixel
unsigned char R = blur_red[pixel];
unsigned char G = blur_green[pixel];
unsigned char B = blur_blue[pixel];
unsigned char A = blur_alpha[pixel];
// Split channels into their own arrays
pixels[byte_index] = R;
pixels[byte_index + 1] = G;
pixels[byte_index + 2] = B;
pixels[byte_index + 3] = A;
}
// Delete channel arrays
delete[] red;
delete[] green;
delete[] blue;
delete[] alpha;
delete[] blur_red;
delete[] blur_green;
delete[] blur_blue;
delete[] blur_alpha;
// return the modified frame
return frame;
}
// Credit: http://blog.ivank.net/fastest-gaussian-blur.html (MIT License)
int* Blur::initBoxes(float sigma, int n) // standard deviation, number of boxes
{
float wIdeal = sqrt((12.0 * sigma * sigma / n) + 1.0); // Ideal averaging filter width
int wl = floor(wIdeal);
if (wl % 2 == 0) wl--;
int wu = wl + 2;
float mIdeal = (12.0 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4.0 * wl - 4);
int m = round(mIdeal);
int *sizes = new int[n]();
for (int i = 0; i < n; i++) sizes[i] = i < m ? wl : wu;
return sizes;
}
// Credit: http://blog.ivank.net/fastest-gaussian-blur.html (MIT License)
void Blur::boxBlurH(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
float iarr = 1.0 / (r + r + 1);
for (int i = 0; i < h; i++) {
int ti = i * w, li = ti, ri = ti + r;
int fv = scl[ti], lv = scl[ti + w - 1], val = (r + 1) * fv;
for (int j = 0; j < r; j++) val += scl[ti + j];
for (int j = 0; j <= r; j++) {
val += scl[ri++] - fv;
tcl[ti++] = round(val * iarr);
}
for (int j = r + 1; j < w - r; j++) {
val += scl[ri++] - scl[li++];
tcl[ti++] = round(val * iarr);
}
for (int j = w - r; j < w; j++) {
val += lv - scl[li++];
tcl[ti++] = round(val * iarr);
}
}
}
void Blur::boxBlurT(unsigned char *scl, unsigned char *tcl, int w, int h, int r) {
float iarr = 1.0 / (r + r + 1);
for (int i = 0; i < w; i++) {
int ti = i, li = ti, ri = ti + r * w;
int fv = scl[ti], lv = scl[ti + w * (h - 1)], val = (r + 1) * fv;
for (int j = 0; j < r; j++) val += scl[ti + j * w];
for (int j = 0; j <= r; j++) {
val += scl[ri] - fv;
tcl[ti] = round(val * iarr);
ri += w;
ti += w;
}
for (int j = r + 1; j < h - r; j++) {
val += scl[ri] - scl[li];
tcl[ti] = round(val * iarr);
li += w;
ri += w;
ti += w;
}
for (int j = h - r; j < h; j++) {
val += lv - scl[li];
tcl[ti] = round(val * iarr);
li += w;
ti += w;
}
}
}
// Generate JSON string of this object
string Blur::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value Blur::JsonValue() {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["horizontal_radius"] = horizontal_radius.JsonValue();
root["vertical_radius"] = vertical_radius.JsonValue();
root["sigma"] = sigma.JsonValue();
root["iterations"] = iterations.JsonValue();
// return JsonValue
return root;
}
// Load JSON string into this object
void Blur::SetJson(string value) {
// Parse JSON string into JSON objects
Json::Value root;
Json::Reader reader;
bool success = reader.parse( value, root );
if (!success)
// Raise exception
throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
try
{
// Set all values that match
SetJsonValue(root);
}
catch (exception e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
}
}
// Load Json::JsonValue into this object
void Blur::SetJsonValue(Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["horizontal_radius"].isNull())
horizontal_radius.SetJsonValue(root["horizontal_radius"]);
if (!root["vertical_radius"].isNull())
vertical_radius.SetJsonValue(root["vertical_radius"]);
if (!root["sigma"].isNull())
sigma.SetJsonValue(root["sigma"]);
if (!root["iterations"].isNull())
iterations.SetJsonValue(root["iterations"]);
}
// Get all properties for a specific frame
string Blur::PropertiesJSON(int64_t requested_frame) {
// Generate JSON properties list
Json::Value root;
root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
// Keyframes
root["horizontal_radius"] = add_property_json("Horizontal Radius", horizontal_radius.GetValue(requested_frame), "float", "", &horizontal_radius, 0, 100, false, requested_frame);
root["vertical_radius"] = add_property_json("Vertical Radius", vertical_radius.GetValue(requested_frame), "float", "", &vertical_radius, 0, 100, false, requested_frame);
root["sigma"] = add_property_json("Sigma", sigma.GetValue(requested_frame), "float", "", &sigma, 0, 100, false, requested_frame);
root["iterations"] = add_property_json("Iterations", iterations.GetValue(requested_frame), "float", "", &iterations, 0, 100, false, requested_frame);
// Return formatted string
return root.toStyledString();
}
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
openshot::Blur::iterations
Keyframe iterations
Iterations keyframe. The # of blur iterations per pixel. 3 iterations = Gaussian.
Definition: Blur.h:78
openshot::ClipBase::Id
void Id(string value)
Set basic properties.
Definition: ClipBase.h:90
openshot::Blur::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Blur.cpp:259
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::EffectInfoStruct::class_name
string class_name
The class name of the effect.
Definition: EffectBase.h:51
openshot::Blur::Json
string Json()
Get and Set JSON methods.
Definition: Blur.cpp:252
openshot::Keyframe::GetInt
int GetInt(int64_t index)
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:248
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:81
openshot::ClipBase::add_property_json
Json::Value add_property_json(string name, float value, string type, string memo, Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame)
Generate JSON for a property.
Definition: ClipBase.cpp:65
openshot::Blur::PropertiesJSON
string PropertiesJSON(int64_t requested_frame)
Definition: Blur.cpp:314
openshot::Keyframe::GetValue
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
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:152
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:33
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::EffectInfoStruct::description
string description
The description of this effect and what it does.
Definition: EffectBase.h:54
openshot::Keyframe::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:321
openshot::EffectInfoStruct::name
string name
The name of the effect.
Definition: EffectBase.h:53
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:121
openshot::Blur::Blur
Blur()
Blank constructor, useful when using Json to load the effect properties.
Definition: Blur.cpp:33
openshot::Blur::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Blur.cpp:297
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 Wipe Tests.e
e
Definition: OpenShot Wipe Tests.py:28
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
openshot::Keyframe::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:362
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:63
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::Blur::SetJson
void SetJson(string value)
Load JSON string into this object.
Definition: Blur.cpp:274