OpenShot Library | libopenshot  0.3.3
Brightness.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "Brightness.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
19 Brightness::Brightness() : brightness(0.0), contrast(3.0) {
20  // Init effect properties
21  init_effect_details();
22 }
23 
24 // Default constructor
25 Brightness::Brightness(Keyframe new_brightness, Keyframe new_contrast) : brightness(new_brightness), contrast(new_contrast)
26 {
27  // Init effect properties
28  init_effect_details();
29 }
30 
31 // Init effect settings
32 void Brightness::init_effect_details()
33 {
36 
38  info.class_name = "Brightness";
39  info.name = "Brightness & Contrast";
40  info.description = "Adjust the brightness and contrast of the frame's image.";
41  info.has_audio = false;
42  info.has_video = true;
43 }
44 
45 // This method is required for all derived classes of EffectBase, and returns a
46 // modified openshot::Frame object
47 std::shared_ptr<openshot::Frame> Brightness::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
48 {
49  // Get the frame's image
50  std::shared_ptr<QImage> frame_image = frame->GetImage();
51 
52  // Get keyframe values for this frame
53  float brightness_value = brightness.GetValue(frame_number);
54  float contrast_value = contrast.GetValue(frame_number);
55 
56  // Loop through pixels
57  unsigned char *pixels = (unsigned char *) frame_image->bits();
58  int pixel_count = frame_image->width() * frame_image->height();
59 
60  #pragma omp parallel for
61  for (int pixel = 0; pixel < pixel_count; ++pixel)
62  {
63  // Compute contrast adjustment factor
64  float factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
65 
66  // Calculate alpha % (to be used for removing pre-multiplied alpha value)
67  int A = pixels[pixel * 4 + 3];
68  float alpha_percent = A / 255.0;
69 
70  // Get RGB values, and remove pre-multiplied alpha
71  unsigned char R = pixels[pixel * 4 + 0] / alpha_percent;
72  unsigned char G = pixels[pixel * 4 + 1] / alpha_percent;
73  unsigned char B = pixels[pixel * 4 + 2] / alpha_percent;
74 
75  // Apply constrained contrast adjustment
76  R = constrain((factor * (R - 128)) + 128);
77  G = constrain((factor * (G - 128)) + 128);
78  B = constrain((factor * (B - 128)) + 128);
79 
80  // Adjust brightness and write constrained values back to image
81  pixels[pixel * 4 + 0] = constrain(R + (255 * brightness_value));
82  pixels[pixel * 4 + 1] = constrain(G + (255 * brightness_value));
83  pixels[pixel * 4 + 2] = constrain(B + (255 * brightness_value));
84 
85  // Pre-multiply the alpha back into the color channels
86  pixels[pixel * 4 + 0] *= alpha_percent;
87  pixels[pixel * 4 + 1] *= alpha_percent;
88  pixels[pixel * 4 + 2] *= alpha_percent;
89  }
90 
91  // return the modified frame
92  return frame;
93 }
94 
95 // Generate JSON string of this object
96 std::string Brightness::Json() const {
97 
98  // Return formatted string
99  return JsonValue().toStyledString();
100 }
101 
102 // Generate Json::Value for this object
103 Json::Value Brightness::JsonValue() const {
104 
105  // Create root json object
106  Json::Value root = EffectBase::JsonValue(); // get parent properties
107  root["type"] = info.class_name;
108  root["brightness"] = brightness.JsonValue();
109  root["contrast"] = contrast.JsonValue();
110 
111  // return JsonValue
112  return root;
113 }
114 
115 // Load JSON string into this object
116 void Brightness::SetJson(const std::string value) {
117 
118  // Parse JSON string into JSON objects
119  try
120  {
121  const Json::Value root = openshot::stringToJson(value);
122  // Set all values that match
123  SetJsonValue(root);
124  }
125  catch (const std::exception& e)
126  {
127  // Error parsing JSON (or missing keys)
128  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
129  }
130 }
131 
132 // Load Json::Value into this object
133 void Brightness::SetJsonValue(const Json::Value root) {
134 
135  // Set parent data
137 
138  // Set data from Json (if key is found)
139  if (!root["brightness"].isNull())
140  brightness.SetJsonValue(root["brightness"]);
141  if (!root["contrast"].isNull())
142  contrast.SetJsonValue(root["contrast"]);
143 }
144 
145 // Get all properties for a specific frame
146 std::string Brightness::PropertiesJSON(int64_t requested_frame) const {
147 
148  // Generate JSON properties list
149  Json::Value root = BasePropertiesJSON(requested_frame);
150 
151  // Keyframes
152  root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
153  root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, -128, 128.0, false, requested_frame);
154 
155  // Return formatted string
156  return root.toStyledString();
157 }
openshot::Brightness::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Brightness.cpp:146
openshot::ClipBase::add_property_json
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:96
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot::Brightness::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition: Brightness.h:60
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Brightness::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Brightness.cpp:96
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Brightness::brightness
Keyframe brightness
Brightness keyframe. A constant value here will prevent animation.
Definition: Brightness.h:41
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
Definition: EffectBase.cpp:179
Brightness.h
Header file for Brightness class.
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Brightness::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Brightness.cpp:103
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::Brightness::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Brightness.cpp:116
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::Brightness::Brightness
Brightness()
Blank constructor, useful when using Json to load the effect properties.
Definition: Brightness.cpp:19
openshot::EffectBase::constrain
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:60
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Brightness::contrast
Keyframe contrast
Contrast keyframe.
Definition: Brightness.h:42
openshot::Brightness::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Brightness.cpp:133
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258