OpenShot Library | libopenshot  0.2.2
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 // This method is required for all derived classes of EffectBase, and returns a
60 // modified openshot::Frame object
61 std::shared_ptr<Frame> Brightness::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
62 {
63  // Get the frame's image
64  std::shared_ptr<QImage> frame_image = frame->GetImage();
65 
66  // Get keyframe values for this frame
67  float brightness_value = brightness.GetValue(frame_number);
68  float contrast_value = contrast.GetValue(frame_number);
69 
70  // Loop through pixels
71  unsigned char *pixels = (unsigned char *) frame_image->bits();
72  for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
73  {
74  // Get the RGB values from the pixel
75  int R = pixels[byte_index];
76  int G = pixels[byte_index + 1];
77  int B = pixels[byte_index + 2];
78  int A = pixels[byte_index + 3];
79 
80  // Adjust the contrast
81  float factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
82  R = constrain((factor * (R - 128)) + 128);
83  G = constrain((factor * (G - 128)) + 128);
84  B = constrain((factor * (B - 128)) + 128);
85 
86  // Adjust the brightness
87  R += (255 * brightness_value);
88  G += (255 * brightness_value);
89  B += (255 * brightness_value);
90 
91  // Constrain the value from 0 to 255
92  R = constrain(R);
93  G = constrain(G);
94  B = constrain(B);
95 
96  // Set all pixels to new value
97  pixels[byte_index] = R;
98  pixels[byte_index + 1] = G;
99  pixels[byte_index + 2] = B;
100  pixels[byte_index + 3] = A; // leave the alpha value alone
101  }
102 
103  // return the modified frame
104  return frame;
105 }
106 
107 // Generate JSON string of this object
109 
110  // Return formatted string
111  return JsonValue().toStyledString();
112 }
113 
114 // Generate Json::JsonValue for this object
115 Json::Value Brightness::JsonValue() {
116 
117  // Create root json object
118  Json::Value root = EffectBase::JsonValue(); // get parent properties
119  root["type"] = info.class_name;
120  root["brightness"] = brightness.JsonValue();
121  root["contrast"] = contrast.JsonValue();
122 
123  // return JsonValue
124  return root;
125 }
126 
127 // Load JSON string into this object
128 void Brightness::SetJson(string value) {
129 
130  // Parse JSON string into JSON objects
131  Json::Value root;
132  Json::Reader reader;
133  bool success = reader.parse( value, root );
134  if (!success)
135  // Raise exception
136  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
137 
138  try
139  {
140  // Set all values that match
141  SetJsonValue(root);
142  }
143  catch (exception e)
144  {
145  // Error parsing JSON (or missing keys)
146  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
147  }
148 }
149 
150 // Load Json::JsonValue into this object
151 void Brightness::SetJsonValue(Json::Value root) {
152 
153  // Set parent data
155 
156  // Set data from Json (if key is found)
157  if (!root["brightness"].isNull())
158  brightness.SetJsonValue(root["brightness"]);
159  if (!root["contrast"].isNull())
160  contrast.SetJsonValue(root["contrast"]);
161 }
162 
163 // Get all properties for a specific frame
164 string Brightness::PropertiesJSON(int64_t requested_frame) {
165 
166  // Generate JSON properties list
167  Json::Value root;
168  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
169  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
170  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
171  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
172  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
173  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
174 
175  // Keyframes
176  root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
177  root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, 0.0, 100.0, false, requested_frame);
178 
179  // Return formatted string
180  return root.toStyledString();
181 }
182 
OpenShot Wipe Tests.contrast
contrast
Definition: OpenShot Wipe Tests.py:24
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
openshot::Brightness::SetJson
void SetJson(string value)
Load JSON string into this object.
Definition: Brightness.cpp:128
openshot::ClipBase::Id
void Id(string value)
Set basic properties.
Definition: ClipBase.h:90
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::Brightness::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: Brightness.cpp:61
openshot::EffectInfoStruct::class_name
string class_name
The class name of the effect.
Definition: EffectBase.h:51
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: EffectBase.cpp:81
openshot::Brightness::brightness
Keyframe brightness
Brightness keyframe. A constant value here will prevent animation.
Definition: Brightness.h:66
openshot::ClipBase::add_property_json
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
openshot::Brightness::PropertiesJSON
string PropertiesJSON(int64_t requested_frame)
Definition: Brightness.cpp:164
openshot::Keyframe::GetValue
double GetValue(int64_t index)
Get the value at a specific index.
Definition: KeyFrame.cpp:226
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::Brightness::Json
string Json()
Get and Set JSON methods.
Definition: Brightness.cpp:108
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:152
OpenShot Wipe Tests.brightness
brightness
Definition: OpenShot Wipe Tests.py:20
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:33
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
openshot::EffectInfoStruct::description
string description
The description of this effect and what it does.
Definition: EffectBase.h:54
openshot::Brightness::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Brightness.cpp:115
openshot::Keyframe::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:321
openshot::EffectInfoStruct::name
string name
The name of the effect.
Definition: EffectBase.h:53
openshot::Brightness::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Brightness.cpp:151
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: EffectBase.cpp:121
OpenShot Wipe Tests.e
e
Definition: OpenShot Wipe Tests.py:28
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
openshot::Brightness::Brightness
Brightness()
Blank constructor, useful when using Json to load the effect properties.
Definition: Brightness.cpp:33
openshot::EffectBase::constrain
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:62
openshot::Brightness::contrast
Keyframe contrast
Contrast keyframe.
Definition: Brightness.h:67
openshot::Keyframe::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:362