OpenShot Library | libopenshot  0.2.5
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  int pixel_count = frame_image->width() * frame_image->height();
73 
74  // Get keyframe values for this frame
75  float saturation_value = saturation.GetValue(frame_number);
76 
77  // Constants used for color saturation formula
78  const double pR = .299;
79  const double pG = .587;
80  const double pB = .114;
81 
82  // Loop through pixels
83  unsigned char *pixels = (unsigned char *) frame_image->bits();
84 
85  #pragma omp parallel for shared (pixels)
86  for (int pixel = 0; pixel < pixel_count; ++pixel)
87  {
88  // Get the RGB values from the pixel
89  int R = pixels[pixel * 4];
90  int G = pixels[pixel * 4 + 1];
91  int B = pixels[pixel * 4 + 2];
92 
93  // Calculate the saturation multiplier
94  double p = sqrt( (R * R * pR) +
95  (G * G * pG) +
96  (B * B * pB) );
97 
98  // Apply adjusted and constrained saturation
99  pixels[pixel * 4] = constrain(p + (R - p) * saturation_value);
100  pixels[pixel * 4 + 1] = constrain(p + (G - p) * saturation_value);
101  pixels[pixel * 4 + 2] = constrain(p + (B - p) * saturation_value);
102  }
103 
104  // return the modified frame
105  return frame;
106 }
107 
108 // Generate JSON string of this object
109 std::string Saturation::Json() const {
110 
111  // Return formatted string
112  return JsonValue().toStyledString();
113 }
114 
115 // Generate Json::Value for this object
116 Json::Value Saturation::JsonValue() const {
117 
118  // Create root json object
119  Json::Value root = EffectBase::JsonValue(); // get parent properties
120  root["type"] = info.class_name;
121  root["saturation"] = saturation.JsonValue();
122 
123  // return JsonValue
124  return root;
125 }
126 
127 // Load JSON string into this object
128 void Saturation::SetJson(const std::string value) {
129 
130  // Parse JSON string into JSON objects
131  try
132  {
133  const Json::Value root = openshot::stringToJson(value);
134  // Set all values that match
135  SetJsonValue(root);
136  }
137  catch (const std::exception& e)
138  {
139  // Error parsing JSON (or missing keys)
140  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
141  }
142 }
143 
144 // Load Json::Value into this object
145 void Saturation::SetJsonValue(const Json::Value root) {
146 
147  // Set parent data
149 
150  // Set data from Json (if key is found)
151  if (!root["saturation"].isNull())
152  saturation.SetJsonValue(root["saturation"]);
153 }
154 
155 // Get all properties for a specific frame
156 std::string Saturation::PropertiesJSON(int64_t requested_frame) const {
157 
158  // Generate JSON properties list
159  Json::Value root;
160  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
161  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
162  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
163  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
164  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
165  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
166 
167  // Keyframes
168  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", &saturation, 0.0, 4.0, false, requested_frame);
169 
170  // Return formatted string
171  return root.toStyledString();
172 }
openshot::ClipBase::add_property_json
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:68
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
openshot::Saturation::Json
std::string Json() const override
Get and Set JSON methods.
Definition: Saturation.cpp:109
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:39
openshot::ClipBase::End
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
openshot::Saturation::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Saturation.cpp:116
openshot::ClipBase::Layer
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
openshot::ClipBase::Id
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
openshot::Saturation::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Saturation.cpp:128
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:64
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:206
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:36
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
openshot::ClipBase::Duration
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
openshot::Saturation::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Saturation.cpp:145
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
openshot::Saturation::GetFrame
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
openshot::Frame::GetImage
std::shared_ptr< QImage > GetImage()
Get pointer to Qt QImage image object.
Definition: Frame.cpp:913
openshot::EffectBase::constrain
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:65
openshot::ClipBase::Position
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:53
openshot::Saturation::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Saturation.cpp:156
openshot::ClipBase::Start
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
openshot::Saturation::Saturation
Saturation()
Blank constructor, useful when using Json to load the effect properties.
Definition: Saturation.cpp:36
openshot::Saturation::saturation
Keyframe saturation
The color saturation: 0.0 = black and white, 1.0 = normal, 2.0 = double saturation.
Definition: Saturation.h:67
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262