OpenShot Library | libopenshot  0.2.5
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
42 Wave::Wave(Keyframe wavelength, Keyframe amplitude, Keyframe multiplier, Keyframe shift_x, Keyframe speed_y)
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 original pixels for frame image, and also make a copy for editing
72  const unsigned char *original_pixels = (unsigned char *) frame_image->constBits();
73  unsigned char *pixels = (unsigned char *) frame_image->bits();
74  int pixel_count = frame_image->width() * frame_image->height();
75 
76  // Get current keyframe values
77  double time = frame_number;
78  double wavelength_value = wavelength.GetValue(frame_number);
79  double amplitude_value = amplitude.GetValue(frame_number);
80  double multiplier_value = multiplier.GetValue(frame_number);
81  double shift_x_value = shift_x.GetValue(frame_number);
82  double speed_y_value = speed_y.GetValue(frame_number);
83 
84  // Loop through pixels
85  #pragma omp parallel for
86  for (int pixel = 0; pixel < pixel_count; ++pixel)
87  {
88  // Calculate pixel Y value
89  int Y = pixel / frame_image->width();
90 
91  // Calculate wave pixel offsets
92  float noiseVal = (100 + Y * 0.001) * multiplier_value; // Time and time multiplier (to make the wave move)
93  float noiseAmp = noiseVal * amplitude_value; // Apply amplitude / height of the wave
94  float waveformVal = sin((Y * wavelength_value) + (time * speed_y_value)); // Waveform algorithm on y-axis
95  float waveVal = (waveformVal + shift_x_value) * noiseAmp; // Shifts pixels on the x-axis
96 
97  long unsigned int source_px = round(pixel + waveVal);
98  if (source_px < 0)
99  source_px = 0;
100  if (source_px >= pixel_count)
101  source_px = pixel_count - 1;
102 
103  // Calculate source array location, and target array location, and copy the 4 color values
104  memcpy(&pixels[pixel * 4], &original_pixels[source_px * 4], sizeof(char) * 4);
105  }
106 
107  // return the modified frame
108  return frame;
109 }
110 
111 // Generate JSON string of this object
112 std::string Wave::Json() const {
113 
114  // Return formatted string
115  return JsonValue().toStyledString();
116 }
117 
118 // Generate Json::Value for this object
119 Json::Value Wave::JsonValue() const {
120 
121  // Create root json object
122  Json::Value root = EffectBase::JsonValue(); // get parent properties
123  root["type"] = info.class_name;
124  root["wavelength"] = wavelength.JsonValue();
125  root["amplitude"] = amplitude.JsonValue();
126  root["multiplier"] = multiplier.JsonValue();
127  root["shift_x"] = shift_x.JsonValue();
128  root["speed_y"] = speed_y.JsonValue();
129 
130  // return JsonValue
131  return root;
132 }
133 
134 // Load JSON string into this object
135 void Wave::SetJson(const std::string value) {
136 
137  // Parse JSON string into JSON objects
138  try
139  {
140  const Json::Value root = openshot::stringToJson(value);
141  // Set all values that match
142  SetJsonValue(root);
143  }
144  catch (const std::exception& e)
145  {
146  // Error parsing JSON (or missing keys)
147  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
148  }
149 }
150 
151 // Load Json::Value into this object
152 void Wave::SetJsonValue(const Json::Value root) {
153 
154  // Set parent data
156 
157  // Set data from Json (if key is found)
158  if (!root["wavelength"].isNull())
159  wavelength.SetJsonValue(root["wavelength"]);
160  if (!root["amplitude"].isNull())
161  amplitude.SetJsonValue(root["amplitude"]);
162  if (!root["multiplier"].isNull())
163  multiplier.SetJsonValue(root["multiplier"]);
164  if (!root["shift_x"].isNull())
165  shift_x.SetJsonValue(root["shift_x"]);
166  if (!root["speed_y"].isNull())
167  speed_y.SetJsonValue(root["speed_y"]);
168 }
169 
170 // Get all properties for a specific frame
171 std::string Wave::PropertiesJSON(int64_t requested_frame) const {
172 
173  // Generate JSON properties list
174  Json::Value root;
175  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
176  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
177  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
178  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
179  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
180  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
181 
182  // Keyframes
183  root["wavelength"] = add_property_json("Wave length", wavelength.GetValue(requested_frame), "float", "", &wavelength, 0.0, 3.0, false, requested_frame);
184  root["amplitude"] = add_property_json("Amplitude", amplitude.GetValue(requested_frame), "float", "", &amplitude, 0.0, 5.0, false, requested_frame);
185  root["multiplier"] = add_property_json("Multiplier", multiplier.GetValue(requested_frame), "float", "", &multiplier, 0.0, 10.0, false, requested_frame);
186  root["shift_x"] = add_property_json("X Shift", shift_x.GetValue(requested_frame), "float", "", &shift_x, 0.0, 1000.0, false, requested_frame);
187  root["speed_y"] = add_property_json("Vertical speed", speed_y.GetValue(requested_frame), "float", "", &speed_y, 0.0, 300.0, false, requested_frame);
188 
189  // Return formatted string
190  return root.toStyledString();
191 }
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:68
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
openshot::Wave::wavelength
Keyframe wavelength
The length of the wave.
Definition: Wave.h:61
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::Wave::speed_y
Keyframe speed_y
Speed of the wave on the Y-axis.
Definition: Wave.h:65
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
openshot::Wave::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: Wave.cpp:66
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
openshot::Wave::shift_x
Keyframe shift_x
Amount to shift X-axis.
Definition: Wave.h:64
openshot::Wave::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Wave.cpp:119
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::Wave::amplitude
Keyframe amplitude
The height of the wave.
Definition: Wave.h:62
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:205
openshot::Wave::multiplier
Keyframe multiplier
Amount to multiply the wave (make it bigger)
Definition: Wave.h:63
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:36
openshot::Wave::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Wave.cpp:135
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
openshot::Wave::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Wave.cpp:171
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
openshot::Wave::Wave
Wave()
Blank constructor, useful when using Json to load the effect properties.
Definition: Wave.cpp:36
openshot::Wave::Json
std::string Json() const override
Get and Set JSON methods.
Definition: Wave.cpp:112
openshot::ClipBase::Id
void Id(std::string value)
Set basic properties.
Definition: ClipBase.h:84
openshot::Frame::GetImage
std::shared_ptr< QImage > GetImage()
Get pointer to Qt QImage image object.
Definition: Frame.cpp:913
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:53
openshot::Wave::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Wave.cpp:152
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262