OpenShot Library | libopenshot  0.2.7
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 "Wave.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 /// Blank constructor, useful when using Json to load the effect properties
37 Wave::Wave() : wavelength(0.06), amplitude(0.3), multiplier(0.2), shift_x(0.0), speed_y(0.2) {
38  // Init effect properties
39  init_effect_details();
40 }
41 
42 // Default constructor
44  : wavelength(wavelength), amplitude(amplitude), multiplier(multiplier), shift_x(shift_x), speed_y(speed_y)
45 {
46  // Init effect properties
47  init_effect_details();
48 }
49 
50 // Init effect settings
51 void Wave::init_effect_details()
52 {
53  /// Initialize the values of the EffectInfo struct.
55 
56  /// Set the effect info
57  info.class_name = "Wave";
58  info.name = "Wave";
59  info.description = "Distort the frame's image into a wave pattern.";
60  info.has_audio = false;
61  info.has_video = true;
62 
63 }
64 
65 // This method is required for all derived classes of EffectBase, and returns a
66 // modified openshot::Frame object
67 std::shared_ptr<openshot::Frame> Wave::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
68 {
69  // Get the frame's image
70  std::shared_ptr<QImage> frame_image = frame->GetImage();
71 
72  // Get original pixels for frame image, and also make a copy for editing
73  const unsigned char *original_pixels = (unsigned char *) frame_image->constBits();
74  unsigned char *pixels = (unsigned char *) frame_image->bits();
75  int pixel_count = frame_image->width() * frame_image->height();
76 
77  // Get current keyframe values
78  double time = frame_number;
79  double wavelength_value = wavelength.GetValue(frame_number);
80  double amplitude_value = amplitude.GetValue(frame_number);
81  double multiplier_value = multiplier.GetValue(frame_number);
82  double shift_x_value = shift_x.GetValue(frame_number);
83  double speed_y_value = speed_y.GetValue(frame_number);
84 
85  // Loop through pixels
86  #pragma omp parallel for
87  for (int pixel = 0; pixel < pixel_count; ++pixel)
88  {
89  // Calculate pixel Y value
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_px = round(pixel + waveVal);
99  if (source_px < 0)
100  source_px = 0;
101  if (source_px >= pixel_count)
102  source_px = pixel_count - 1;
103 
104  // Calculate source array location, and target array location, and copy the 4 color values
105  memcpy(&pixels[pixel * 4], &original_pixels[source_px * 4], sizeof(char) * 4);
106  }
107 
108  // return the modified frame
109  return frame;
110 }
111 
112 // Generate JSON string of this object
113 std::string Wave::Json() const {
114 
115  // Return formatted string
116  return JsonValue().toStyledString();
117 }
118 
119 // Generate Json::Value for this object
120 Json::Value Wave::JsonValue() const {
121 
122  // Create root json object
123  Json::Value root = EffectBase::JsonValue(); // get parent properties
124  root["type"] = info.class_name;
125  root["wavelength"] = wavelength.JsonValue();
126  root["amplitude"] = amplitude.JsonValue();
127  root["multiplier"] = multiplier.JsonValue();
128  root["shift_x"] = shift_x.JsonValue();
129  root["speed_y"] = speed_y.JsonValue();
130 
131  // return JsonValue
132  return root;
133 }
134 
135 // Load JSON string into this object
136 void Wave::SetJson(const std::string value) {
137 
138  // Parse JSON string into JSON objects
139  try
140  {
141  const Json::Value root = openshot::stringToJson(value);
142  // Set all values that match
143  SetJsonValue(root);
144  }
145  catch (const std::exception& e)
146  {
147  // Error parsing JSON (or missing keys)
148  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
149  }
150 }
151 
152 // Load Json::Value into this object
153 void Wave::SetJsonValue(const Json::Value root) {
154 
155  // Set parent data
157 
158  // Set data from Json (if key is found)
159  if (!root["wavelength"].isNull())
160  wavelength.SetJsonValue(root["wavelength"]);
161  if (!root["amplitude"].isNull())
162  amplitude.SetJsonValue(root["amplitude"]);
163  if (!root["multiplier"].isNull())
164  multiplier.SetJsonValue(root["multiplier"]);
165  if (!root["shift_x"].isNull())
166  shift_x.SetJsonValue(root["shift_x"]);
167  if (!root["speed_y"].isNull())
168  speed_y.SetJsonValue(root["speed_y"]);
169 }
170 
171 // Get all properties for a specific frame
172 std::string Wave::PropertiesJSON(int64_t requested_frame) const {
173 
174  // Generate JSON properties list
175  Json::Value root;
176  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
177  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
178  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
179  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
180  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
181  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
182 
183  // Keyframes
184  root["wavelength"] = add_property_json("Wave length", wavelength.GetValue(requested_frame), "float", "", &wavelength, 0.0, 3.0, false, requested_frame);
185  root["amplitude"] = add_property_json("Amplitude", amplitude.GetValue(requested_frame), "float", "", &amplitude, 0.0, 5.0, false, requested_frame);
186  root["multiplier"] = add_property_json("Multiplier", multiplier.GetValue(requested_frame), "float", "", &multiplier, 0.0, 10.0, false, requested_frame);
187  root["shift_x"] = add_property_json("X Shift", shift_x.GetValue(requested_frame), "float", "", &shift_x, 0.0, 1000.0, false, requested_frame);
188  root["speed_y"] = add_property_json("Vertical speed", speed_y.GetValue(requested_frame), "float", "", &speed_y, 0.0, 300.0, false, requested_frame);
189 
190  // Set the parent effect which properties this effect will inherit
191  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
192 
193  // Return formatted string
194  return root.toStyledString();
195 }
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Wave.cpp:172
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
std::string Json() const override
Generate JSON string of this object.
Definition: Wave.cpp:113
Header file for Wave effect class.
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Wave.cpp:136
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Wave.cpp:153
Wave()
Default constructor, useful when using Json to load the effect properties.
Definition: Wave.cpp:37
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
Header file for all Exception classes.
Keyframe wavelength
The length of the wave.
Definition: Wave.h:62
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: Wave.h:86
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Wave.cpp:120
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
Exception for invalid JSON.
Definition: Exceptions.h:205
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
Keyframe speed_y
Speed of the wave on the Y-axis.
Definition: Wave.h:66
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
Keyframe shift_x
Amount to shift X-axis.
Definition: Wave.h:65
Keyframe amplitude
The height of the wave.
Definition: Wave.h:63
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:72
Keyframe multiplier
Amount to multiply the wave (make it bigger)
Definition: Wave.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:109
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87