OpenShot Library | libopenshot  0.3.2
Shift.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "Shift.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
19 Shift::Shift() : x(0.0), y(0.0) {
20  // Init effect properties
21  init_effect_details();
22 }
23 
24 // Default constructor
25 Shift::Shift(Keyframe x, Keyframe y) : x(x), y(y)
26 {
27  // Init effect properties
28  init_effect_details();
29 }
30 
31 // Init effect settings
32 void Shift::init_effect_details()
33 {
36 
38  info.class_name = "Shift";
39  info.name = "Shift";
40  info.description = "Shift the image up, down, left, and right (with infinite wrapping).";
41  info.has_audio = false;
42  info.has_video = true;
43 }
44 
45 // This method is required for all derived classes of EffectBase, and returns a
46 // modified openshot::Frame object
47 std::shared_ptr<openshot::Frame> Shift::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
48 {
49  // Get the frame's image
50  std::shared_ptr<QImage> frame_image = frame->GetImage();
51  unsigned char *pixels = (unsigned char *) frame_image->bits();
52 
53  // Get the current shift amount, and clamp to range (-1 to 1 range)
54  double x_shift = x.GetValue(frame_number);
55  double x_shift_limit = fmod(fabs(x_shift), 1.0);
56  double y_shift = y.GetValue(frame_number);
57  double y_shift_limit = fmod(fabs(y_shift), 1.0);
58 
59  // Declare temp arrays to hold pixels while we move things around
60  unsigned char *temp_row = new unsigned char[frame_image->width() * 4]();
61 
62  // X-SHIFT
63  // Loop through rows
64  for (int row = 0; row < frame_image->height(); row++) {
65  // Copy current row's pixels
66  int starting_row_pixel = row * frame_image->width();
67  memcpy(temp_row, &pixels[starting_row_pixel * 4], sizeof(char) * frame_image->width() * 4);
68 
69  // Replace current row with left part of the pixels
70  if (x_shift > 0.0) {
71  // Move left side to the right
72  int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
73  memcpy(&pixels[(starting_row_pixel + relative_pixel_start) * 4], &temp_row[0], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
74 
75  // Move right side to the left
76  memcpy(&pixels[starting_row_pixel * 4], &temp_row[(frame_image->width() - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
77  } else if (x_shift < 0.0) {
78  // Move right side to the left
79  int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
80  memcpy(&pixels[starting_row_pixel * 4], &temp_row[relative_pixel_start * 4], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
81 
82  // Move left side to the right
83  memcpy(&pixels[(starting_row_pixel + (frame_image->width() - relative_pixel_start)) * 4], &temp_row[0], sizeof(char) * relative_pixel_start * 4);
84  }
85  }
86 
87  // Make temp copy of pixels for Y-SHIFT
88  unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4]();
89  memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4);
90 
91  // Y-SHIFT
92  // Replace current row with left part of the pixels
93  if (y_shift > 0.0) {
94  // Move top side to bottom
95  int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
96  memcpy(&pixels[relative_pixel_start * 4], temp_image, sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
97 
98  // Move bottom side to top
99  memcpy(pixels, &temp_image[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
100 
101  } else if (y_shift < 0.0) {
102  // Move bottom side to top
103  int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
104  memcpy(pixels, &temp_image[relative_pixel_start * 4], sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
105 
106  // Move left side to the right
107  memcpy(&pixels[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], temp_image, sizeof(char) * relative_pixel_start * 4);
108  }
109 
110  // Delete arrays
111  delete[] temp_row;
112  delete[] temp_image;
113 
114  // return the modified frame
115  return frame;
116 }
117 
118 // Generate JSON string of this object
119 std::string Shift::Json() const {
120 
121  // Return formatted string
122  return JsonValue().toStyledString();
123 }
124 
125 // Generate Json::Value for this object
126 Json::Value Shift::JsonValue() const {
127 
128  // Create root json object
129  Json::Value root = EffectBase::JsonValue(); // get parent properties
130  root["type"] = info.class_name;
131  root["x"] = x.JsonValue();
132  root["y"] = y.JsonValue();
133 
134  // return JsonValue
135  return root;
136 }
137 
138 // Load JSON string into this object
139 void Shift::SetJson(const std::string value) {
140 
141  // Parse JSON string into JSON objects
142  try
143  {
144  const Json::Value root = openshot::stringToJson(value);
145  // Set all values that match
146  SetJsonValue(root);
147  }
148  catch (const std::exception& e)
149  {
150  // Error parsing JSON (or missing keys)
151  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
152  }
153 }
154 
155 // Load Json::Value into this object
156 void Shift::SetJsonValue(const Json::Value root) {
157 
158  // Set parent data
160 
161  // Set data from Json (if key is found)
162  if (!root["x"].isNull())
163  x.SetJsonValue(root["x"]);
164  if (!root["y"].isNull())
165  y.SetJsonValue(root["y"]);
166 }
167 
168 // Get all properties for a specific frame
169 std::string Shift::PropertiesJSON(int64_t requested_frame) const {
170 
171  // Generate JSON properties list
172  Json::Value root = BasePropertiesJSON(requested_frame);
173 
174  // Keyframes
175  root["x"] = add_property_json("X Shift", x.GetValue(requested_frame), "float", "", &x, -1, 1, false, requested_frame);
176  root["y"] = add_property_json("Y Shift", y.GetValue(requested_frame), "float", "", &y, -1, 1, false, requested_frame);
177 
178  // Return formatted string
179  return root.toStyledString();
180 }
openshot::ClipBase::add_property_json
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:96
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Shift::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Shift.cpp:139
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
Shift.h
Header file for Shift effect class.
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Shift::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Shift.cpp:119
openshot::Shift::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Shift.cpp:156
openshot::Shift::Shift
Shift()
Blank constructor, useful when using Json to load the effect properties.
Definition: Shift.cpp:19
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Shift::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Shift.cpp:126
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
Definition: EffectBase.cpp:179
openshot::Shift::GetFrame
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:62
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
openshot::Shift::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Shift.cpp:169
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Shift::x
Keyframe x
Shift the X coordinates (left or right)
Definition: Shift.h:44
openshot::Shift::y
Keyframe y
Shift the Y coordinates (up or down)
Definition: Shift.h:45
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258