OpenShot Library | libopenshot  0.2.3
Saturation.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Saturation 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/Saturation.h"
29 
30 using namespace openshot;
31 
32 /// Blank constructor, useful when using Json to load the effect properties
33 Saturation::Saturation() : saturation(1.0) {
34  // Init effect properties
35  init_effect_details();
36 }
37 
38 // Default constructor
39 Saturation::Saturation(Keyframe new_saturation) : saturation(new_saturation)
40 {
41  // Init effect properties
42  init_effect_details();
43 }
44 
45 // Init effect settings
46 void Saturation::init_effect_details()
47 {
48  /// Initialize the values of the EffectInfo struct.
50 
51  /// Set the effect info
52  info.class_name = "Saturation";
53  info.name = "Color Saturation";
54  info.description = "Adjust the color saturation.";
55  info.has_audio = false;
56  info.has_video = true;
57 }
58 
59 // This method is required for all derived classes of EffectBase, and returns a
60 // modified openshot::Frame object
61 std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
62 {
63  // Get the frame's image
64  std::shared_ptr<QImage> frame_image = frame->GetImage();
65 
66  if (!frame_image)
67  return frame;
68 
69  // Get keyframe values for this frame
70  float saturation_value = saturation.GetValue(frame_number);
71 
72  // Constants used for color saturation formula
73  double pR = .299;
74  double pG = .587;
75  double pB = .114;
76 
77  // Loop through pixels
78  unsigned char *pixels = (unsigned char *) frame_image->bits();
79  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
80  {
81  // Get the RGB values from the pixel
82  int R = pixels[byte_index];
83  int G = pixels[byte_index + 1];
84  int B = pixels[byte_index + 2];
85  int A = pixels[byte_index + 3];
86 
87  // Calculate the saturation multiplier
88  double p = sqrt( (R * R * pR) +
89  (G * G * pG) +
90  (B * B * pB) );
91 
92  // Adjust the saturation
93  R = p + (R - p) * saturation_value;
94  G = p + (G - p) * saturation_value;
95  B = p + (B - p) * saturation_value;
96 
97  // Constrain the value from 0 to 255
98  R = constrain(R);
99  G = constrain(G);
100  B = constrain(B);
101 
102  // Set all pixels to new value
103  pixels[byte_index] = R;
104  pixels[byte_index + 1] = G;
105  pixels[byte_index + 2] = B;
106  pixels[byte_index + 3] = A; // leave the alpha value alone
107  }
108 
109  // return the modified frame
110  return frame;
111 }
112 
113 // Generate JSON string of this object
115 
116  // Return formatted string
117  return JsonValue().toStyledString();
118 }
119 
120 // Generate Json::JsonValue for this object
121 Json::Value Saturation::JsonValue() {
122 
123  // Create root json object
124  Json::Value root = EffectBase::JsonValue(); // get parent properties
125  root["type"] = info.class_name;
126  root["saturation"] = saturation.JsonValue();
127 
128  // return JsonValue
129  return root;
130 }
131 
132 // Load JSON string into this object
133 void Saturation::SetJson(string value) {
134 
135  // Parse JSON string into JSON objects
136  Json::Value root;
137  Json::Reader reader;
138  bool success = reader.parse( value, root );
139  if (!success)
140  // Raise exception
141  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
142 
143  try
144  {
145  // Set all values that match
146  SetJsonValue(root);
147  }
148  catch (exception e)
149  {
150  // Error parsing JSON (or missing keys)
151  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
152  }
153 }
154 
155 // Load Json::JsonValue into this object
156 void Saturation::SetJsonValue(Json::Value root) {
157 
158  // Set parent data
160 
161  // Set data from Json (if key is found)
162  if (!root["saturation"].isNull())
163  saturation.SetJsonValue(root["saturation"]);
164 }
165 
166 // Get all properties for a specific frame
167 string Saturation::PropertiesJSON(int64_t requested_frame) {
168 
169  // Generate JSON properties list
170  Json::Value root;
171  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
172  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
173  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
174  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
175  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
176  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
177 
178  // Keyframes
179  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", &saturation, 0.0, 4.0, false, requested_frame);
180 
181  // Return formatted string
182  return root.toStyledString();
183 }
184 
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Saturation.cpp:121
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:352
Saturation()
Blank constructor, useful when using Json to load the effect properties.
Definition: Saturation.cpp:33
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 Json()
Get and Set JSON methods.
Definition: Saturation.cpp:114
string class_name
The class name of the effect.
Definition: EffectBase.h:51
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
string Id()
Get basic properties.
Definition: ClipBase.h:80
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:81
string name
The name of the effect.
Definition: EffectBase.h:53
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
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Saturation.cpp:156
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
string PropertiesJSON(int64_t requested_frame)
Definition: Saturation.cpp:167
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
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:62
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: Saturation.cpp:61
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64
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
void SetJson(string value)
Load JSON string into this object.
Definition: Saturation.cpp:133
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
Keyframe saturation
The color saturation: 0.0 = black and white, 1.0 = normal, 2.0 = double saturation.
Definition: Saturation.h:66