OpenShot Library | libopenshot 0.2.7
Crop.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for Crop effect class (cropping any side, with x/y offsets)
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 "Crop.h"
32#include "Exceptions.h"
33
34using namespace openshot;
35
36/// Blank constructor, useful when using Json to load the effect properties
37Crop::Crop() : left(0.0), top(0.0), right(0.0), bottom(0.0), x(0.0), y(0.0) {
38 // Init effect properties
39 init_effect_details();
40}
41
42// Default constructor
43Crop::Crop(Keyframe left, Keyframe top, Keyframe right, Keyframe bottom) :
44 left(left), top(top), right(right), bottom(bottom), x(0.0), y(0.0)
45{
46 // Init effect properties
47 init_effect_details();
48}
49
50// Init effect settings
51void Crop::init_effect_details()
52{
53 /// Initialize the values of the EffectInfo struct.
55
56 /// Set the effect info
57 info.class_name = "Crop";
58 info.name = "Crop";
59 info.description = "Crop out any part of your video.";
60 info.has_audio = false;
61 info.has_video = true;
62}
63
64// This method is required for all derived classes of EffectBase, and returns a
65// modified openshot::Frame object
66std::shared_ptr<openshot::Frame> Crop::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
67{
68 // Get the frame's image
69 std::shared_ptr<QImage> frame_image = frame->GetImage();
70
71 // Get transparent color target image (which will become the cropped image)
72 auto cropped_image = std::make_shared<QImage>(
73 frame_image->width(), frame_image->height(), QImage::Format_RGBA8888_Premultiplied);
74 cropped_image->fill(QColor(QString::fromStdString("transparent")));
75
76 // Get current keyframe values
77 double left_value = left.GetValue(frame_number);
78 double top_value = top.GetValue(frame_number);
79 double right_value = right.GetValue(frame_number);
80 double bottom_value = bottom.GetValue(frame_number);
81
82 // Get the current shift amount (if any... to slide the image around in the cropped area)
83 double x_shift = x.GetValue(frame_number);
84 double y_shift = y.GetValue(frame_number);
85
86 // Get pixel array pointers
87 unsigned char *pixels = (unsigned char *) frame_image->bits();
88 unsigned char *cropped_pixels = (unsigned char *) cropped_image->bits();
89
90 // Get pixels sizes of all crop sides
91 int top_bar_height = top_value * frame_image->height();
92 int bottom_bar_height = bottom_value * frame_image->height();
93 int left_bar_width = left_value * frame_image->width();
94 int right_bar_width = right_value * frame_image->width();
95 int column_offset = x_shift * frame_image->width();
96 int row_offset = y_shift * frame_image->height();
97
98 // Image copy variables
99 int image_width = frame_image->width();
100 int src_start = left_bar_width;
101 int dst_start = left_bar_width;
102 int copy_length = frame_image->width() - right_bar_width - left_bar_width;
103
104 // Adjust for x offset
105 int copy_offset = 0;
106
107 if (column_offset < 0) {
108 // dest to the right
109 src_start += column_offset;
110 if (src_start < 0) {
111 int diff = 0 - src_start; // how far under 0 are we?
112 src_start = 0;
113 dst_start += diff;
114 copy_offset = -diff;
115 } else {
116 copy_offset = 0;
117 }
118
119 } else {
120 // dest to the left
121 src_start += column_offset;
122 if (image_width - src_start >= copy_length) {
123 // We have plenty pixels, use original copy-length
124 copy_offset = 0;
125 } else {
126 // We don't have enough pixels, shorten copy-length
127 copy_offset = (image_width - src_start) - copy_length;
128 }
129 }
130
131 // Loop through rows of pixels
132 for (int row = 0; row < frame_image->height(); row++) {
133 int adjusted_row = row - row_offset;
134 // Is this row visible?
135 if (adjusted_row >= top_bar_height && adjusted_row < (frame_image->height() - bottom_bar_height) && (copy_length + copy_offset > 0)) {
136 // Copy image (row by row, with offsets for x and y offset, and src/dst starting points for column filtering)
137 memcpy(&cropped_pixels[((adjusted_row * frame_image->width()) + dst_start) * 4],
138 &pixels[((row * frame_image->width()) + src_start) * 4],
139 sizeof(char) * (copy_length + copy_offset) * 4);
140 }
141 }
142
143 // Set frame image
144 frame->AddImage(cropped_image);
145
146 // return the modified frame
147 return frame;
148}
149
150// Generate JSON string of this object
151std::string Crop::Json() const {
152
153 // Return formatted string
154 return JsonValue().toStyledString();
155}
156
157// Generate Json::Value for this object
158Json::Value Crop::JsonValue() const {
159
160 // Create root json object
161 Json::Value root = EffectBase::JsonValue(); // get parent properties
162 root["type"] = info.class_name;
163 root["left"] = left.JsonValue();
164 root["top"] = top.JsonValue();
165 root["right"] = right.JsonValue();
166 root["bottom"] = bottom.JsonValue();
167 root["x"] = x.JsonValue();
168 root["y"] = y.JsonValue();
169
170 // return JsonValue
171 return root;
172}
173
174// Load JSON string into this object
175void Crop::SetJson(const std::string value) {
176
177 // Parse JSON string into JSON objects
178 try
179 {
180 const Json::Value root = openshot::stringToJson(value);
181 // Set all values that match
182 SetJsonValue(root);
183 }
184 catch (const std::exception& e)
185 {
186 // Error parsing JSON (or missing keys)
187 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
188 }
189}
190
191// Load Json::Value into this object
192void Crop::SetJsonValue(const Json::Value root) {
193
194 // Set parent data
196
197 // Set data from Json (if key is found)
198 if (!root["left"].isNull())
199 left.SetJsonValue(root["left"]);
200 if (!root["top"].isNull())
201 top.SetJsonValue(root["top"]);
202 if (!root["right"].isNull())
203 right.SetJsonValue(root["right"]);
204 if (!root["bottom"].isNull())
205 bottom.SetJsonValue(root["bottom"]);
206 if (!root["x"].isNull())
207 x.SetJsonValue(root["x"]);
208 if (!root["y"].isNull())
209 y.SetJsonValue(root["y"]);
210}
211
212// Get all properties for a specific frame
213std::string Crop::PropertiesJSON(int64_t requested_frame) const {
214
215 // Generate JSON properties list
216 Json::Value root;
217 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
218 root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
219 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
220 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
221 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
222 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
223
224 // Keyframes
225 root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 1.0, false, requested_frame);
226 root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
227 root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
228 root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);
229 root["x"] = add_property_json("X Offset", x.GetValue(requested_frame), "float", "", &x, -1.0, 1.0, false, requested_frame);
230 root["y"] = add_property_json("Y Offset", y.GetValue(requested_frame), "float", "", &y, -1.0, 1.0, false, requested_frame);
231
232 // Set the parent effect which properties this effect will inherit
233 root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
234
235 // Return formatted string
236 return root.toStyledString();
237}
Header file for Crop effect class.
Header file for all Exception classes.
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
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
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Crop.cpp:175
Keyframe right
Size of right bar.
Definition: Crop.h:65
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Crop.cpp:213
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Crop.cpp:158
Keyframe y
Y-offset.
Definition: Crop.h:68
Keyframe left
Size of left bar.
Definition: Crop.h:63
Keyframe x
X-offset.
Definition: Crop.h:67
Crop()
Blank constructor, useful when using Json to load the effect properties.
Definition: Crop.cpp:37
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Crop.cpp:192
Keyframe bottom
Size of bottom bar.
Definition: Crop.h:66
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:87
Keyframe top
Size of top bar.
Definition: Crop.h:64
std::string Json() const override
Generate JSON string of this object.
Definition: Crop.cpp:151
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87
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:72
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56