OpenShot Library | libopenshot  0.1.2
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 tr1::shared_ptr<Frame> Saturation::GetFrame(tr1::shared_ptr<Frame> frame, long int frame_number)
74 {
75  // Get the frame's image
76  tr1::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) throw(InvalidJSON) {
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(long int requested_frame) {
180 
181  // Requested Point
182  Point requested_point(requested_frame, requested_frame);
183 
184  // Generate JSON properties list
185  Json::Value root;
186  root["id"] = add_property_json("ID", 0.0, "string", Id(), false, 0, -1, -1, CONSTANT, -1, true);
187  root["position"] = add_property_json("Position", Position(), "float", "", false, 0, 0, 30 * 60 * 60 * 48, CONSTANT, -1, false);
188  root["layer"] = add_property_json("Track", Layer(), "int", "", false, 0, 0, 20, CONSTANT, -1, false);
189  root["start"] = add_property_json("Start", Start(), "float", "", false, 0, 0, 30 * 60 * 60 * 48, CONSTANT, -1, false);
190  root["end"] = add_property_json("End", End(), "float", "", false, 0, 0, 30 * 60 * 60 * 48, CONSTANT, -1, false);
191  root["duration"] = add_property_json("Duration", Duration(), "float", "", false, 0, 0, 30 * 60 * 60 * 48, CONSTANT, -1, true);
192 
193  // Keyframes
194  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", saturation.Contains(requested_point), saturation.GetCount(), 0.0, 4.0, saturation.GetClosestPoint(requested_point).interpolation, saturation.GetClosestPoint(requested_point).co.X, false);
195 
196  // Return formatted string
197  return root.toStyledString();
198 }
199 
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:319
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:86
Saturation()
Blank constructor, useful when using Json to load the effect properties.
Definition: Saturation.cpp:33
bool Contains(Point p)
Does this keyframe contain a specific point.
Definition: KeyFrame.cpp:175
float End()
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
A Point is the basic building block of a key-frame curve.
Definition: Point.h:81
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
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:360
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
float GetValue(long int index)
Get the value at a specific index.
Definition: KeyFrame.cpp:224
Json::Value add_property_json(string name, float value, string type, string memo, bool contains_point, int number_of_points, float min_value, float max_value, InterpolationType intepolation, int closest_point_x, bool readonly)
Generate JSON for a property.
Definition: ClipBase.cpp:65
string Id()
Get basic properties.
Definition: ClipBase.h:76
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
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
float X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:61
Point GetClosestPoint(Point p)
Get current point (or closest point) from the X coordinate (i.e. the frame number) ...
Definition: KeyFrame.cpp:193
string PropertiesJSON(long int requested_frame)
Definition: Saturation.cpp:179
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Saturation.cpp:168
This namespace is the default namespace for all code in the openshot library.
long int GetCount()
Get the number of points (i.e. # of points)
Definition: KeyFrame.cpp:453
Coordinate co
This is the primary coordinate.
Definition: Point.h:83
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
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64
tr1::shared_ptr< Frame > GetFrame(tr1::shared_ptr< Frame > frame, long int frame_number)
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Definition: Saturation.cpp:73
float Duration()
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:48
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
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