OpenShot Library | libopenshot  0.1.1
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  // Constants used for color saturation formula
79  double pR = .299;
80  double pG = .587;
81  double pB = .114;
82 
83  // Loop through pixels
84  unsigned char *pixels = (unsigned char *) frame_image->bits();
85  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
86  {
87  // Get the RGB values from the pixel
88  int R = pixels[byte_index];
89  int G = pixels[byte_index + 1];
90  int B = pixels[byte_index + 2];
91  int A = pixels[byte_index + 3];
92 
93  // Calculate the saturation multiplier
94  double p = sqrt( (R * R * pR) +
95  (G * G * pG) +
96  (B * B * pB) );
97 
98  // Adjust the saturation
99  R = p + (R - p) * saturation.GetValue(frame_number);
100  G = p + (G - p) * saturation.GetValue(frame_number);
101  B = p + (B - p) * saturation.GetValue(frame_number);
102 
103  // Constrain the value from 0 to 255
104  R = constrain(R);
105  G = constrain(G);
106  B = constrain(B);
107 
108  // Set all pixels to new value
109  pixels[byte_index] = R;
110  pixels[byte_index + 1] = G;
111  pixels[byte_index + 2] = B;
112  pixels[byte_index + 3] = A; // leave the alpha value alone
113  }
114 
115  // return the modified frame
116  return frame;
117 }
118 
119 // Generate JSON string of this object
121 
122  // Return formatted string
123  return JsonValue().toStyledString();
124 }
125 
126 // Generate Json::JsonValue for this object
127 Json::Value Saturation::JsonValue() {
128 
129  // Create root json object
130  Json::Value root = EffectBase::JsonValue(); // get parent properties
131  root["type"] = info.class_name;
132  root["saturation"] = saturation.JsonValue();
133 
134  // return JsonValue
135  return root;
136 }
137 
138 // Load JSON string into this object
139 void Saturation::SetJson(string value) throw(InvalidJSON) {
140 
141  // Parse JSON string into JSON objects
142  Json::Value root;
143  Json::Reader reader;
144  bool success = reader.parse( value, root );
145  if (!success)
146  // Raise exception
147  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
148 
149  try
150  {
151  // Set all values that match
152  SetJsonValue(root);
153  }
154  catch (exception e)
155  {
156  // Error parsing JSON (or missing keys)
157  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
158  }
159 }
160 
161 // Load Json::JsonValue into this object
162 void Saturation::SetJsonValue(Json::Value root) {
163 
164  // Set parent data
166 
167  // Set data from Json (if key is found)
168  if (!root["saturation"].isNull())
169  saturation.SetJsonValue(root["saturation"]);
170 }
171 
172 // Get all properties for a specific frame
173 string Saturation::PropertiesJSON(long int requested_frame) {
174 
175  // Requested Point
176  Point requested_point(requested_frame, requested_frame);
177 
178  // Generate JSON properties list
179  Json::Value root;
180  root["id"] = add_property_json("ID", 0.0, "string", Id(), false, 0, -1, -1, CONSTANT, -1, true);
181  root["position"] = add_property_json("Position", Position(), "float", "", false, 0, 0, 1000 * 60 * 30, CONSTANT, -1, false);
182  root["layer"] = add_property_json("Layer", Layer(), "int", "", false, 0, 0, 1000, CONSTANT, -1, false);
183  root["start"] = add_property_json("Start", Start(), "float", "", false, 0, 0, 1000 * 60 * 30, CONSTANT, -1, false);
184  root["end"] = add_property_json("End", End(), "float", "", false, 0, 0, 1000 * 60 * 30, CONSTANT, -1, false);
185  root["duration"] = add_property_json("Duration", Duration(), "float", "", false, 0, 0, 1000 * 60 * 30, CONSTANT, -1, true);
186 
187  // Keyframes
188  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", saturation.Contains(requested_point), saturation.GetCount(), -10000, 10000, saturation.GetClosestPoint(requested_point).interpolation, saturation.GetClosestPoint(requested_point).co.X, false);
189 
190  // Return formatted string
191  return root.toStyledString();
192 }
193 
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Saturation.cpp:127
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:120
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:103
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:173
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Saturation.cpp:162
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:139
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