OpenShot Library | libopenshot  0.2.4
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(abs(x_shift), 1.0);
73  double y_shift = y.GetValue(frame_number);
74  double y_shift_limit = fmod(abs(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() {
137 
138  // Return formatted string
139  return JsonValue().toStyledString();
140 }
141 
142 // Generate Json::JsonValue for this object
143 Json::Value Shift::JsonValue() {
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(std::string value) {
157 
158  // Parse JSON string into JSON objects
159  Json::Value root;
160  Json::CharReaderBuilder rbuilder;
161  Json::CharReader* reader(rbuilder.newCharReader());
162 
163  std::string errors;
164  bool success = reader->parse( value.c_str(),
165  value.c_str() + value.size(), &root, &errors );
166  delete reader;
167 
168  if (!success)
169  // Raise exception
170  throw InvalidJSON("JSON could not be parsed (or is invalid)");
171 
172  try
173  {
174  // Set all values that match
175  SetJsonValue(root);
176  }
177  catch (const std::exception& e)
178  {
179  // Error parsing JSON (or missing keys)
180  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
181  }
182 }
183 
184 // Load Json::JsonValue into this object
185 void Shift::SetJsonValue(Json::Value root) {
186 
187  // Set parent data
189 
190  // Set data from Json (if key is found)
191  if (!root["x"].isNull())
192  x.SetJsonValue(root["x"]);
193  if (!root["y"].isNull())
194  y.SetJsonValue(root["y"]);
195 }
196 
197 // Get all properties for a specific frame
198 std::string Shift::PropertiesJSON(int64_t requested_frame) {
199 
200  // Generate JSON properties list
201  Json::Value root;
202  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
203  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
204  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
205  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
206  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
207  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
208 
209  // Keyframes
210  root["x"] = add_property_json("X Shift", x.GetValue(requested_frame), "float", "", &x, -1, 1, false, requested_frame);
211  root["y"] = add_property_json("Y Shift", y.GetValue(requested_frame), "float", "", &y, -1, 1, false, requested_frame);
212 
213  // Return formatted string
214  return root.toStyledString();
215 }
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Shift.cpp:143
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
std::string Json()
Get and Set JSON methods.
Definition: Shift.cpp:136
std::string PropertiesJSON(int64_t requested_frame)
Definition: Shift.cpp:198
Keyframe x
Shift the X coordinates (left or right)
Definition: Shift.h:61
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 Id()
Get basic properties.
Definition: ClipBase.h:76
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:129
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
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
void SetJson(std::string value)
Load JSON string into this object.
Definition: Shift.cpp:156
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
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
Keyframe y
Shift the Y coordinates (up or down)
Definition: Shift.h:62
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
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Shift.cpp:185
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73