OpenShot Library | libopenshot  0.2.5
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  * @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/Brightness.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Brightness::Brightness() : brightness(0.0), contrast(3.0) {
37  // Init effect properties
38  init_effect_details();
39 }
40 
41 // Default constructor
42 Brightness::Brightness(Keyframe new_brightness, Keyframe new_contrast) : brightness(new_brightness), contrast(new_contrast)
43 {
44  // Init effect properties
45  init_effect_details();
46 }
47 
48 // Init effect settings
49 void Brightness::init_effect_details()
50 {
51  /// Initialize the values of the EffectInfo struct.
53 
54  /// Set the effect info
55  info.class_name = "Brightness";
56  info.name = "Brightness & Contrast";
57  info.description = "Adjust the brightness and contrast of the frame's image.";
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> Brightness::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  // Get keyframe values for this frame
70  float brightness_value = brightness.GetValue(frame_number);
71  float contrast_value = contrast.GetValue(frame_number);
72 
73  // Loop through pixels
74  unsigned char *pixels = (unsigned char *) frame_image->bits();
75  int pixel_count = frame_image->width() * frame_image->height();
76 
77  #pragma omp parallel for
78  for (int pixel = 0; pixel < pixel_count; ++pixel)
79  {
80  // Compute contrast adjustment factor
81  float factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
82 
83  // Get RGB pixels from image and apply constrained contrast adjustment
84  int R = constrain((factor * (pixels[pixel * 4] - 128)) + 128);
85  int G = constrain((factor * (pixels[pixel * 4 + 1] - 128)) + 128);
86  int B = constrain((factor * (pixels[pixel * 4 + 2] - 128)) + 128);
87  // (Don't modify Alpha value)
88 
89  // Adjust brightness and write constrained values back to image
90  pixels[pixel * 4] = constrain(R + (255 * brightness_value));
91  pixels[pixel * 4 + 1] = constrain(G + (255 * brightness_value));
92  pixels[pixel * 4 + 2] = constrain(B + (255 * brightness_value));
93  }
94 
95  // return the modified frame
96  return frame;
97 }
98 
99 // Generate JSON string of this object
100 std::string Brightness::Json() const {
101 
102  // Return formatted string
103  return JsonValue().toStyledString();
104 }
105 
106 // Generate Json::Value for this object
107 Json::Value Brightness::JsonValue() const {
108 
109  // Create root json object
110  Json::Value root = EffectBase::JsonValue(); // get parent properties
111  root["type"] = info.class_name;
112  root["brightness"] = brightness.JsonValue();
113  root["contrast"] = contrast.JsonValue();
114 
115  // return JsonValue
116  return root;
117 }
118 
119 // Load JSON string into this object
120 void Brightness::SetJson(const std::string value) {
121 
122  // Parse JSON string into JSON objects
123  try
124  {
125  const Json::Value root = openshot::stringToJson(value);
126  // Set all values that match
127  SetJsonValue(root);
128  }
129  catch (const std::exception& e)
130  {
131  // Error parsing JSON (or missing keys)
132  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
133  }
134 }
135 
136 // Load Json::Value into this object
137 void Brightness::SetJsonValue(const Json::Value root) {
138 
139  // Set parent data
141 
142  // Set data from Json (if key is found)
143  if (!root["brightness"].isNull())
144  brightness.SetJsonValue(root["brightness"]);
145  if (!root["contrast"].isNull())
146  contrast.SetJsonValue(root["contrast"]);
147 }
148 
149 // Get all properties for a specific frame
150 std::string Brightness::PropertiesJSON(int64_t requested_frame) const {
151 
152  // Generate JSON properties list
153  Json::Value root;
154  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
155  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
156  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
157  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
158  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
159  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
160 
161  // Keyframes
162  root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
163  root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, 0.0, 100.0, false, requested_frame);
164 
165  // Return formatted string
166  return root.toStyledString();
167 }
Keyframe brightness
Brightness keyframe. A constant value here will prevent animation.
Definition: Brightness.h:67
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Brightness.cpp:137
std::string Json() const override
Get and Set JSON methods.
Definition: Brightness.cpp:100
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:64
Keyframe contrast
Contrast keyframe.
Definition: Brightness.h:68
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Brightness.cpp:107
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Brightness.cpp:120
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Brightness.cpp:150
Brightness()
Blank constructor, useful when using Json to load the effect properties.
Definition: Brightness.cpp:36
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
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
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:65
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73
Exception for invalid JSON.
Definition: Exceptions.h:206
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:64
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:262
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
std::string name
The name of the effect.
Definition: EffectBase.h:53
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54