OpenShot Library | libopenshot  0.2.5
Crop.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Crop 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/Crop.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Crop::Crop() : left(0.1), top(0.1), right(0.1), bottom(0.1) {
37  // Init effect properties
38  init_effect_details();
39 }
40 
41 // Default constructor
42 Crop::Crop(Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
43  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 Crop::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Crop";
57  info.name = "Crop";
58  info.description = "Crop out any part of your video.";
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> Crop::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 transparent color (and create small transparent image)
71  std::shared_ptr<QImage> tempColor = std::shared_ptr<QImage>(new QImage(frame_image->width(), 1, QImage::Format_RGBA8888));
72  tempColor->fill(QColor(QString::fromStdString("transparent")));
73 
74  // Get current keyframe values
75  double left_value = left.GetValue(frame_number);
76  double top_value = top.GetValue(frame_number);
77  double right_value = right.GetValue(frame_number);
78  double bottom_value = bottom.GetValue(frame_number);
79 
80  // Get pixel array pointers
81  unsigned char *pixels = (unsigned char *) frame_image->bits();
82  unsigned char *color_pixels = (unsigned char *) tempColor->bits();
83 
84  // Get pixels sizes of all crop sides
85  int top_bar_height = top_value * frame_image->height();
86  int bottom_bar_height = bottom_value * frame_image->height();
87  int left_bar_width = left_value * frame_image->width();
88  int right_bar_width = right_value * frame_image->width();
89 
90  // Loop through rows
91  for (int row = 0; row < frame_image->height(); row++) {
92 
93  // Top & Bottom Crop
94  if ((top_bar_height > 0.0 && row <= top_bar_height) || (bottom_bar_height > 0.0 && row >= frame_image->height() - bottom_bar_height)) {
95  memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * frame_image->width() * 4);
96  } else {
97  // Left Crop
98  if (left_bar_width > 0.0) {
99  memcpy(&pixels[row * frame_image->width() * 4], color_pixels, sizeof(char) * left_bar_width * 4);
100  }
101 
102  // Right Crop
103  if (right_bar_width > 0.0) {
104  memcpy(&pixels[((row * frame_image->width()) + (frame_image->width() - right_bar_width)) * 4], color_pixels, sizeof(char) * right_bar_width * 4);
105  }
106  }
107  }
108 
109  // Cleanup colors and arrays
110  tempColor.reset();
111 
112  // return the modified frame
113  return frame;
114 }
115 
116 // Generate JSON string of this object
117 std::string Crop::Json() const {
118 
119  // Return formatted string
120  return JsonValue().toStyledString();
121 }
122 
123 // Generate Json::Value for this object
124 Json::Value Crop::JsonValue() const {
125 
126  // Create root json object
127  Json::Value root = EffectBase::JsonValue(); // get parent properties
128  root["type"] = info.class_name;
129  root["left"] = left.JsonValue();
130  root["top"] = top.JsonValue();
131  root["right"] = right.JsonValue();
132  root["bottom"] = bottom.JsonValue();
133 
134  // return JsonValue
135  return root;
136 }
137 
138 // Load JSON string into this object
139 void Crop::SetJson(const std::string value) {
140 
141  // Parse JSON string into JSON objects
142  try
143  {
144  const Json::Value root = openshot::stringToJson(value);
145  // Set all values that match
146  SetJsonValue(root);
147  }
148  catch (const std::exception& e)
149  {
150  // Error parsing JSON (or missing keys)
151  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
152  }
153 }
154 
155 // Load Json::Value into this object
156 void Crop::SetJsonValue(const Json::Value root) {
157 
158  // Set parent data
160 
161  // Set data from Json (if key is found)
162  if (!root["left"].isNull())
163  left.SetJsonValue(root["left"]);
164  if (!root["top"].isNull())
165  top.SetJsonValue(root["top"]);
166  if (!root["right"].isNull())
167  right.SetJsonValue(root["right"]);
168  if (!root["bottom"].isNull())
169  bottom.SetJsonValue(root["bottom"]);
170 }
171 
172 // Get all properties for a specific frame
173 std::string Crop::PropertiesJSON(int64_t requested_frame) const {
174 
175  // Generate JSON properties list
176  Json::Value root;
177  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
178  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
179  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
180  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
181  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
182  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
183 
184  // Keyframes
185  root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
186  root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
187  root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
188  root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
189 
190  // Return formatted string
191  return root.toStyledString();
192 }
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
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
Keyframe right
Size of right bar.
Definition: Crop.h:65
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Crop.cpp:173
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Crop.cpp:124
Keyframe left
Size of left bar.
Definition: Crop.h:63
Crop()
Blank constructor, useful when using Json to load the effect properties.
Definition: Crop.cpp:36
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Crop.cpp:156
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: Crop.cpp:65
Keyframe bottom
Size of bottom bar.
Definition: Crop.h:66
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Crop.cpp:139
Keyframe top
Size of top bar.
Definition: Crop.h:64
std::string Json() const override
Get and Set JSON methods.
Definition: Crop.cpp:117
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
Exception for invalid JSON.
Definition: Exceptions.h:206
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:64
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
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
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54