19 Blur::Blur() : horizontal_radius(6.0), vertical_radius(6.0), sigma(3.0), iterations(3.0),
22 init_effect_details();
27 horizontal_radius(new_horizontal_radius), vertical_radius(new_vertical_radius),
31 init_effect_details();
35 void Blur::init_effect_details()
50 std::shared_ptr<openshot::Frame>
Blur::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
53 std::shared_ptr<QImage> frame_image = frame->GetImage();
62 int w = frame_image->width();
63 int h = frame_image->height();
66 QImage image_copy = frame_image->copy();
67 std::shared_ptr<QImage> frame_image_2 = std::make_shared<QImage>(image_copy);
70 for (
int iteration = 0; iteration < iteration_value; ++iteration)
73 if (horizontal_radius_value > 0.0) {
75 boxBlurH(frame_image->bits(), frame_image_2->bits(), w, h, horizontal_radius_value);
78 frame_image.swap(frame_image_2);
82 if (vertical_radius_value > 0.0) {
84 boxBlurT(frame_image->bits(), frame_image_2->bits(), w, h, vertical_radius_value);
87 frame_image.swap(frame_image_2);
101 std::shared_ptr<QImage> mask_image, int64_t frame_number)
const {
103 if (!original_image || !effected_image || !mask_image)
105 if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
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();
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;
118 factor = 1.0f - factor;
120 factor = factor * factor;
121 const float inverse = 1.0f - factor;
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];
136 void Blur::boxBlurH(
unsigned char *scl,
unsigned char *tcl,
int w,
int h,
int r) {
137 float iarr = 1.0 / (r + r + 1);
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];
147 for (
int j = 0; j <= r; ++j) {
148 val += scl[ri++ * 4 + ch] - fv;
149 tcl[ti++ * 4 + ch] = round(val * iarr);
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);
155 for (
int j = w - r; j < w; ++j) {
156 val += lv - scl[li++ * 4 + ch];
157 tcl[ti++ * 4 + ch] = round(val * iarr);
163 void Blur::boxBlurT(
unsigned char *scl,
unsigned char *tcl,
int w,
int h,
int r) {
164 float iarr = 1.0 / (r + r + 1);
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);
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);
185 for (
int j = h - r; j < h; j++) {
186 val += lv - scl[li * 4 + ch];
187 tcl[ti * 4 + ch] = round(val * iarr);
228 catch (
const std::exception& e)
231 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
242 if (!root[
"horizontal_radius"].isNull())
244 if (!root[
"vertical_radius"].isNull())
246 if (!root[
"sigma"].isNull())
248 if (!root[
"iterations"].isNull())
250 if (!root[
"mask_mode"].isNull())
270 return root.toStyledString();