OpenShot Library | libopenshot  0.3.3
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 
29  Keyframe left, Keyframe top,
30  Keyframe right, Keyframe bottom,
31  Keyframe x, Keyframe y) :
32  left(left), top(top), right(right), bottom(bottom), x(x), y(y), resize(false)
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  QPainter p(&cropped);
103  p.drawImage(paint_r, *frame_image, copy_r);
104  p.end();
105 
106  if (resize) {
107  // Resize image to match cropped QRect (reduce frame size)
108  frame->AddImage(std::make_shared<QImage>(cropped.copy(paint_r.toRect())));
109  } else {
110  // Copy cropped image into transparent frame image (maintain frame size)
111  frame->AddImage(std::make_shared<QImage>(cropped.copy()));
112  }
113 
114  // return the modified frame
115  return frame;
116 }
117 
118 // Generate JSON string of this object
119 std::string Crop::Json() const {
120 
121  // Return formatted string
122  return JsonValue().toStyledString();
123 }
124 
125 // Generate Json::Value for this object
126 Json::Value Crop::JsonValue() const {
127 
128  // Create root json object
129  Json::Value root = EffectBase::JsonValue(); // get parent properties
130  root["type"] = info.class_name;
131  root["left"] = left.JsonValue();
132  root["top"] = top.JsonValue();
133  root["right"] = right.JsonValue();
134  root["bottom"] = bottom.JsonValue();
135  root["x"] = x.JsonValue();
136  root["y"] = y.JsonValue();
137  root["resize"] = resize;
138 
139  // return JsonValue
140  return root;
141 }
142 
143 // Load JSON string into this object
144 void Crop::SetJson(const std::string value) {
145 
146  // Parse JSON string into JSON objects
147  try
148  {
149  const Json::Value root = openshot::stringToJson(value);
150  // Set all values that match
151  SetJsonValue(root);
152  }
153  catch (const std::exception& e)
154  {
155  // Error parsing JSON (or missing keys)
156  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
157  }
158 }
159 
160 // Load Json::Value into this object
161 void Crop::SetJsonValue(const Json::Value root) {
162 
163  // Set parent data
165 
166  // Set data from Json (if key is found)
167  if (!root["left"].isNull())
168  left.SetJsonValue(root["left"]);
169  if (!root["top"].isNull())
170  top.SetJsonValue(root["top"]);
171  if (!root["right"].isNull())
172  right.SetJsonValue(root["right"]);
173  if (!root["bottom"].isNull())
174  bottom.SetJsonValue(root["bottom"]);
175  if (!root["x"].isNull())
176  x.SetJsonValue(root["x"]);
177  if (!root["y"].isNull())
178  y.SetJsonValue(root["y"]);
179  if (!root["resize"].isNull())
180  resize = root["resize"].asBool();
181 }
182 
183 // Get all properties for a specific frame
184 std::string Crop::PropertiesJSON(int64_t requested_frame) const {
185 
186  // Generate JSON properties list
187  Json::Value root = BasePropertiesJSON(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  // Add replace_image choices (dropdown style)
198  root["resize"] = add_property_json("Resize Image", resize, "int", "", NULL, 0, 1, false, requested_frame);
199  root["resize"]["choices"].append(add_property_choice_json("Yes", true, resize));
200  root["resize"]["choices"].append(add_property_choice_json("No", false, resize));
201 
202  // Return formatted string
203  return root.toStyledString();
204 }
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::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Crop::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Crop.cpp:184
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot::Crop::right
Keyframe right
Size of right bar.
Definition: Crop.h:47
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Crop::bottom
Keyframe bottom
Size of bottom bar.
Definition: Crop.h:48
openshot::ClipBase::add_property_choice_json
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
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Crop::left
Keyframe left
Size of left bar.
Definition: Crop.h:45
openshot::Crop::y
Keyframe y
Y-offset.
Definition: Crop.h:50
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
KeyFrame.h
Header file for the Keyframe class.
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::Crop::x
Keyframe x
X-offset.
Definition: Crop.h:49
openshot::Crop::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Crop.cpp:161
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::Crop::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: Crop.h:74
openshot::Crop::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Crop.cpp:119
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Crop
This class crops a frame image (from any side), and can be animated with openshot::Keyframe curves ov...
Definition: Crop.h:37
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::Crop::resize
bool resize
Auto-resize image after crop operation.
Definition: Crop.h:51
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::Crop::Crop
Crop()
Blank constructor, useful when using Json to load the effect properties.
Definition: Crop.cpp:26
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Crop::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Crop.cpp:126
Crop.h
Header file for Crop effect class.
openshot::Crop::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Crop.cpp:144
Exceptions.h
Header file for all Exception classes.
openshot::Crop::top
Keyframe top
Size of top bar.
Definition: Crop.h:46
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258