OpenShot Library | libopenshot  0.2.4
Wave.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Wave 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/Wave.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Wave::Wave() : wavelength(0.06), amplitude(0.3), multiplier(0.2), shift_x(0.0), speed_y(0.2) {
37  // Init effect properties
38  init_effect_details();
39 }
40 
41 // Default constructor
43  : wavelength(wavelength), amplitude(amplitude), multiplier(multiplier), shift_x(shift_x), speed_y(speed_y)
44 {
45  // Init effect properties
46  init_effect_details();
47 }
48 
49 // Init effect settings
50 void Wave::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Wave";
57  info.name = "Wave";
58  info.description = "Distort the frame's image into a wave pattern.";
59  info.has_audio = false;
60  info.has_video = true;
61 
62 }
63 
64 // This method is required for all derived classes of EffectBase, and returns a
65 // modified openshot::Frame object
66 std::shared_ptr<Frame> Wave::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
67 {
68  // Get the frame's image
69  std::shared_ptr<QImage> frame_image = frame->GetImage();
70 
71  // Get pixels for frame image
72  unsigned char *pixels = (unsigned char *) frame_image->bits();
73 
74  // Make temp copy of pixels before we start changing them
75  unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4]();
76  memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4);
77 
78  // Get current keyframe values
79  double time = frame_number;//abs(((frame_number + 255) % 510) - 255);
80  double wavelength_value = wavelength.GetValue(frame_number);
81  double amplitude_value = amplitude.GetValue(frame_number);
82  double multiplier_value = multiplier.GetValue(frame_number);
83  double shift_x_value = shift_x.GetValue(frame_number);
84  double speed_y_value = speed_y.GetValue(frame_number);
85 
86  // Loop through pixels
87  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
88  {
89  // Calculate X and Y pixel coordinates
90  int Y = pixel / frame_image->width();
91 
92  // Calculate wave pixel offsets
93  float noiseVal = (100 + Y * 0.001) * multiplier_value; // Time and time multiplier (to make the wave move)
94  float noiseAmp = noiseVal * amplitude_value; // Apply amplitude / height of the wave
95  float waveformVal = sin((Y * wavelength_value) + (time * speed_y_value)); // Waveform algorithm on y-axis
96  float waveVal = (waveformVal + shift_x_value) * noiseAmp; // Shifts pixels on the x-axis
97 
98  long unsigned int source_X = round(pixel + waveVal) * 4;
99  if (source_X < 0)
100  source_X = 0;
101  if (source_X > frame_image->width() * frame_image->height() * 4 * sizeof(char))
102  source_X = (frame_image->width() * frame_image->height() * 4 * sizeof(char)) - (sizeof(char) * 4);
103 
104  // Calculate source array location, and target array location, and copy the 4 color values
105  memcpy(&pixels[byte_index], &temp_image[source_X], sizeof(char) * 4);
106  }
107 
108  // Delete arrays
109  delete[] temp_image;
110 
111  // return the modified frame
112  return frame;
113 }
114 
115 // Generate JSON string of this object
116 std::string Wave::Json() {
117 
118  // Return formatted string
119  return JsonValue().toStyledString();
120 }
121 
122 // Generate Json::JsonValue for this object
123 Json::Value Wave::JsonValue() {
124 
125  // Create root json object
126  Json::Value root = EffectBase::JsonValue(); // get parent properties
127  root["type"] = info.class_name;
128  root["wavelength"] = wavelength.JsonValue();
129  root["amplitude"] = amplitude.JsonValue();
130  root["multiplier"] = multiplier.JsonValue();
131  root["shift_x"] = shift_x.JsonValue();
132  root["speed_y"] = speed_y.JsonValue();
133 
134  // return JsonValue
135  return root;
136 }
137 
138 // Load JSON string into this object
139 void Wave::SetJson(std::string value) {
140 
141  // Parse JSON string into JSON objects
142  Json::Value root;
143  Json::CharReaderBuilder rbuilder;
144  Json::CharReader* reader(rbuilder.newCharReader());
145 
146  std::string errors;
147  bool success = reader->parse( value.c_str(),
148  value.c_str() + value.size(), &root, &errors );
149  delete reader;
150 
151  if (!success)
152  // Raise exception
153  throw InvalidJSON("JSON could not be parsed (or is invalid)");
154 
155  try
156  {
157  // Set all values that match
158  SetJsonValue(root);
159  }
160  catch (const std::exception& e)
161  {
162  // Error parsing JSON (or missing keys)
163  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
164  }
165 }
166 
167 // Load Json::JsonValue into this object
168 void Wave::SetJsonValue(Json::Value root) {
169 
170  // Set parent data
172 
173  // Set data from Json (if key is found)
174  if (!root["wavelength"].isNull())
175  wavelength.SetJsonValue(root["wavelength"]);
176  if (!root["amplitude"].isNull())
177  amplitude.SetJsonValue(root["amplitude"]);
178  if (!root["multiplier"].isNull())
179  multiplier.SetJsonValue(root["multiplier"]);
180  if (!root["shift_x"].isNull())
181  shift_x.SetJsonValue(root["shift_x"]);
182  if (!root["speed_y"].isNull())
183  speed_y.SetJsonValue(root["speed_y"]);
184 }
185 
186 // Get all properties for a specific frame
187 std::string Wave::PropertiesJSON(int64_t requested_frame) {
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["wavelength"] = add_property_json("Wave length", wavelength.GetValue(requested_frame), "float", "", &wavelength, 0.0, 3.0, false, requested_frame);
200  root["amplitude"] = add_property_json("Amplitude", amplitude.GetValue(requested_frame), "float", "", &amplitude, 0.0, 5.0, false, requested_frame);
201  root["multiplier"] = add_property_json("Multiplier", multiplier.GetValue(requested_frame), "float", "", &multiplier, 0.0, 10.0, false, requested_frame);
202  root["shift_x"] = add_property_json("X Shift", shift_x.GetValue(requested_frame), "float", "", &shift_x, 0.0, 1000.0, false, requested_frame);
203  root["speed_y"] = add_property_json("Vertical speed", speed_y.GetValue(requested_frame), "float", "", &speed_y, 0.0, 300.0, false, requested_frame);
204 
205  // Return formatted string
206  return root.toStyledString();
207 }
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
std::string Json()
Get and Set JSON methods.
Definition: Wave.cpp:116
Wave()
Blank constructor, useful when using Json to load the effect properties.
Definition: Wave.cpp:36
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
void SetJson(std::string value)
Load JSON string into this object.
Definition: Wave.cpp:139
Keyframe wavelength
The length of the wave.
Definition: Wave.h:61
std::string Id()
Get basic properties.
Definition: ClipBase.h:76
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Wave.cpp:168
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:129
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: Wave.cpp:66
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
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 speed_y
Speed of the wave on the Y-axis.
Definition: Wave.h:65
Keyframe shift_x
Amount to shift X-axis.
Definition: Wave.h:64
std::string PropertiesJSON(int64_t requested_frame)
Definition: Wave.cpp:187
Keyframe amplitude
The height of the wave.
Definition: Wave.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
Keyframe multiplier
Amount to multiply the wave (make it bigger)
Definition: Wave.h:63
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Wave.cpp:123
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
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73