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