OpenShot Library | libopenshot  0.2.7
Shift.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Shift 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 "Shift.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 /// Blank constructor, useful when using Json to load the effect properties
37 Shift::Shift() : x(0.0), y(0.0) {
38  // Init effect properties
39  init_effect_details();
40 }
41 
42 // Default constructor
43 Shift::Shift(Keyframe x, Keyframe y) : x(x), y(y)
44 {
45  // Init effect properties
46  init_effect_details();
47 }
48 
49 // Init effect settings
50 void Shift::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Shift";
57  info.name = "Shift";
58  info.description = "Shift the image up, down, left, and right (with infinite wrapping).";
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<openshot::Frame> Shift::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
66 {
67  // Get the frame's image
68  std::shared_ptr<QImage> frame_image = frame->GetImage();
69  unsigned char *pixels = (unsigned char *) frame_image->bits();
70 
71  // Get the current shift amount, and clamp to range (-1 to 1 range)
72  double x_shift = x.GetValue(frame_number);
73  double x_shift_limit = fmod(fabs(x_shift), 1.0);
74  double y_shift = y.GetValue(frame_number);
75  double y_shift_limit = fmod(fabs(y_shift), 1.0);
76 
77  // Declare temp arrays to hold pixels while we move things around
78  unsigned char *temp_row = new unsigned char[frame_image->width() * 4]();
79 
80  // X-SHIFT
81  // Loop through rows
82  for (int row = 0; row < frame_image->height(); row++) {
83  // Copy current row's pixels
84  int starting_row_pixel = row * frame_image->width();
85  memcpy(temp_row, &pixels[starting_row_pixel * 4], sizeof(char) * frame_image->width() * 4);
86 
87  // Replace current row with left part of the pixels
88  if (x_shift > 0.0) {
89  // Move left side to the right
90  int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
91  memcpy(&pixels[(starting_row_pixel + relative_pixel_start) * 4], &temp_row[0], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
92 
93  // Move right side to the left
94  memcpy(&pixels[starting_row_pixel * 4], &temp_row[(frame_image->width() - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
95  } else if (x_shift < 0.0) {
96  // Move right side to the left
97  int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
98  memcpy(&pixels[starting_row_pixel * 4], &temp_row[relative_pixel_start * 4], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
99 
100  // Move left side to the right
101  memcpy(&pixels[(starting_row_pixel + (frame_image->width() - relative_pixel_start)) * 4], &temp_row[0], sizeof(char) * relative_pixel_start * 4);
102  }
103  }
104 
105  // Make temp copy of pixels for Y-SHIFT
106  unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4]();
107  memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4);
108 
109  // Y-SHIFT
110  // Replace current row with left part of the pixels
111  if (y_shift > 0.0) {
112  // Move top side to bottom
113  int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
114  memcpy(&pixels[relative_pixel_start * 4], temp_image, sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
115 
116  // Move bottom side to top
117  memcpy(pixels, &temp_image[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
118 
119  } else if (y_shift < 0.0) {
120  // Move bottom side to top
121  int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
122  memcpy(pixels, &temp_image[relative_pixel_start * 4], sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
123 
124  // Move left side to the right
125  memcpy(&pixels[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], temp_image, sizeof(char) * relative_pixel_start * 4);
126  }
127 
128  // Delete arrays
129  delete[] temp_row;
130  delete[] temp_image;
131 
132  // return the modified frame
133  return frame;
134 }
135 
136 // Generate JSON string of this object
137 std::string Shift::Json() const {
138 
139  // Return formatted string
140  return JsonValue().toStyledString();
141 }
142 
143 // Generate Json::Value for this object
144 Json::Value Shift::JsonValue() const {
145 
146  // Create root json object
147  Json::Value root = EffectBase::JsonValue(); // get parent properties
148  root["type"] = info.class_name;
149  root["x"] = x.JsonValue();
150  root["y"] = y.JsonValue();
151 
152  // return JsonValue
153  return root;
154 }
155 
156 // Load JSON string into this object
157 void Shift::SetJson(const std::string value) {
158 
159  // Parse JSON string into JSON objects
160  try
161  {
162  const Json::Value root = openshot::stringToJson(value);
163  // Set all values that match
164  SetJsonValue(root);
165  }
166  catch (const std::exception& e)
167  {
168  // Error parsing JSON (or missing keys)
169  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
170  }
171 }
172 
173 // Load Json::Value into this object
174 void Shift::SetJsonValue(const Json::Value root) {
175 
176  // Set parent data
178 
179  // Set data from Json (if key is found)
180  if (!root["x"].isNull())
181  x.SetJsonValue(root["x"]);
182  if (!root["y"].isNull())
183  y.SetJsonValue(root["y"]);
184 }
185 
186 // Get all properties for a specific frame
187 std::string Shift::PropertiesJSON(int64_t requested_frame) const {
188 
189  // Generate JSON properties list
190  Json::Value root;
191  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
192  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
193  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
194  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
195  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
196  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
197 
198  // Keyframes
199  root["x"] = add_property_json("X Shift", x.GetValue(requested_frame), "float", "", &x, -1, 1, false, requested_frame);
200  root["y"] = add_property_json("Y Shift", y.GetValue(requested_frame), "float", "", &y, -1, 1, false, requested_frame);
201 
202  // Set the parent effect which properties this effect will inherit
203  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
204 
205  // Return formatted string
206  return root.toStyledString();
207 }
Header file for all Exception classes.
Header file for Shift effect class.
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
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
Keyframe y
Shift the Y coordinates (up or down)
Definition: Shift.h:63
std::string Json() const override
Generate JSON string of this object.
Definition: Shift.cpp:137
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Shift.cpp:144
Keyframe x
Shift the X coordinates (left or right)
Definition: Shift.h:62
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Shift.cpp:174
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Shift.cpp:187
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: Shift.h:80
Shift()
Blank constructor, useful when using Json to load the effect properties.
Definition: Shift.cpp:37
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Shift.cpp:157
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