OpenShot Library | libopenshot 1.1.1
Loading...
Searching...
No Matches
Pixelate.cpp
Go to the documentation of this file.
1
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include "Pixelate.h"
14#include "Exceptions.h"
15#include "Json.h"
16
17#include <QImage>
18#include <QPainter>
19#include <QRect>
20#include <QPoint>
21
22#include <algorithm>
23
24using namespace openshot;
25
26namespace {
27double clamp_margin(double value) {
28 if (value < 0.0)
29 return 0.0;
30 if (value > 1.0)
31 return 1.0;
32 return value;
33}
34}
35
37Pixelate::Pixelate() : pixelization(0.5), left(0.0), top(0.0), right(0.0), bottom(0.0),
39 // Init effect properties
40 init_effect_details();
41}
42
43// Default constructor
44Pixelate::Pixelate(Keyframe pixelization, Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
45 pixelization(pixelization), left(left), top(top), right(right), bottom(bottom),
47{
48 // Init effect properties
49 init_effect_details();
50}
51
52// Init effect settings
53void Pixelate::init_effect_details()
54{
57
59 info.class_name = "Pixelate";
60 info.name = "Pixelate";
61 info.description = "Pixelate (increase or decrease) the number of visible pixels.";
62 info.has_audio = false;
63 info.has_video = true;
64}
65
66// This method is required for all derived classes of EffectBase, and returns a
67// modified openshot::Frame object
68std::shared_ptr<openshot::Frame>
69Pixelate::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
70{
71 // Get the frame's image
72 std::shared_ptr<QImage> frame_image = frame->GetImage();
73
74 // Get current keyframe values
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);
80
81 if (pixelization_value > 0.0) {
82 int w = frame_image->width();
83 int h = frame_image->height();
84 if (w <= 0 || h <= 0)
85 return frame;
86
87 // Define area we're working on in terms of a QRect with QMargins applied
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)
94 });
95 area = area.intersected(QRect(QPoint(0,0), frame_image->size()));
96 if (area.isEmpty())
97 return frame;
98
99 int scale_to = (int) (area.width() * pixelization_value);
100 if (scale_to < 1) {
101 scale_to = 1; // Not less than one pixel
102 }
103 // Copy and scale pixels in area to be pixelated
104 auto frame_scaled = frame_image->copy(area).scaledToWidth(scale_to, Qt::SmoothTransformation);
105
106 // Draw pixelated image back over original
107 QPainter painter(frame_image.get());
108 painter.drawImage(area, frame_scaled);
109 painter.end();
110 }
111
112 // return the modified frame
113 return frame;
114}
115
116bool Pixelate::UseCustomMaskBlend(int64_t frame_number) const {
117 (void) frame_number;
119}
120
121void Pixelate::ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
122 std::shared_ptr<QImage> mask_image, int64_t frame_number) const {
123 (void) frame_number;
124 if (!original_image || !effected_image || !mask_image)
125 return;
126 if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
127 return;
128
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();
133
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;
138 if (mask_invert)
139 factor = 1.0f - factor;
140 factor = factor * factor;
141 const float inverse = 1.0f - factor;
142
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];
150 }
151}
152
153// Generate JSON string of this object
154std::string Pixelate::Json() const {
155
156 // Return formatted string
157 return JsonValue().toStyledString();
158}
159
160// Generate Json::Value for this object
161Json::Value Pixelate::JsonValue() const {
162
163 // Create root json object
164 Json::Value root = EffectBase::JsonValue(); // get parent properties
165 root["type"] = info.class_name;
166 root["pixelization"] = pixelization.JsonValue();
167 root["left"] = left.JsonValue();
168 root["top"] = top.JsonValue();
169 root["right"] = right.JsonValue();
170 root["bottom"] = bottom.JsonValue();
171 root["mask_mode"] = mask_mode;
172
173 // return JsonValue
174 return root;
175}
176
177// Load JSON string into this object
178void Pixelate::SetJson(const std::string value) {
179
180 // Parse JSON string into JSON objects
181 try
182 {
183 const Json::Value root = openshot::stringToJson(value);
184 // Set all values that match
185 SetJsonValue(root);
186 }
187 catch (const std::exception& e)
188 {
189 // Error parsing JSON (or missing keys)
190 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
191 }
192}
193
194// Load Json::Value into this object
195void Pixelate::SetJsonValue(const Json::Value root) {
196
197 // Set parent data
199
200 // Set data from Json (if key is found)
201 if (!root["pixelization"].isNull())
202 pixelization.SetJsonValue(root["pixelization"]);
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())
212 mask_mode = root["mask_mode"].asInt();
213}
214
215// Get all properties for a specific frame
216std::string Pixelate::PropertiesJSON(int64_t requested_frame) const {
217
218 // Generate JSON properties list
219 Json::Value root = BasePropertiesJSON(requested_frame);
220
221 // Keyframes
222 root["pixelization"] = add_property_json("Pixelization", pixelization.GetValue(requested_frame), "float", "", &pixelization, 0.0, 0.9999, false, requested_frame);
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);
227 root["mask_mode"] = add_property_json("Mask Mode", mask_mode, "int", "", NULL, 0, 1, false, requested_frame);
228 root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", PIXELATE_MASK_LIMIT_TO_AREA, mask_mode));
229 root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", PIXELATE_MASK_VARY_STRENGTH, mask_mode));
230
231 // Return formatted string
232 return root.toStyledString();
233}
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)
Definition ClipBase.cpp:136
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.
Definition ClipBase.cpp:100
bool mask_invert
Invert grayscale mask values before blending.
Definition EffectBase.h:115
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.
Definition EffectBase.h:114
Exception for invalid JSON.
Definition Exceptions.h:224
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition KeyFrame.h:53
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition KeyFrame.cpp:372
double GetValue(int64_t index) const
Get the value at a specific index.
Definition KeyFrame.cpp:258
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition KeyFrame.cpp:339
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
Definition Pixelate.cpp:116
Pixelate()
Default constructor, useful when using Json to load the effect properties.
Definition Pixelate.cpp:37
int mask_mode
Mask behavior mode for this effect.
Definition Pixelate.h:55
Keyframe right
Size of right margin.
Definition Pixelate.h:53
std::string Json() const override
Generate JSON string of this object.
Definition Pixelate.cpp:154
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Pixelate.cpp:216
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...
Definition Pixelate.h:76
Keyframe pixelization
Amount of pixelization.
Definition Pixelate.h:50
Keyframe left
Size of left margin.
Definition Pixelate.h:51
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Pixelate.cpp:161
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Pixelate.cpp:178
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.
Definition Pixelate.cpp:121
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Pixelate.cpp:195
Keyframe top
Size of top margin.
Definition Pixelate.h:52
Keyframe bottom
Size of bottom margin.
Definition Pixelate.h:54
This namespace is the default namespace for all code in the openshot library.
@ PIXELATE_MASK_VARY_STRENGTH
Definition Pixelate.h:29
@ PIXELATE_MASK_LIMIT_TO_AREA
Definition Pixelate.h:28
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:43
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:44
std::string class_name
The class name of the effect.
Definition EffectBase.h:39
std::string name
The name of the effect.
Definition EffectBase.h:40
std::string description
The description of this effect and what it does.
Definition EffectBase.h:41