27double clamp_margin(
double value) {
40 init_effect_details();
49 init_effect_details();
53void Pixelate::init_effect_details()
61 info.
description =
"Pixelate (increase or decrease) the number of visible pixels.";
68std::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);
76 double left_value =
left.GetValue(frame_number);
77 double top_value =
top.GetValue(frame_number);
78 double right_value =
right.GetValue(frame_number);
79 double bottom_value =
bottom.GetValue(frame_number);
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];
165 root[
"type"] =
info.class_name;
167 root[
"left"] =
left.JsonValue();
168 root[
"top"] =
top.JsonValue();
169 root[
"right"] =
right.JsonValue();
170 root[
"bottom"] =
bottom.JsonValue();
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())
204 left.SetJsonValue(root[
"left"]);
205 if (!root[
"top"].isNull())
206 top.SetJsonValue(root[
"top"]);
207 if (!root[
"right"].isNull())
208 right.SetJsonValue(root[
"right"]);
209 if (!root[
"bottom"].isNull())
210 bottom.SetJsonValue(root[
"bottom"]);
211 if (!root[
"mask_mode"].isNull())
223 root[
"left"] =
add_property_json(
"Margin: Left",
left.GetValue(requested_frame),
"float",
"", &
left, 0.0, 1.0,
false, requested_frame);
224 root[
"top"] =
add_property_json(
"Margin: Top",
top.GetValue(requested_frame),
"float",
"", &
top, 0.0, 1.0,
false, requested_frame);
225 root[
"right"] =
add_property_json(
"Margin: Right",
right.GetValue(requested_frame),
"float",
"", &
right, 0.0, 1.0,
false, requested_frame);
226 root[
"bottom"] =
add_property_json(
"Margin: Bottom",
bottom.GetValue(requested_frame),
"float",
"", &
bottom, 0.0, 1.0,
false, requested_frame);
232 return root.toStyledString();
Header file for all Exception classes.
Header file for JSON class.
Header file for Pixelate effect class.
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.
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
Pixelate()
Default constructor, useful when using Json to load the effect properties.
int mask_mode
Mask behavior mode for this effect.
Keyframe right
Size of right margin.
std::string Json() const override
Generate JSON string of this object.
std::string PropertiesJSON(int64_t requested_frame) const override
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...
Keyframe pixelization
Amount of pixelization.
Keyframe left
Size of left margin.
Json::Value JsonValue() const override
Generate Json::Value for this object.
void SetJson(const std::string value) override
Load JSON string into this object.
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.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Keyframe top
Size of top margin.
Keyframe bottom
Size of bottom margin.
This namespace is the default namespace for all code in the openshot library.
@ PIXELATE_MASK_VARY_STRENGTH
@ PIXELATE_MASK_LIMIT_TO_AREA
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.