OpenShot Library | libopenshot  0.3.2
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
31 Pixelate::Pixelate(Keyframe pixelization, Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
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 = BasePropertiesJSON(requested_frame);
156 
157  // Keyframes
158  root["pixelization"] = add_property_json("Pixelization", pixelization.GetValue(requested_frame), "float", "", &pixelization, 0.0, 0.9999, false, requested_frame);
159  root["left"] = add_property_json("Left Margin", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
160  root["top"] = add_property_json("Top Margin", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
161  root["right"] = add_property_json("Right Margin", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
162  root["bottom"] = add_property_json("Bottom Margin", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
163 
164  // Return formatted string
165  return root.toStyledString();
166 }
openshot::ClipBase::add_property_json
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
openshot::Pixelate::GetFrame
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
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot::Pixelate::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Pixelate.cpp:152
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Pixelate::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Pixelate.cpp:133
openshot::Pixelate::pixelization
Keyframe pixelization
Amount of pixelization.
Definition: Pixelate.h:42
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Pixelate::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Pixelate.cpp:93
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
Definition: EffectBase.cpp:179
openshot::Pixelate::bottom
Keyframe bottom
Size of bottom margin.
Definition: Pixelate.h:46
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
openshot::Pixelate::right
Keyframe right
Size of right margin.
Definition: Pixelate.h:45
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::Pixelate::left
Keyframe left
Size of left margin.
Definition: Pixelate.h:43
openshot::Pixelate::Pixelate
Pixelate()
Default constructor, useful when using Json to load the effect properties.
Definition: Pixelate.cpp:25
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Pixelate::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Pixelate.cpp:100
Pixelate.h
Header file for Pixelate effect class.
Json.h
Header file for JSON class.
openshot::Pixelate::top
Keyframe top
Size of top margin.
Definition: Pixelate.h:44
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Pixelate::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Pixelate.cpp:116
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258