OpenShot Library | libopenshot  0.1.9
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 // Constrain a color value from 0 to 255
60 int Saturation::constrain(int color_value)
61 {
62  // Constrain new color from 0 to 255
63  if (color_value < 0)
64  color_value = 0;
65  else if (color_value > 255)
66  color_value = 255;
67 
68  return color_value;
69 }
70 
71 // This method is required for all derived classes of EffectBase, and returns a
72 // modified openshot::Frame object
73 std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
74 {
75  // Get the frame's image
76  std::shared_ptr<QImage> frame_image = frame->GetImage();
77 
78  if (!frame_image)
79  return frame;
80 
81  // Get keyframe values for this frame
82  float saturation_value = saturation.GetValue(frame_number);
83 
84  // Constants used for color saturation formula
85  double pR = .299;
86  double pG = .587;
87  double pB = .114;
88 
89  // Loop through pixels
90  unsigned char *pixels = (unsigned char *) frame_image->bits();
91  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
92  {
93  // Get the RGB values from the pixel
94  int R = pixels[byte_index];
95  int G = pixels[byte_index + 1];
96  int B = pixels[byte_index + 2];
97  int A = pixels[byte_index + 3];
98 
99  // Calculate the saturation multiplier
100  double p = sqrt( (R * R * pR) +
101  (G * G * pG) +
102  (B * B * pB) );
103 
104  // Adjust the saturation
105  R = p + (R - p) * saturation_value;
106  G = p + (G - p) * saturation_value;
107  B = p + (B - p) * saturation_value;
108 
109  // Constrain the value from 0 to 255
110  R = constrain(R);
111  G = constrain(G);
112  B = constrain(B);
113 
114  // Set all pixels to new value
115  pixels[byte_index] = R;
116  pixels[byte_index + 1] = G;
117  pixels[byte_index + 2] = B;
118  pixels[byte_index + 3] = A; // leave the alpha value alone
119  }
120 
121  // return the modified frame
122  return frame;
123 }
124 
125 // Generate JSON string of this object
127 
128  // Return formatted string
129  return JsonValue().toStyledString();
130 }
131 
132 // Generate Json::JsonValue for this object
133 Json::Value Saturation::JsonValue() {
134 
135  // Create root json object
136  Json::Value root = EffectBase::JsonValue(); // get parent properties
137  root["type"] = info.class_name;
138  root["saturation"] = saturation.JsonValue();
139 
140  // return JsonValue
141  return root;
142 }
143 
144 // Load JSON string into this object
145 void Saturation::SetJson(string value) {
146 
147  // Parse JSON string into JSON objects
148  Json::Value root;
149  Json::Reader reader;
150  bool success = reader.parse( value, root );
151  if (!success)
152  // Raise exception
153  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
154 
155  try
156  {
157  // Set all values that match
158  SetJsonValue(root);
159  }
160  catch (exception e)
161  {
162  // Error parsing JSON (or missing keys)
163  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
164  }
165 }
166 
167 // Load Json::JsonValue into this object
168 void Saturation::SetJsonValue(Json::Value root) {
169 
170  // Set parent data
172 
173  // Set data from Json (if key is found)
174  if (!root["saturation"].isNull())
175  saturation.SetJsonValue(root["saturation"]);
176 }
177 
178 // Get all properties for a specific frame
179 string Saturation::PropertiesJSON(int64_t requested_frame) {
180 
181  // Generate JSON properties list
182  Json::Value root;
183  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
184  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
185  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
186  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
187  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
188  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
189 
190  // Keyframes
191  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", &saturation, 0.0, 4.0, false, requested_frame);
192 
193  // Return formatted string
194  return root.toStyledString();
195 }
196 
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Saturation.cpp:133
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:321
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:86
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:84
string Json()
Get and Set JSON methods.
Definition: Saturation.cpp:126
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:69
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:362
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:82
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:83
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:109
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Saturation.cpp:168
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
This namespace is the default namespace for all code in the openshot library.
string PropertiesJSON(int64_t requested_frame)
Definition: Saturation.cpp:179
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
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:73
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:87
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:85
void SetJson(string value)
Load JSON string into this object.
Definition: Saturation.cpp:145
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:69