OpenShot Library | libopenshot  0.3.1
Crop.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 "Crop.h"
14 #include "Exceptions.h"
15 #include "KeyFrame.h"
16 
17 #include <QImage>
18 #include <QPainter>
19 #include <QRectF>
20 #include <QRect>
21 #include <QSize>
22 
23 using namespace openshot;
24 
26 Crop::Crop() : Crop::Crop(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) {}
27 
31  Keyframe x, Keyframe y) :
32  left(left), top(top), right(right), bottom(bottom), x(x), y(y)
33 {
34  // Init effect properties
35  init_effect_details();
36 }
37 
38 // Init effect settings
39 void Crop::init_effect_details()
40 {
43 
45  info.class_name = "Crop";
46  info.name = "Crop";
47  info.description = "Crop out any part of your video.";
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> Crop::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
55 {
56  // Get the frame's image
57  std::shared_ptr<QImage> frame_image = frame->GetImage();
58 
59  // Get current keyframe values
60  double left_value = left.GetValue(frame_number);
61  double top_value = top.GetValue(frame_number);
62  double right_value = right.GetValue(frame_number);
63  double bottom_value = bottom.GetValue(frame_number);
64 
65  // Get the current shift amount
66  double x_shift = x.GetValue(frame_number);
67  double y_shift = y.GetValue(frame_number);
68 
69  QSize sz = frame_image->size();
70 
71  // Compute destination rectangle to paint into
72  QRectF paint_r(
73  left_value * sz.width(), top_value * sz.height(),
74  std::max(0.0, 1.0 - left_value - right_value) * sz.width(),
75  std::max(0.0, 1.0 - top_value - bottom_value) * sz.height());
76 
77  // Copy rectangle is destination translated by offsets
78  QRectF copy_r = paint_r;
79  copy_r.translate(x_shift * sz.width(), y_shift * sz.height());
80 
81  // Constrain offset copy rect to stay within image borders
82  if (copy_r.left() < 0) {
83  paint_r.setLeft(paint_r.left() - copy_r.left());
84  copy_r.setLeft(0);
85  }
86  if (copy_r.right() > sz.width()) {
87  paint_r.setRight(paint_r.right() - (copy_r.right() - sz.width()));
88  copy_r.setRight(sz.width());
89  }
90  if (copy_r.top() < 0) {
91  paint_r.setTop(paint_r.top() - copy_r.top());
92  copy_r.setTop(0);
93  }
94  if (copy_r.bottom() > sz.height()) {
95  paint_r.setBottom(paint_r.bottom() - (copy_r.bottom() - sz.height()));
96  copy_r.setBottom(sz.height());
97  }
98 
99  QImage cropped(sz, QImage::Format_RGBA8888_Premultiplied);
100  cropped.fill(Qt::transparent);
101 
102  const QImage src(*frame_image);
103 
104  QPainter p(&cropped);
105  p.drawImage(paint_r, src, copy_r);
106  p.end();
107 
108  // Set frame image
109  frame->AddImage(std::make_shared<QImage>(cropped.copy()));
110 
111  // return the modified frame
112  return frame;
113 }
114 
115 // Generate JSON string of this object
116 std::string Crop::Json() const {
117 
118  // Return formatted string
119  return JsonValue().toStyledString();
120 }
121 
122 // Generate Json::Value for this object
123 Json::Value Crop::JsonValue() const {
124 
125  // Create root json object
126  Json::Value root = EffectBase::JsonValue(); // get parent properties
127  root["type"] = info.class_name;
128  root["left"] = left.JsonValue();
129  root["top"] = top.JsonValue();
130  root["right"] = right.JsonValue();
131  root["bottom"] = bottom.JsonValue();
132  root["x"] = x.JsonValue();
133  root["y"] = y.JsonValue();
134 
135  // return JsonValue
136  return root;
137 }
138 
139 // Load JSON string into this object
140 void Crop::SetJson(const std::string value) {
141 
142  // Parse JSON string into JSON objects
143  try
144  {
145  const Json::Value root = openshot::stringToJson(value);
146  // Set all values that match
147  SetJsonValue(root);
148  }
149  catch (const std::exception& e)
150  {
151  // Error parsing JSON (or missing keys)
152  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
153  }
154 }
155 
156 // Load Json::Value into this object
157 void Crop::SetJsonValue(const Json::Value root) {
158 
159  // Set parent data
161 
162  // Set data from Json (if key is found)
163  if (!root["left"].isNull())
164  left.SetJsonValue(root["left"]);
165  if (!root["top"].isNull())
166  top.SetJsonValue(root["top"]);
167  if (!root["right"].isNull())
168  right.SetJsonValue(root["right"]);
169  if (!root["bottom"].isNull())
170  bottom.SetJsonValue(root["bottom"]);
171  if (!root["x"].isNull())
172  x.SetJsonValue(root["x"]);
173  if (!root["y"].isNull())
174  y.SetJsonValue(root["y"]);
175 }
176 
177 // Get all properties for a specific frame
178 std::string Crop::PropertiesJSON(int64_t requested_frame) const {
179 
180  // Generate JSON properties list
181  Json::Value root;
182  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
183  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
184  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
185  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
186  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
187  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
188 
189  // Keyframes
190  root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
191  root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
192  root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
193  root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
194  root["x"] = add_property_json("X Offset", x.GetValue(requested_frame), "float", "", &x, -1.0, 1.0, false, requested_frame);
195  root["y"] = add_property_json("Y Offset", y.GetValue(requested_frame), "float", "", &y, -1.0, 1.0, false, requested_frame);
196 
197  // Set the parent effect which properties this effect will inherit
198  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
199 
200  // Return formatted string
201  return root.toStyledString();
202 }
Keyframe top
Size of top bar.
Definition: Crop.h:46
This class crops a frame image (from any side), and can be animated with openshot::Keyframe curves ov...
Definition: Crop.h:37
Keyframe y
Y-offset.
Definition: Crop.h:50
Keyframe right
Size of right bar.
Definition: Crop.h:47
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Crop.cpp:140
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
Keyframe x
X-offset.
Definition: Crop.h:49
Keyframe left
Size of left bar.
Definition: Crop.h:45
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
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Crop.cpp:123
Header file for the Keyframe class.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Crop.cpp:157
Header file for Crop 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: Crop.h:73
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
Header file for all Exception classes.
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Crop.cpp:178
Keyframe bottom
Size of bottom bar.
Definition: Crop.h:48
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
std::string Json() const override
Generate JSON string of this object.
Definition: Crop.cpp:116
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
Crop()
Blank constructor, useful when using Json to load the effect properties.
Definition: Crop.cpp:26
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
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
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
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69