OpenShot Library | libopenshot  0.2.2
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../../include/effects/Shift.h"
29 
30 using namespace openshot;
31 
32 /// Blank constructor, useful when using Json to load the effect properties
33 Shift::Shift() : x(0.0), y(0.0) {
34  // Init effect properties
35  init_effect_details();
36 }
37 
38 // Default constructor
39 Shift::Shift(Keyframe x, Keyframe y) : x(x), y(y)
40 {
41  // Init effect properties
42  init_effect_details();
43 }
44 
45 // Init effect settings
46 void Shift::init_effect_details()
47 {
48  /// Initialize the values of the EffectInfo struct.
50 
51  /// Set the effect info
52  info.class_name = "Shift";
53  info.name = "Shift";
54  info.description = "Shift the image up, down, left, and right (with infinite wrapping).";
55  info.has_audio = false;
56  info.has_video = true;
57 }
58 
59 // This method is required for all derived classes of EffectBase, and returns a
60 // modified openshot::Frame object
61 std::shared_ptr<Frame> Shift::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
62 {
63  // Get the frame's image
64  std::shared_ptr<QImage> frame_image = frame->GetImage();
65  unsigned char *pixels = (unsigned char *) frame_image->bits();
66 
67  // Get the current shift amount, and clamp to range (-1 to 1 range)
68  double x_shift = x.GetValue(frame_number);
69  double x_shift_limit = fmod(abs(x_shift), 1.0);
70  double y_shift = y.GetValue(frame_number);
71  double y_shift_limit = fmod(abs(y_shift), 1.0);
72 
73  // Declare temp arrays to hold pixels while we move things around
74  unsigned char *temp_row = new unsigned char[frame_image->width() * 4]();
75 
76  // X-SHIFT
77  // Loop through rows
78  for (int row = 0; row < frame_image->height(); row++) {
79  // Copy current row's pixels
80  int starting_row_pixel = row * frame_image->width();
81  memcpy(temp_row, &pixels[starting_row_pixel * 4], sizeof(char) * frame_image->width() * 4);
82 
83  // Replace current row with left part of the pixels
84  if (x_shift > 0.0) {
85  // Move left side to the right
86  int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
87  memcpy(&pixels[(starting_row_pixel + relative_pixel_start) * 4], &temp_row[0], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
88 
89  // Move right side to the left
90  memcpy(&pixels[starting_row_pixel * 4], &temp_row[(frame_image->width() - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
91  } else if (x_shift < 0.0) {
92  // Move right side to the left
93  int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
94  memcpy(&pixels[starting_row_pixel * 4], &temp_row[relative_pixel_start * 4], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
95 
96  // Move left side to the right
97  memcpy(&pixels[(starting_row_pixel + (frame_image->width() - relative_pixel_start)) * 4], &temp_row[0], sizeof(char) * relative_pixel_start * 4);
98  }
99  }
100 
101  // Make temp copy of pixels for Y-SHIFT
102  unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4]();
103  memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4);
104 
105  // Y-SHIFT
106  // Replace current row with left part of the pixels
107  if (y_shift > 0.0) {
108  // Move top side to bottom
109  int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
110  memcpy(&pixels[relative_pixel_start * 4], temp_image, sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
111 
112  // Move bottom side to top
113  memcpy(pixels, &temp_image[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
114 
115  } else if (y_shift < 0.0) {
116  // Move bottom side to top
117  int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
118  memcpy(pixels, &temp_image[relative_pixel_start * 4], sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
119 
120  // Move left side to the right
121  memcpy(&pixels[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], temp_image, sizeof(char) * relative_pixel_start * 4);
122  }
123 
124  // Delete arrays
125  delete[] temp_row;
126  delete[] temp_image;
127 
128  // return the modified frame
129  return frame;
130 }
131 
132 // Generate JSON string of this object
133 string Shift::Json() {
134 
135  // Return formatted string
136  return JsonValue().toStyledString();
137 }
138 
139 // Generate Json::JsonValue for this object
140 Json::Value Shift::JsonValue() {
141 
142  // Create root json object
143  Json::Value root = EffectBase::JsonValue(); // get parent properties
144  root["type"] = info.class_name;
145  root["x"] = x.JsonValue();
146  root["y"] = y.JsonValue();
147 
148  // return JsonValue
149  return root;
150 }
151 
152 // Load JSON string into this object
153 void Shift::SetJson(string value) {
154 
155  // Parse JSON string into JSON objects
156  Json::Value root;
157  Json::Reader reader;
158  bool success = reader.parse( value, root );
159  if (!success)
160  // Raise exception
161  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
162 
163  try
164  {
165  // Set all values that match
166  SetJsonValue(root);
167  }
168  catch (exception e)
169  {
170  // Error parsing JSON (or missing keys)
171  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
172  }
173 }
174 
175 // Load Json::JsonValue into this object
176 void Shift::SetJsonValue(Json::Value root) {
177 
178  // Set parent data
180 
181  // Set data from Json (if key is found)
182  if (!root["x"].isNull())
183  x.SetJsonValue(root["x"]);
184  if (!root["y"].isNull())
185  y.SetJsonValue(root["y"]);
186 }
187 
188 // Get all properties for a specific frame
189 string Shift::PropertiesJSON(int64_t requested_frame) {
190 
191  // Generate JSON properties list
192  Json::Value root;
193  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
194  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
195  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
196  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
197  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
198  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
199 
200  // Keyframes
201  root["x"] = add_property_json("X Shift", x.GetValue(requested_frame), "float", "", &x, -1, 1, false, requested_frame);
202  root["y"] = add_property_json("Y Shift", y.GetValue(requested_frame), "float", "", &y, -1, 1, false, requested_frame);
203 
204  // Return formatted string
205  return root.toStyledString();
206 }
207 
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
openshot::Shift::GetFrame
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:61
openshot::ClipBase::Id
void Id(string value)
Set basic properties.
Definition: ClipBase.h:90
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::EffectInfoStruct::class_name
string class_name
The class name of the effect.
Definition: EffectBase.h:51
openshot::Shift::PropertiesJSON
string PropertiesJSON(int64_t requested_frame)
Definition: Shift.cpp:189
openshot::Shift::Shift
Shift()
Blank constructor, useful when using Json to load the effect properties.
Definition: Shift.cpp:33
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:81
openshot::ClipBase::add_property_json
Json::Value add_property_json(string name, float value, string type, string memo, Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame)
Generate JSON for a property.
Definition: ClipBase.cpp:65
openshot::Shift::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Shift.cpp:176
openshot::Shift::SetJson
void SetJson(string value)
Load JSON string into this object.
Definition: Shift.cpp:153
openshot::Keyframe::GetValue
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:64
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:152
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:33
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
openshot::EffectInfoStruct::description
string description
The description of this effect and what it does.
Definition: EffectBase.h:54
openshot::Keyframe::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:321
openshot::EffectInfoStruct::name
string name
The name of the effect.
Definition: EffectBase.h:53
openshot::Shift::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Shift.cpp:140
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:121
OpenShot Wipe Tests.e
e
Definition: OpenShot Wipe Tests.py:28
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
openshot::Shift::Json
string Json()
Get and Set JSON methods.
Definition: Shift.cpp:133
openshot::Shift::x
Keyframe x
Shift the X coordinates (left or right)
Definition: Shift.h:60
openshot::Keyframe::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:362
openshot::Shift::y
Keyframe y
Shift the Y coordinates (up or down)
Definition: Shift.h:61