OpenShot Library | libopenshot  0.3.1
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 using namespace openshot;
23 
25 Pixelate::Pixelate() : pixelization(0.5), left(0.0), top(0.0), right(0.0), bottom(0.0) {
26  // Init effect properties
27  init_effect_details();
28 }
29 
30 // Default constructor
32  pixelization(pixelization), left(left), top(top), right(right), bottom(bottom)
33 {
34  // Init effect properties
35  init_effect_details();
36 }
37 
38 // Init effect settings
39 void Pixelate::init_effect_details()
40 {
43 
45  info.class_name = "Pixelate";
46  info.name = "Pixelate";
47  info.description = "Pixelate (increase or decrease) the number of visible pixels.";
48  info.has_audio = false;
49  info.has_video = true;
50 }
51 
52 // This method is required for all derived classes of EffectBase, and returns a
53 // modified openshot::Frame object
54 std::shared_ptr<openshot::Frame>
55 Pixelate::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
56 {
57  // Get the frame's image
58  std::shared_ptr<QImage> frame_image = frame->GetImage();
59 
60  // Get current keyframe values
61  double pixelization_value = std::min(pow(0.001, fabs(pixelization.GetValue(frame_number))), 1.0);
62  double left_value = left.GetValue(frame_number);
63  double top_value = top.GetValue(frame_number);
64  double right_value = right.GetValue(frame_number);
65  double bottom_value = bottom.GetValue(frame_number);
66 
67  if (pixelization_value > 0.0) {
68  int w = frame_image->width();
69  int h = frame_image->height();
70 
71  // Define area we're working on in terms of a QRect with QMargins applied
72  QRect area(QPoint(0,0), frame_image->size());
73  area = area.marginsRemoved({int(left_value * w), int(top_value * h), int(right_value * w), int(bottom_value * h)});
74 
75  int scale_to = (int) (area.width() * pixelization_value);
76  if (scale_to < 1) {
77  scale_to = 1; // Not less than one pixel
78  }
79  // Copy and scale pixels in area to be pixelated
80  auto frame_scaled = frame_image->copy(area).scaledToWidth(scale_to, Qt::SmoothTransformation);
81 
82  // Draw pixelated image back over original
83  QPainter painter(frame_image.get());
84  painter.drawImage(area, frame_scaled);
85  painter.end();
86  }
87 
88  // return the modified frame
89  return frame;
90 }
91 
92 // Generate JSON string of this object
93 std::string Pixelate::Json() const {
94 
95  // Return formatted string
96  return JsonValue().toStyledString();
97 }
98 
99 // Generate Json::Value for this object
100 Json::Value Pixelate::JsonValue() const {
101 
102  // Create root json object
103  Json::Value root = EffectBase::JsonValue(); // get parent properties
104  root["type"] = info.class_name;
105  root["pixelization"] = pixelization.JsonValue();
106  root["left"] = left.JsonValue();
107  root["top"] = top.JsonValue();
108  root["right"] = right.JsonValue();
109  root["bottom"] = bottom.JsonValue();
110 
111  // return JsonValue
112  return root;
113 }
114 
115 // Load JSON string into this object
116 void Pixelate::SetJson(const std::string value) {
117 
118  // Parse JSON string into JSON objects
119  try
120  {
121  const Json::Value root = openshot::stringToJson(value);
122  // Set all values that match
123  SetJsonValue(root);
124  }
125  catch (const std::exception& e)
126  {
127  // Error parsing JSON (or missing keys)
128  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
129  }
130 }
131 
132 // Load Json::Value into this object
133 void Pixelate::SetJsonValue(const Json::Value root) {
134 
135  // Set parent data
137 
138  // Set data from Json (if key is found)
139  if (!root["pixelization"].isNull())
140  pixelization.SetJsonValue(root["pixelization"]);
141  if (!root["left"].isNull())
142  left.SetJsonValue(root["left"]);
143  if (!root["top"].isNull())
144  top.SetJsonValue(root["top"]);
145  if (!root["right"].isNull())
146  right.SetJsonValue(root["right"]);
147  if (!root["bottom"].isNull())
148  bottom.SetJsonValue(root["bottom"]);
149 }
150 
151 // Get all properties for a specific frame
152 std::string Pixelate::PropertiesJSON(int64_t requested_frame) const {
153 
154  // Generate JSON properties list
155  Json::Value root;
156  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
157  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
158  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
159  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
160  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
161  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
162 
163  // Keyframes
164  root["pixelization"] = add_property_json("Pixelization", pixelization.GetValue(requested_frame), "float", "", &pixelization, 0.0, 0.9999, false, requested_frame);
165  root["left"] = add_property_json("Left Margin", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
166  root["top"] = add_property_json("Top Margin", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
167  root["right"] = add_property_json("Right Margin", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
168  root["bottom"] = add_property_json("Bottom Margin", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
169 
170  // Set the parent effect which properties this effect will inherit
171  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
172 
173  // Return formatted string
174  return root.toStyledString();
175 }
Pixelate()
Default constructor, useful when using Json to load the effect properties.
Definition: Pixelate.cpp:25
Keyframe bottom
Size of bottom margin.
Definition: Pixelate.h:46
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:85
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:88
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:112
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Pixelate.cpp:116
Keyframe pixelization
Amount of pixelization.
Definition: Pixelate.h:42
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:89
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:77
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
Header file for all Exception classes.
Keyframe right
Size of right margin.
Definition: Pixelate.h:45
Header file for JSON class.
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Pixelate.cpp:152
Keyframe top
Size of top margin.
Definition: Pixelate.h:44
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
std::string name
The name of the effect.
Definition: EffectBase.h:37
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Pixelate.cpp:100
Exception for invalid JSON.
Definition: Exceptions.h:217
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:39
std::string Json() const override
Generate JSON string of this object.
Definition: Pixelate.cpp:93
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:86
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:53
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
Header file for Pixelate effect class.
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:67
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Pixelate.cpp:133
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
Keyframe left
Size of left margin.
Definition: Pixelate.h:43
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69