OpenShot Library | libopenshot  0.2.4
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  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "../../include/effects/Saturation.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Saturation::Saturation() : saturation(1.0) {
37  // Init effect properties
38  init_effect_details();
39 }
40 
41 // Default constructor
42 Saturation::Saturation(Keyframe new_saturation) : saturation(new_saturation)
43 {
44  // Init effect properties
45  init_effect_details();
46 }
47 
48 // Init effect settings
49 void Saturation::init_effect_details()
50 {
51  /// Initialize the values of the EffectInfo struct.
53 
54  /// Set the effect info
55  info.class_name = "Saturation";
56  info.name = "Color Saturation";
57  info.description = "Adjust the color saturation.";
58  info.has_audio = false;
59  info.has_video = true;
60 }
61 
62 // This method is required for all derived classes of EffectBase, and returns a
63 // modified openshot::Frame object
64 std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
65 {
66  // Get the frame's image
67  std::shared_ptr<QImage> frame_image = frame->GetImage();
68 
69  if (!frame_image)
70  return frame;
71 
72  // Get keyframe values for this frame
73  float saturation_value = saturation.GetValue(frame_number);
74 
75  // Constants used for color saturation formula
76  double pR = .299;
77  double pG = .587;
78  double pB = .114;
79 
80  // Loop through pixels
81  unsigned char *pixels = (unsigned char *) frame_image->bits();
82  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
83  {
84  // Get the RGB values from the pixel
85  int R = pixels[byte_index];
86  int G = pixels[byte_index + 1];
87  int B = pixels[byte_index + 2];
88  int A = pixels[byte_index + 3];
89 
90  // Calculate the saturation multiplier
91  double p = sqrt( (R * R * pR) +
92  (G * G * pG) +
93  (B * B * pB) );
94 
95  // Adjust the saturation
96  R = p + (R - p) * saturation_value;
97  G = p + (G - p) * saturation_value;
98  B = p + (B - p) * saturation_value;
99 
100  // Constrain the value from 0 to 255
101  R = constrain(R);
102  G = constrain(G);
103  B = constrain(B);
104 
105  // Set all pixels to new value
106  pixels[byte_index] = R;
107  pixels[byte_index + 1] = G;
108  pixels[byte_index + 2] = B;
109  pixels[byte_index + 3] = A; // leave the alpha value alone
110  }
111 
112  // return the modified frame
113  return frame;
114 }
115 
116 // Generate JSON string of this object
117 std::string Saturation::Json() {
118 
119  // Return formatted string
120  return JsonValue().toStyledString();
121 }
122 
123 // Generate Json::JsonValue for this object
124 Json::Value Saturation::JsonValue() {
125 
126  // Create root json object
127  Json::Value root = EffectBase::JsonValue(); // get parent properties
128  root["type"] = info.class_name;
129  root["saturation"] = saturation.JsonValue();
130 
131  // return JsonValue
132  return root;
133 }
134 
135 // Load JSON string into this object
136 void Saturation::SetJson(std::string value) {
137 
138  // Parse JSON string into JSON objects
139  Json::Value root;
140  Json::CharReaderBuilder rbuilder;
141  Json::CharReader* reader(rbuilder.newCharReader());
142 
143  std::string errors;
144  bool success = reader->parse( value.c_str(),
145  value.c_str() + value.size(), &root, &errors );
146  delete reader;
147 
148  if (!success)
149  // Raise exception
150  throw InvalidJSON("JSON could not be parsed (or is invalid)");
151 
152  try
153  {
154  // Set all values that match
155  SetJsonValue(root);
156  }
157  catch (const std::exception& e)
158  {
159  // Error parsing JSON (or missing keys)
160  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
161  }
162 }
163 
164 // Load Json::JsonValue into this object
165 void Saturation::SetJsonValue(Json::Value root) {
166 
167  // Set parent data
169 
170  // Set data from Json (if key is found)
171  if (!root["saturation"].isNull())
172  saturation.SetJsonValue(root["saturation"]);
173 }
174 
175 // Get all properties for a specific frame
176 std::string Saturation::PropertiesJSON(int64_t requested_frame) {
177 
178  // Generate JSON properties list
179  Json::Value root;
180  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
181  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
182  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
183  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
184  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
185  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
186 
187  // Keyframes
188  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", &saturation, 0.0, 4.0, false, requested_frame);
189 
190  // Return formatted string
191  return root.toStyledString();
192 }
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Saturation.cpp:124
void SetJson(std::string value)
Load JSON string into this object.
Definition: Saturation.cpp:136
Saturation()
Blank constructor, useful when using Json to load the effect properties.
Definition: Saturation.cpp:36
std::string Json()
Get and Set JSON methods.
Definition: Saturation.cpp:117
float End()
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:84
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:374
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
std::string PropertiesJSON(int64_t requested_frame)
Definition: Saturation.cpp:176
std::string Id()
Get basic properties.
Definition: ClipBase.h:76
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:129
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
std::string name
The name of the effect.
Definition: EffectBase.h:53
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Saturation.cpp:165
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame)
Generate JSON for a property.
Definition: ClipBase.cpp:68
This namespace is the default namespace for all code in the openshot library.
Json::Value JsonValue() const
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:329
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
Exception for invalid JSON.
Definition: Exceptions.h:205
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:65
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:64
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:81
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
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:67