OpenShot Library | libopenshot  0.4.0
Wave.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 "Wave.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
19 Wave::Wave() : wavelength(0.06), amplitude(0.3), multiplier(0.2), shift_x(0.0), speed_y(0.2) {
20  // Init effect properties
21  init_effect_details();
22 }
23 
24 // Default constructor
25 Wave::Wave(Keyframe wavelength, Keyframe amplitude, Keyframe multiplier, Keyframe shift_x, Keyframe speed_y)
26  : wavelength(wavelength), amplitude(amplitude), multiplier(multiplier), shift_x(shift_x), speed_y(speed_y)
27 {
28  // Init effect properties
29  init_effect_details();
30 }
31 
32 // Init effect settings
33 void Wave::init_effect_details()
34 {
37 
39  info.class_name = "Wave";
40  info.name = "Wave";
41  info.description = "Distort the frame's image into a wave pattern.";
42  info.has_audio = false;
43  info.has_video = true;
44 
45 }
46 
47 // This method is required for all derived classes of EffectBase, and returns a
48 // modified openshot::Frame object
49 std::shared_ptr<openshot::Frame> Wave::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
50 {
51  // Get the frame's image
52  std::shared_ptr<QImage> frame_image = frame->GetImage();
53 
54  // Get original pixels for frame image, and also make a copy for editing
55  const unsigned char *original_pixels = (unsigned char *) frame_image->constBits();
56  unsigned char *pixels = (unsigned char *) frame_image->bits();
57  int pixel_count = frame_image->width() * frame_image->height();
58 
59  // Get current keyframe values
60  double time = frame_number;
61  double wavelength_value = wavelength.GetValue(frame_number);
62  double amplitude_value = amplitude.GetValue(frame_number);
63  double multiplier_value = multiplier.GetValue(frame_number);
64  double shift_x_value = shift_x.GetValue(frame_number);
65  double speed_y_value = speed_y.GetValue(frame_number);
66 
67  // Loop through pixels
68  #pragma omp parallel for
69  for (int pixel = 0; pixel < pixel_count; ++pixel)
70  {
71  // Calculate pixel Y value
72  int Y = pixel / frame_image->width();
73 
74  // Calculate wave pixel offsets
75  float noiseVal = (100 + Y * 0.001) * multiplier_value; // Time and time multiplier (to make the wave move)
76  float noiseAmp = noiseVal * amplitude_value; // Apply amplitude / height of the wave
77  float waveformVal = sin((Y * wavelength_value) + (time * speed_y_value)); // Waveform algorithm on y-axis
78  float waveVal = (waveformVal + shift_x_value) * noiseAmp; // Shifts pixels on the x-axis
79 
80  long unsigned int source_px = round(pixel + waveVal);
81  if (source_px < 0)
82  source_px = 0;
83  if (source_px >= pixel_count)
84  source_px = pixel_count - 1;
85 
86  // Calculate source array location, and target array location, and copy the 4 color values
87  memcpy(&pixels[pixel * 4], &original_pixels[source_px * 4], sizeof(char) * 4);
88  }
89 
90  // return the modified frame
91  return frame;
92 }
93 
94 // Generate JSON string of this object
95 std::string Wave::Json() const {
96 
97  // Return formatted string
98  return JsonValue().toStyledString();
99 }
100 
101 // Generate Json::Value for this object
102 Json::Value Wave::JsonValue() const {
103 
104  // Create root json object
105  Json::Value root = EffectBase::JsonValue(); // get parent properties
106  root["type"] = info.class_name;
107  root["wavelength"] = wavelength.JsonValue();
108  root["amplitude"] = amplitude.JsonValue();
109  root["multiplier"] = multiplier.JsonValue();
110  root["shift_x"] = shift_x.JsonValue();
111  root["speed_y"] = speed_y.JsonValue();
112 
113  // return JsonValue
114  return root;
115 }
116 
117 // Load JSON string into this object
118 void Wave::SetJson(const std::string value) {
119 
120  // Parse JSON string into JSON objects
121  try
122  {
123  const Json::Value root = openshot::stringToJson(value);
124  // Set all values that match
125  SetJsonValue(root);
126  }
127  catch (const std::exception& e)
128  {
129  // Error parsing JSON (or missing keys)
130  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
131  }
132 }
133 
134 // Load Json::Value into this object
135 void Wave::SetJsonValue(const Json::Value root) {
136 
137  // Set parent data
139 
140  // Set data from Json (if key is found)
141  if (!root["wavelength"].isNull())
142  wavelength.SetJsonValue(root["wavelength"]);
143  if (!root["amplitude"].isNull())
144  amplitude.SetJsonValue(root["amplitude"]);
145  if (!root["multiplier"].isNull())
146  multiplier.SetJsonValue(root["multiplier"]);
147  if (!root["shift_x"].isNull())
148  shift_x.SetJsonValue(root["shift_x"]);
149  if (!root["speed_y"].isNull())
150  speed_y.SetJsonValue(root["speed_y"]);
151 }
152 
153 // Get all properties for a specific frame
154 std::string Wave::PropertiesJSON(int64_t requested_frame) const {
155 
156  // Generate JSON properties list
157  Json::Value root = BasePropertiesJSON(requested_frame);
158 
159  // Keyframes
160  root["wavelength"] = add_property_json("Wave length", wavelength.GetValue(requested_frame), "float", "", &wavelength, 0.0, 3.0, false, requested_frame);
161  root["amplitude"] = add_property_json("Amplitude", amplitude.GetValue(requested_frame), "float", "", &amplitude, 0.0, 5.0, false, requested_frame);
162  root["multiplier"] = add_property_json("Multiplier", multiplier.GetValue(requested_frame), "float", "", &multiplier, 0.0, 10.0, false, requested_frame);
163  root["shift_x"] = add_property_json("X Shift", shift_x.GetValue(requested_frame), "float", "", &shift_x, 0.0, 1000.0, false, requested_frame);
164  root["speed_y"] = add_property_json("Vertical speed", speed_y.GetValue(requested_frame), "float", "", &speed_y, 0.0, 300.0, false, requested_frame);
165 
166  // Return formatted string
167  return root.toStyledString();
168 }
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::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot::Wave::wavelength
Keyframe wavelength
The length of the wave.
Definition: Wave.h:44
openshot::Wave::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: Wave.h:68
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Wave::speed_y
Keyframe speed_y
Speed of the wave on the Y-axis.
Definition: Wave.h:48
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::Wave::shift_x
Keyframe shift_x
Amount to shift X-axis.
Definition: Wave.h:47
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::Wave::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Wave.cpp:102
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::Wave::amplitude
Keyframe amplitude
The height of the wave.
Definition: Wave.h:45
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Wave::multiplier
Keyframe multiplier
Amount to multiply the wave (make it bigger)
Definition: Wave.h:46
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
Wave.h
Header file for Wave effect class.
openshot::Wave::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Wave.cpp:154
openshot::Wave::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Wave.cpp:118
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::Wave::Wave
Wave()
Default constructor, useful when using Json to load the effect properties.
Definition: Wave.cpp:19
openshot::Wave::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Wave.cpp:95
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Wave::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Wave.cpp:135
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