OpenShot Library | libopenshot  0.1.9
Brightness.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Brightness 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/Brightness.h"
29 
30 using namespace openshot;
31 
32 /// Blank constructor, useful when using Json to load the effect properties
34  // Init effect properties
35  init_effect_details();
36 }
37 
38 // Default constructor
39 Brightness::Brightness(Keyframe new_brightness, Keyframe new_contrast) : brightness(new_brightness), contrast(new_contrast)
40 {
41  // Init effect properties
42  init_effect_details();
43 }
44 
45 // Init effect settings
46 void Brightness::init_effect_details()
47 {
48  /// Initialize the values of the EffectInfo struct.
50 
51  /// Set the effect info
52  info.class_name = "Brightness";
53  info.name = "Brightness & Contrast";
54  info.description = "Adjust the brightness and contrast of the frame's image.";
55  info.has_audio = false;
56  info.has_video = true;
57 }
58 
59 // Constrain a color value from 0 to 255
60 int Brightness::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 
72 // This method is required for all derived classes of EffectBase, and returns a
73 // modified openshot::Frame object
74 std::shared_ptr<Frame> Brightness::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
75 {
76  // Get the frame's image
77  std::shared_ptr<QImage> frame_image = frame->GetImage();
78 
79  // Get keyframe values for this frame
80  float brightness_value = brightness.GetValue(frame_number);
81  float contrast_value = contrast.GetValue(frame_number);
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  // Adjust the contrast
94  int factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
95  R = constrain((factor * (R - 128)) + 128);
96  G = constrain((factor * (G - 128)) + 128);
97  B = constrain((factor * (B - 128)) + 128);
98 
99  // Adjust the brightness
100  R += (255 * brightness_value);
101  G += (255 * brightness_value);
102  B += (255 * brightness_value);
103 
104  // Constrain the value from 0 to 255
105  R = constrain(R);
106  G = constrain(G);
107  B = constrain(B);
108 
109  // Set all pixels to new value
110  pixels[byte_index] = R;
111  pixels[byte_index + 1] = G;
112  pixels[byte_index + 2] = B;
113  pixels[byte_index + 3] = A; // leave the alpha value alone
114  }
115 
116  // return the modified frame
117  return frame;
118 }
119 
120 // Generate JSON string of this object
122 
123  // Return formatted string
124  return JsonValue().toStyledString();
125 }
126 
127 // Generate Json::JsonValue for this object
128 Json::Value Brightness::JsonValue() {
129 
130  // Create root json object
131  Json::Value root = EffectBase::JsonValue(); // get parent properties
132  root["type"] = info.class_name;
133  root["brightness"] = brightness.JsonValue();
134  root["contrast"] = contrast.JsonValue();
135 
136  // return JsonValue
137  return root;
138 }
139 
140 // Load JSON string into this object
141 void Brightness::SetJson(string value) {
142 
143  // Parse JSON string into JSON objects
144  Json::Value root;
145  Json::Reader reader;
146  bool success = reader.parse( value, root );
147  if (!success)
148  // Raise exception
149  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
150 
151  try
152  {
153  // Set all values that match
154  SetJsonValue(root);
155  }
156  catch (exception e)
157  {
158  // Error parsing JSON (or missing keys)
159  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
160  }
161 }
162 
163 // Load Json::JsonValue into this object
164 void Brightness::SetJsonValue(Json::Value root) {
165 
166  // Set parent data
168 
169  // Set data from Json (if key is found)
170  if (!root["brightness"].isNull())
171  brightness.SetJsonValue(root["brightness"]);
172  if (!root["contrast"].isNull())
173  contrast.SetJsonValue(root["contrast"]);
174 }
175 
176 // Get all properties for a specific frame
177 string Brightness::PropertiesJSON(int64_t requested_frame) {
178 
179  // Generate JSON properties list
180  Json::Value root;
181  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
182  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
183  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
184  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
185  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
186  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
187 
188  // Keyframes
189  root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
190  root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, 0.0, 100.0, false, requested_frame);
191 
192  // Return formatted string
193  return root.toStyledString();
194 }
195 
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:321
Brightness()
Blank constructor, useful when using Json to load the effect properties.
Definition: Brightness.cpp:33
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Brightness.cpp:164
Keyframe contrast
Contrast keyframe.
Definition: Brightness.h:70
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 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
Keyframe brightness
Brightness keyframe. A constant value here will prevent animation.
Definition: Brightness.h:69
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
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: Brightness.cpp:74
void SetJson(string value)
Load JSON string into this object.
Definition: Brightness.cpp:141
This namespace is the default namespace for all code in the openshot library.
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
string PropertiesJSON(int64_t requested_frame)
Definition: Brightness.cpp:177
float Duration()
Get the length of this clip (in seconds)
Definition: ClipBase.h:87
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Brightness.cpp:128
float Start()
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:85
string Json()
Get and Set JSON methods.
Definition: Brightness.cpp:121
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73