OpenShot Library | libopenshot  0.2.5
Pixelate.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Pixelate effect class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "../../include/effects/Pixelate.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Pixelate::Pixelate() : pixelization(0.7), left(0.0), top(0.0), right(0.0), bottom(0.0) {
37  // Init effect properties
38  init_effect_details();
39 }
40 
41 // Default constructor
43  pixelization(pixelization), left(left), top(top), right(right), bottom(bottom)
44 {
45  // Init effect properties
46  init_effect_details();
47 }
48 
49 // Init effect settings
50 void Pixelate::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Pixelate";
57  info.name = "Pixelate";
58  info.description = "Pixelate (increase or decrease) the number of visible pixels.";
59  info.has_audio = false;
60  info.has_video = true;
61 }
62 
63 // This method is required for all derived classes of EffectBase, and returns a
64 // modified openshot::Frame object
65 std::shared_ptr<Frame> Pixelate::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
66 {
67  // Get the frame's image
68  std::shared_ptr<QImage> frame_image = frame->GetImage();
69 
70  // Get current keyframe values
71  double pixelization_value = 1.0 - std::min(fabs(pixelization.GetValue(frame_number)), 1.0);
72  double left_value = left.GetValue(frame_number);
73  double top_value = top.GetValue(frame_number);
74  double right_value = right.GetValue(frame_number);
75  double bottom_value = bottom.GetValue(frame_number);
76 
77  if (pixelization_value > 0.0) {
78  int w = frame_image->width();
79  int h = frame_image->height();
80 
81  // Define area we're working on in terms of a QRect with QMargins applied
82  QRect area(QPoint(0,0), frame_image->size());
83  area = area.marginsRemoved({int(left_value * w), int(top_value * h), int(right_value * w), int(bottom_value * h)});
84 
85  // Copy and scale pixels in area to be pixelated
86  auto frame_scaled = frame_image->copy(area).scaledToWidth(area.width() * pixelization_value, Qt::SmoothTransformation);
87 
88  // Draw pixelated image back over original
89  QPainter painter(frame_image.get());
90  painter.drawImage(area, frame_scaled);
91  painter.end();
92  }
93 
94  // return the modified frame
95  return frame;
96 }
97 
98 // Generate JSON string of this object
99 std::string Pixelate::Json() const {
100 
101  // Return formatted string
102  return JsonValue().toStyledString();
103 }
104 
105 // Generate Json::Value for this object
106 Json::Value Pixelate::JsonValue() const {
107 
108  // Create root json object
109  Json::Value root = EffectBase::JsonValue(); // get parent properties
110  root["type"] = info.class_name;
111  root["pixelization"] = pixelization.JsonValue();
112  root["left"] = left.JsonValue();
113  root["top"] = top.JsonValue();
114  root["right"] = right.JsonValue();
115  root["bottom"] = bottom.JsonValue();
116 
117  // return JsonValue
118  return root;
119 }
120 
121 // Load JSON string into this object
122 void Pixelate::SetJson(const std::string value) {
123 
124  // Parse JSON string into JSON objects
125  try
126  {
127  const Json::Value root = openshot::stringToJson(value);
128  // Set all values that match
129  SetJsonValue(root);
130  }
131  catch (const std::exception& e)
132  {
133  // Error parsing JSON (or missing keys)
134  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
135  }
136 }
137 
138 // Load Json::Value into this object
139 void Pixelate::SetJsonValue(const Json::Value root) {
140 
141  // Set parent data
143 
144  // Set data from Json (if key is found)
145  if (!root["pixelization"].isNull())
146  pixelization.SetJsonValue(root["pixelization"]);
147  if (!root["left"].isNull())
148  left.SetJsonValue(root["left"]);
149  if (!root["top"].isNull())
150  top.SetJsonValue(root["top"]);
151  if (!root["right"].isNull())
152  right.SetJsonValue(root["right"]);
153  if (!root["bottom"].isNull())
154  bottom.SetJsonValue(root["bottom"]);
155 }
156 
157 // Get all properties for a specific frame
158 std::string Pixelate::PropertiesJSON(int64_t requested_frame) const {
159 
160  // Generate JSON properties list
161  Json::Value root;
162  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
163  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
164  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
165  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
166  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
167  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
168 
169  // Keyframes
170  root["pixelization"] = add_property_json("Pixelization", pixelization.GetValue(requested_frame), "float", "", &pixelization, 0.0, 0.9999, false, requested_frame);
171  root["left"] = add_property_json("Left Margin", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
172  root["top"] = add_property_json("Top Margin", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
173  root["right"] = add_property_json("Right Margin", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
174  root["bottom"] = add_property_json("Bottom Margin", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
175 
176  // Return formatted string
177  return root.toStyledString();
178 }
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Pixelate.cpp:139
Pixelate()
Blank constructor, useful when using Json to load the effect properties.
Definition: Pixelate.cpp:36
Keyframe bottom
Size of bottom margin.
Definition: Pixelate.h:65
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
Keyframe pixelization
Amount of pixelization.
Definition: Pixelate.h:61
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number)
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Definition: Pixelate.cpp:65
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
Keyframe right
Size of right margin.
Definition: Pixelate.h:64
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Pixelate.cpp:158
Keyframe top
Size of top margin.
Definition: Pixelate.h:63
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
std::string name
The name of the effect.
Definition: EffectBase.h:53
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Pixelate.cpp:122
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
This namespace is the default namespace for all code in the openshot library.
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Pixelate.cpp:106
Exception for invalid JSON.
Definition: Exceptions.h:205
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262
std::string Json() const override
Get and Set JSON methods.
Definition: Pixelate.cpp:99
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64
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:68
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
Keyframe left
Size of left margin.
Definition: Pixelate.h:62
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73