OpenShot Library | libopenshot  0.2.4
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
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() {
118 
119  // Return formatted string
120  return JsonValue().toStyledString();
121 }
122 
123 // Generate Json::JsonValue for this object
124 Json::Value Crop::JsonValue() {
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(std::string value) {
140 
141  // Parse JSON string into JSON objects
142  Json::Value root;
143  Json::CharReaderBuilder rbuilder;
144  Json::CharReader* reader(rbuilder.newCharReader());
145 
146  std::string errors;
147  bool success = reader->parse( value.c_str(),
148  value.c_str() + value.size(), &root, &errors );
149  delete reader;
150 
151  if (!success)
152  // Raise exception
153  throw InvalidJSON("JSON could not be parsed (or is invalid)");
154 
155  try
156  {
157  // Set all values that match
158  SetJsonValue(root);
159  }
160  catch (const std::exception& e)
161  {
162  // Error parsing JSON (or missing keys)
163  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
164  }
165 }
166 
167 // Load Json::JsonValue into this object
168 void Crop::SetJsonValue(Json::Value root) {
169 
170  // Set parent data
172 
173  // Set data from Json (if key is found)
174  if (!root["left"].isNull())
175  left.SetJsonValue(root["left"]);
176  if (!root["top"].isNull())
177  top.SetJsonValue(root["top"]);
178  if (!root["right"].isNull())
179  right.SetJsonValue(root["right"]);
180  if (!root["bottom"].isNull())
181  bottom.SetJsonValue(root["bottom"]);
182 }
183 
184 // Get all properties for a specific frame
185 std::string Crop::PropertiesJSON(int64_t requested_frame) {
186 
187  // Generate JSON properties list
188  Json::Value root;
189  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
190  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
191  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
192  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
193  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
194  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
195 
196  // Keyframes
197  root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
198  root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
199  root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
200  root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
201 
202  // Return formatted string
203  return root.toStyledString();
204 }
Keyframe top
Size of top bar.
Definition: Crop.h:64
Keyframe right
Size of right bar.
Definition: Crop.h:65
Keyframe left
Size of left bar.
Definition: Crop.h:63
float End()
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:84
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:374
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
std::string Json()
Get and Set JSON methods.
Definition: Crop.cpp:117
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Crop.cpp:168
Keyframe bottom
Size of bottom bar.
Definition: Crop.h:66
std::string Id()
Get basic properties.
Definition: ClipBase.h:76
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
void SetJson(std::string value)
Load JSON string into this object.
Definition: Crop.cpp:139
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
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:129
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 PropertiesJSON(int64_t requested_frame)
Definition: Crop.cpp:185
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame)
Generate JSON for a property.
Definition: ClipBase.cpp:68
Crop()
Blank constructor, useful when using Json to load the effect properties.
Definition: Crop.cpp:36
This namespace is the default namespace for all code in the openshot library.
Json::Value JsonValue() const
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:329
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Crop.cpp:124
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
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
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64
float Duration()
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73