OpenShot Library | libopenshot  0.3.0
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  // Get RGB pixels from image and apply constrained contrast adjustment
67  int R = constrain((factor * (pixels[pixel * 4] - 128)) + 128);
68  int G = constrain((factor * (pixels[pixel * 4 + 1] - 128)) + 128);
69  int B = constrain((factor * (pixels[pixel * 4 + 2] - 128)) + 128);
70  // (Don't modify Alpha value)
71 
72  // Adjust brightness and write constrained values back to image
73  pixels[pixel * 4] = constrain(R + (255 * brightness_value));
74  pixels[pixel * 4 + 1] = constrain(G + (255 * brightness_value));
75  pixels[pixel * 4 + 2] = constrain(B + (255 * brightness_value));
76  }
77 
78  // return the modified frame
79  return frame;
80 }
81 
82 // Generate JSON string of this object
83 std::string Brightness::Json() const {
84 
85  // Return formatted string
86  return JsonValue().toStyledString();
87 }
88 
89 // Generate Json::Value for this object
90 Json::Value Brightness::JsonValue() const {
91 
92  // Create root json object
93  Json::Value root = EffectBase::JsonValue(); // get parent properties
94  root["type"] = info.class_name;
95  root["brightness"] = brightness.JsonValue();
96  root["contrast"] = contrast.JsonValue();
97 
98  // return JsonValue
99  return root;
100 }
101 
102 // Load JSON string into this object
103 void Brightness::SetJson(const std::string value) {
104 
105  // Parse JSON string into JSON objects
106  try
107  {
108  const Json::Value root = openshot::stringToJson(value);
109  // Set all values that match
110  SetJsonValue(root);
111  }
112  catch (const std::exception& e)
113  {
114  // Error parsing JSON (or missing keys)
115  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
116  }
117 }
118 
119 // Load Json::Value into this object
120 void Brightness::SetJsonValue(const Json::Value root) {
121 
122  // Set parent data
124 
125  // Set data from Json (if key is found)
126  if (!root["brightness"].isNull())
127  brightness.SetJsonValue(root["brightness"]);
128  if (!root["contrast"].isNull())
129  contrast.SetJsonValue(root["contrast"]);
130 }
131 
132 // Get all properties for a specific frame
133 std::string Brightness::PropertiesJSON(int64_t requested_frame) const {
134 
135  // Generate JSON properties list
136  Json::Value root;
137  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
138  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
139  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
140  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
141  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
142  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
143 
144  // Keyframes
145  root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
146  root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, 0.0, 100.0, false, requested_frame);
147 
148  // Set the parent effect which properties this effect will inherit
149  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
150 
151  // Return formatted string
152  return root.toStyledString();
153 }
Header file for Brightness class.
Header file for all Exception classes.
Keyframe brightness
Brightness keyframe. A constant value here will prevent animation.
Definition: Brightness.h:41
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
std::string Json() const override
Generate JSON string of this object.
Definition: Brightness.cpp:83
Keyframe contrast
Contrast keyframe.
Definition: Brightness.h:42
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Brightness.cpp:120
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Brightness.cpp:90
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Brightness.cpp:133
Brightness()
Blank constructor, useful when using Json to load the effect properties.
Definition: Brightness.cpp:19
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Brightness.cpp:103
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:88
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:89
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:85
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:86
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
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:77
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:58
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:112
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
Exception for invalid JSON.
Definition: Exceptions.h:218
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:54
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:358
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:325
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:39
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
std::string name
The name of the effect.
Definition: EffectBase.h:37
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38