OpenShot Library | libopenshot 0.6.0
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
22using namespace openshot;
23
25Pixelate::Pixelate() : pixelization(0.5), left(0.0), top(0.0), right(0.0), bottom(0.0),
27 // Init effect properties
28 init_effect_details();
29}
30
31// Default constructor
32Pixelate::Pixelate(Keyframe pixelization, Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
33 pixelization(pixelization), left(left), top(top), right(right), bottom(bottom),
35{
36 // Init effect properties
37 init_effect_details();
38}
39
40// Init effect settings
41void Pixelate::init_effect_details()
42{
45
47 info.class_name = "Pixelate";
48 info.name = "Pixelate";
49 info.description = "Pixelate (increase or decrease) the number of visible pixels.";
50 info.has_audio = false;
51 info.has_video = true;
52}
53
54// This method is required for all derived classes of EffectBase, and returns a
55// modified openshot::Frame object
56std::shared_ptr<openshot::Frame>
57Pixelate::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
58{
59 // Get the frame's image
60 std::shared_ptr<QImage> frame_image = frame->GetImage();
61
62 // Get current keyframe values
63 double pixelization_value = std::min(pow(0.001, fabs(pixelization.GetValue(frame_number))), 1.0);
64 double left_value = left.GetValue(frame_number);
65 double top_value = top.GetValue(frame_number);
66 double right_value = right.GetValue(frame_number);
67 double bottom_value = bottom.GetValue(frame_number);
68
69 if (pixelization_value > 0.0) {
70 int w = frame_image->width();
71 int h = frame_image->height();
72
73 // Define area we're working on in terms of a QRect with QMargins applied
74 QRect area(QPoint(0,0), frame_image->size());
75 area = area.marginsRemoved({int(left_value * w), int(top_value * h), int(right_value * w), int(bottom_value * h)});
76
77 int scale_to = (int) (area.width() * pixelization_value);
78 if (scale_to < 1) {
79 scale_to = 1; // Not less than one pixel
80 }
81 // Copy and scale pixels in area to be pixelated
82 auto frame_scaled = frame_image->copy(area).scaledToWidth(scale_to, Qt::SmoothTransformation);
83
84 // Draw pixelated image back over original
85 QPainter painter(frame_image.get());
86 painter.drawImage(area, frame_scaled);
87 painter.end();
88 }
89
90 // return the modified frame
91 return frame;
92}
93
94bool Pixelate::UseCustomMaskBlend(int64_t frame_number) const {
95 (void) frame_number;
97}
98
99void Pixelate::ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
100 std::shared_ptr<QImage> mask_image, int64_t frame_number) const {
101 (void) frame_number;
102 if (!original_image || !effected_image || !mask_image)
103 return;
104 if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
105 return;
106
107 unsigned char* original_pixels = reinterpret_cast<unsigned char*>(original_image->bits());
108 unsigned char* effected_pixels = reinterpret_cast<unsigned char*>(effected_image->bits());
109 unsigned char* mask_pixels = reinterpret_cast<unsigned char*>(mask_image->bits());
110 const int pixel_count = effected_image->width() * effected_image->height();
111
112 #pragma omp parallel for schedule(static)
113 for (int i = 0; i < pixel_count; ++i) {
114 const int idx = i * 4;
115 float factor = static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
116 if (mask_invert)
117 factor = 1.0f - factor;
118 factor = factor * factor;
119 const float inverse = 1.0f - factor;
120
121 effected_pixels[idx] = static_cast<unsigned char>(
122 (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
123 effected_pixels[idx + 1] = static_cast<unsigned char>(
124 (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
125 effected_pixels[idx + 2] = static_cast<unsigned char>(
126 (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
127 effected_pixels[idx + 3] = original_pixels[idx + 3];
128 }
129}
130
131// Generate JSON string of this object
132std::string Pixelate::Json() const {
133
134 // Return formatted string
135 return JsonValue().toStyledString();
136}
137
138// Generate Json::Value for this object
139Json::Value Pixelate::JsonValue() const {
140
141 // Create root json object
142 Json::Value root = EffectBase::JsonValue(); // get parent properties
143 root["type"] = info.class_name;
144 root["pixelization"] = pixelization.JsonValue();
145 root["left"] = left.JsonValue();
146 root["top"] = top.JsonValue();
147 root["right"] = right.JsonValue();
148 root["bottom"] = bottom.JsonValue();
149 root["mask_mode"] = mask_mode;
150
151 // return JsonValue
152 return root;
153}
154
155// Load JSON string into this object
156void Pixelate::SetJson(const std::string value) {
157
158 // Parse JSON string into JSON objects
159 try
160 {
161 const Json::Value root = openshot::stringToJson(value);
162 // Set all values that match
163 SetJsonValue(root);
164 }
165 catch (const std::exception& e)
166 {
167 // Error parsing JSON (or missing keys)
168 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
169 }
170}
171
172// Load Json::Value into this object
173void Pixelate::SetJsonValue(const Json::Value root) {
174
175 // Set parent data
177
178 // Set data from Json (if key is found)
179 if (!root["pixelization"].isNull())
180 pixelization.SetJsonValue(root["pixelization"]);
181 if (!root["left"].isNull())
182 left.SetJsonValue(root["left"]);
183 if (!root["top"].isNull())
184 top.SetJsonValue(root["top"]);
185 if (!root["right"].isNull())
186 right.SetJsonValue(root["right"]);
187 if (!root["bottom"].isNull())
188 bottom.SetJsonValue(root["bottom"]);
189 if (!root["mask_mode"].isNull())
190 mask_mode = root["mask_mode"].asInt();
191}
192
193// Get all properties for a specific frame
194std::string Pixelate::PropertiesJSON(int64_t requested_frame) const {
195
196 // Generate JSON properties list
197 Json::Value root = BasePropertiesJSON(requested_frame);
198
199 // Keyframes
200 root["pixelization"] = add_property_json("Pixelization", pixelization.GetValue(requested_frame), "float", "", &pixelization, 0.0, 0.9999, false, requested_frame);
201 root["left"] = add_property_json("Left Margin", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
202 root["top"] = add_property_json("Top Margin", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
203 root["right"] = add_property_json("Right Margin", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
204 root["bottom"] = add_property_json("Bottom Margin", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
205 root["mask_mode"] = add_property_json("Mask Mode", mask_mode, "int", "", NULL, 0, 1, false, requested_frame);
206 root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", PIXELATE_MASK_LIMIT_TO_AREA, mask_mode));
207 root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", PIXELATE_MASK_VARY_STRENGTH, mask_mode));
208
209 // Return formatted string
210 return root.toStyledString();
211}
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:132
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:96
bool mask_invert
Invert grayscale mask values before blending.
Definition EffectBase.h:111
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:110
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:94
Pixelate()
Default constructor, useful when using Json to load the effect properties.
Definition Pixelate.cpp:25
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:132
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Pixelate.cpp:194
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:139
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Pixelate.cpp:156
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:99
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Pixelate.cpp:173
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.
Definition Compressor.h:29
@ 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