27 double clamp_margin(
double value) {
40 init_effect_details();
45 pixelization(pixelization), left(left), top(top), right(right), bottom(bottom),
49 init_effect_details();
53 void Pixelate::init_effect_details()
61 info.
description =
"Pixelate (increase or decrease) the number of visible pixels.";
68 std::shared_ptr<openshot::Frame>
72 std::shared_ptr<QImage> frame_image = frame->GetImage();
75 double pixelization_value = std::min(pow(0.001, fabs(
pixelization.
GetValue(frame_number))), 1.0);
81 if (pixelization_value > 0.0) {
82 int w = frame_image->width();
83 int h = frame_image->height();
88 QRect area(QPoint(0,0), frame_image->size());
89 area = area.marginsRemoved({
90 int(clamp_margin(left_value) * w),
91 int(clamp_margin(top_value) * h),
92 int(clamp_margin(right_value) * w),
93 int(clamp_margin(bottom_value) * h)
95 area = area.intersected(QRect(QPoint(0,0), frame_image->size()));
99 int scale_to = (int) (area.width() * pixelization_value);
104 auto frame_scaled = frame_image->copy(area).scaledToWidth(scale_to, Qt::SmoothTransformation);
107 QPainter painter(frame_image.get());
108 painter.drawImage(area, frame_scaled);
122 std::shared_ptr<QImage> mask_image, int64_t frame_number)
const {
124 if (!original_image || !effected_image || !mask_image)
126 if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
129 unsigned char* original_pixels =
reinterpret_cast<unsigned char*
>(original_image->bits());
130 unsigned char* effected_pixels =
reinterpret_cast<unsigned char*
>(effected_image->bits());
131 unsigned char* mask_pixels =
reinterpret_cast<unsigned char*
>(mask_image->bits());
132 const int pixel_count = effected_image->width() * effected_image->height();
134 #pragma omp parallel for schedule(static)
135 for (
int i = 0; i < pixel_count; ++i) {
136 const int idx = i * 4;
137 float factor =
static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
139 factor = 1.0f - factor;
140 factor = factor * factor;
141 const float inverse = 1.0f - factor;
143 effected_pixels[idx] =
static_cast<unsigned char>(
144 (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
145 effected_pixels[idx + 1] =
static_cast<unsigned char>(
146 (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
147 effected_pixels[idx + 2] =
static_cast<unsigned char>(
148 (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
149 effected_pixels[idx + 3] = original_pixels[idx + 3];
187 catch (
const std::exception& e)
190 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
201 if (!root[
"pixelization"].isNull())
203 if (!root[
"left"].isNull())
205 if (!root[
"top"].isNull())
207 if (!root[
"right"].isNull())
209 if (!root[
"bottom"].isNull())
211 if (!root[
"mask_mode"].isNull())
232 return root.toStyledString();