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();