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