OpenShot Library | libopenshot  0.2.3
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../../include/effects/Wave.h"
29 
30 using namespace openshot;
31 
32 /// Blank constructor, useful when using Json to load the effect properties
33 Wave::Wave() : wavelength(0.06), amplitude(0.3), multiplier(0.2), shift_x(0.0), speed_y(0.2) {
34  // Init effect properties
35  init_effect_details();
36 }
37 
38 // Default constructor
39 Wave::Wave(Keyframe wavelength, Keyframe amplitude, Keyframe multiplier, Keyframe shift_x, Keyframe speed_y)
40  : wavelength(wavelength), amplitude(amplitude), multiplier(multiplier), shift_x(shift_x), speed_y(speed_y)
41 {
42  // Init effect properties
43  init_effect_details();
44 }
45 
46 // Init effect settings
47 void Wave::init_effect_details()
48 {
49  /// Initialize the values of the EffectInfo struct.
51 
52  /// Set the effect info
53  info.class_name = "Wave";
54  info.name = "Wave";
55  info.description = "Distort the frame's image into a wave pattern.";
56  info.has_audio = false;
57  info.has_video = true;
58 
59 }
60 
61 // This method is required for all derived classes of EffectBase, and returns a
62 // modified openshot::Frame object
63 std::shared_ptr<Frame> Wave::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
64 {
65  // Get the frame's image
66  std::shared_ptr<QImage> frame_image = frame->GetImage();
67 
68  // Get pixels for frame image
69  unsigned char *pixels = (unsigned char *) frame_image->bits();
70 
71  // Make temp copy of pixels before we start changing them
72  unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4]();
73  memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4);
74 
75  // Get current keyframe values
76  double time = frame_number;//abs(((frame_number + 255) % 510) - 255);
77  double wavelength_value = wavelength.GetValue(frame_number);
78  double amplitude_value = amplitude.GetValue(frame_number);
79  double multiplier_value = multiplier.GetValue(frame_number);
80  double shift_x_value = shift_x.GetValue(frame_number);
81  double speed_y_value = speed_y.GetValue(frame_number);
82 
83  // Loop through pixels
84  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
85  {
86  // Calculate X and Y pixel coordinates
87  int Y = pixel / frame_image->width();
88 
89  // Calculate wave pixel offsets
90  float noiseVal = (100 + Y * 0.001) * multiplier_value; // Time and time multiplier (to make the wave move)
91  float noiseAmp = noiseVal * amplitude_value; // Apply amplitude / height of the wave
92  float waveformVal = sin((Y * wavelength_value) + (time * speed_y_value)); // Waveform algorithm on y-axis
93  float waveVal = (waveformVal + shift_x_value) * noiseAmp; // Shifts pixels on the x-axis
94 
95  int source_X = round(pixel + waveVal) * 4;
96  if (source_X < 0)
97  source_X = 0;
98  if (source_X > frame_image->width() * frame_image->height() * 4 * sizeof(char))
99  source_X = (frame_image->width() * frame_image->height() * 4 * sizeof(char)) - (sizeof(char) * 4);
100 
101  // Calculate source array location, and target array location, and copy the 4 color values
102  memcpy(&pixels[byte_index], &temp_image[source_X], sizeof(char) * 4);
103  }
104 
105  // Delete arrays
106  delete[] temp_image;
107 
108  // return the modified frame
109  return frame;
110 }
111 
112 // Generate JSON string of this object
113 string Wave::Json() {
114 
115  // Return formatted string
116  return JsonValue().toStyledString();
117 }
118 
119 // Generate Json::JsonValue for this object
120 Json::Value Wave::JsonValue() {
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(string value) {
137 
138  // Parse JSON string into JSON objects
139  Json::Value root;
140  Json::Reader reader;
141  bool success = reader.parse( value, root );
142  if (!success)
143  // Raise exception
144  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
145 
146  try
147  {
148  // Set all values that match
149  SetJsonValue(root);
150  }
151  catch (exception e)
152  {
153  // Error parsing JSON (or missing keys)
154  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
155  }
156 }
157 
158 // Load Json::JsonValue into this object
159 void Wave::SetJsonValue(Json::Value root) {
160 
161  // Set parent data
163 
164  // Set data from Json (if key is found)
165  if (!root["wavelength"].isNull())
166  wavelength.SetJsonValue(root["wavelength"]);
167  if (!root["amplitude"].isNull())
168  amplitude.SetJsonValue(root["amplitude"]);
169  if (!root["multiplier"].isNull())
170  multiplier.SetJsonValue(root["multiplier"]);
171  if (!root["shift_x"].isNull())
172  shift_x.SetJsonValue(root["shift_x"]);
173  if (!root["speed_y"].isNull())
174  speed_y.SetJsonValue(root["speed_y"]);
175 }
176 
177 // Get all properties for a specific frame
178 string Wave::PropertiesJSON(int64_t requested_frame) {
179 
180  // Generate JSON properties list
181  Json::Value root;
182  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
183  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
184  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
185  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
186  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
187  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
188 
189  // Keyframes
190  root["wavelength"] = add_property_json("Wave length", wavelength.GetValue(requested_frame), "float", "", &wavelength, 0.0, 3.0, false, requested_frame);
191  root["amplitude"] = add_property_json("Amplitude", amplitude.GetValue(requested_frame), "float", "", &amplitude, 0.0, 5.0, false, requested_frame);
192  root["multiplier"] = add_property_json("Multiplier", multiplier.GetValue(requested_frame), "float", "", &multiplier, 0.0, 10.0, false, requested_frame);
193  root["shift_x"] = add_property_json("X Shift", shift_x.GetValue(requested_frame), "float", "", &shift_x, 0.0, 1000.0, false, requested_frame);
194  root["speed_y"] = add_property_json("Vertical speed", speed_y.GetValue(requested_frame), "float", "", &speed_y, 0.0, 300.0, false, requested_frame);
195 
196  // Return formatted string
197  return root.toStyledString();
198 }
199 
void SetJson(string value)
Load JSON string into this object.
Definition: Wave.cpp:136
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:352
string Json()
Get and Set JSON methods.
Definition: Wave.cpp:113
float End()
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:84
Json::Value add_property_json(string name, float value, string type, string memo, Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame)
Generate JSON for a property.
Definition: ClipBase.cpp:65
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:82
string class_name
The class name of the effect.
Definition: EffectBase.h:51
Wave()
Blank constructor, useful when using Json to load the effect properties.
Definition: Wave.cpp:33
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:81
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:393
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
Keyframe wavelength
The length of the wave.
Definition: Wave.h:60
string Id()
Get basic properties.
Definition: ClipBase.h:80
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:81
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Wave.cpp:159
string name
The name of the effect.
Definition: EffectBase.h:53
string PropertiesJSON(int64_t requested_frame)
Definition: Wave.cpp:178
string description
The description of this effect and what it does.
Definition: EffectBase.h:54
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:121
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:63
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
Exception for invalid JSON.
Definition: Exceptions.h:152
Keyframe speed_y
Speed of the wave on the Y-axis.
Definition: Wave.h:64
Keyframe shift_x
Amount to shift X-axis.
Definition: Wave.h:63
Keyframe amplitude
The height of the wave.
Definition: Wave.h:61
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:62
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Wave.cpp:120
float Duration()
Get the length of this clip (in seconds)
Definition: ClipBase.h:85
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:83
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73