OpenShot Library | libopenshot 0.6.0
Loading...
Searching...
No Matches
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#include <array>
16
17using namespace openshot;
18
20Brightness::Brightness() : brightness(0.0), contrast(3.0), mask_mode(BRIGHTNESS_MASK_LIMIT_TO_AREA) {
21 // Init effect properties
22 init_effect_details();
23}
24
25// Default constructor
26Brightness::Brightness(Keyframe new_brightness, Keyframe new_contrast) :
27 brightness(new_brightness), contrast(new_contrast), mask_mode(BRIGHTNESS_MASK_LIMIT_TO_AREA)
28{
29 // Init effect properties
30 init_effect_details();
31}
32
33// Init effect settings
34void Brightness::init_effect_details()
35{
38
40 info.class_name = "Brightness";
41 info.name = "Brightness & Contrast";
42 info.description = "Adjust the brightness and contrast of the frame's image.";
43 info.has_audio = false;
44 info.has_video = true;
45}
46
47// This method is required for all derived classes of EffectBase, and returns a
48// modified openshot::Frame object
49std::shared_ptr<openshot::Frame> Brightness::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
50{
51 // Get the frame's image
52 std::shared_ptr<QImage> frame_image = frame->GetImage();
53 if (!frame_image)
54 return frame;
55
56 // Get keyframe values for this frame
57 const float brightness_value = brightness.GetValue(frame_number);
58 const float contrast_value = contrast.GetValue(frame_number);
59 const float contrast_factor = (259.0f * (contrast_value + 255.0f)) / (255.0f * (259.0f - contrast_value));
60 const int brightness_offset_i = static_cast<int>(255.0f * brightness_value);
61
62 // Loop through pixels
63 unsigned char *pixels = reinterpret_cast<unsigned char *>(frame_image->bits());
64 const int pixel_count = frame_image->width() * frame_image->height();
65 // LUT for undoing premultiplication without a per-pixel divide.
66 static const std::array<float, 256> inv_alpha = [] {
67 std::array<float, 256> lut{};
68 lut[0] = 0.0f;
69 for (int i = 1; i < 256; ++i)
70 lut[i] = 255.0f / static_cast<float>(i);
71 return lut;
72 }();
73 const auto clamp_u8 = [](int value) -> unsigned char {
74 if (value < 0) return 0;
75 if (value > 255) return 255;
76 return static_cast<unsigned char>(value);
77 };
78 const auto clamp_i = [](int value) -> int {
79 if (value < 0) return 0;
80 if (value > 255) return 255;
81 return value;
82 };
83
84 const auto adjust_contrast_and_brightness = [&](int &R, int &G, int &B) {
85 R = clamp_u8(clamp_i(static_cast<int>((contrast_factor * (R - 128)) + 128.0f)) + brightness_offset_i);
86 G = clamp_u8(clamp_i(static_cast<int>((contrast_factor * (G - 128)) + 128.0f)) + brightness_offset_i);
87 B = clamp_u8(clamp_i(static_cast<int>((contrast_factor * (B - 128)) + 128.0f)) + brightness_offset_i);
88 };
89
90 #pragma omp parallel for if(pixel_count >= 16384) schedule(static)
91 for (int pixel = 0; pixel < pixel_count; ++pixel)
92 {
93 const int idx = pixel * 4;
94
95 // Split hot paths by alpha to avoid unnecessary premultiply/unpremultiply work.
96 const int A = pixels[idx + 3];
97 if (A <= 0)
98 continue;
99 int R = 0;
100 int G = 0;
101 int B = 0;
102 if (A == 255) {
103 R = pixels[idx + 0];
104 G = pixels[idx + 1];
105 B = pixels[idx + 2];
106 adjust_contrast_and_brightness(R, G, B);
107 pixels[idx + 0] = static_cast<unsigned char>(R);
108 pixels[idx + 1] = static_cast<unsigned char>(G);
109 pixels[idx + 2] = static_cast<unsigned char>(B);
110 } else {
111 const float alpha_percent = static_cast<float>(A) * (1.0f / 255.0f);
112 const float inv_alpha_percent = inv_alpha[A];
113
114 // Get RGB values, and remove pre-multiplied alpha
115 R = static_cast<int>(pixels[idx + 0] * inv_alpha_percent);
116 G = static_cast<int>(pixels[idx + 1] * inv_alpha_percent);
117 B = static_cast<int>(pixels[idx + 2] * inv_alpha_percent);
118 adjust_contrast_and_brightness(R, G, B);
119
120 // Pre-multiply alpha back into color channels
121 pixels[idx + 0] = static_cast<unsigned char>(R * alpha_percent);
122 pixels[idx + 1] = static_cast<unsigned char>(G * alpha_percent);
123 pixels[idx + 2] = static_cast<unsigned char>(B * alpha_percent);
124 }
125 }
126
127 // return the modified frame
128 return frame;
129}
130
131bool Brightness::UseCustomMaskBlend(int64_t frame_number) const {
132 (void) frame_number;
134}
135
136void Brightness::ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
137 std::shared_ptr<QImage> mask_image, int64_t frame_number) const {
138 (void) frame_number;
139 if (!original_image || !effected_image || !mask_image)
140 return;
141 if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
142 return;
143
144 unsigned char* original_pixels = reinterpret_cast<unsigned char*>(original_image->bits());
145 unsigned char* effected_pixels = reinterpret_cast<unsigned char*>(effected_image->bits());
146 unsigned char* mask_pixels = reinterpret_cast<unsigned char*>(mask_image->bits());
147 const int pixel_count = effected_image->width() * effected_image->height();
148
149 if (mask_invert) {
150 #pragma omp parallel for schedule(static)
151 for (int i = 0; i < pixel_count; ++i) {
152 const int idx = i * 4;
153 float factor = static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
154 factor = 1.0f - factor;
155 factor = factor * factor;
156 const float inverse = 1.0f - factor;
157
158 effected_pixels[idx] = static_cast<unsigned char>(
159 (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
160 effected_pixels[idx + 1] = static_cast<unsigned char>(
161 (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
162 effected_pixels[idx + 2] = static_cast<unsigned char>(
163 (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
164 effected_pixels[idx + 3] = original_pixels[idx + 3];
165 }
166 } else {
167 #pragma omp parallel for schedule(static)
168 for (int i = 0; i < pixel_count; ++i) {
169 const int idx = i * 4;
170 float factor = static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
171 factor = factor * factor;
172 const float inverse = 1.0f - factor;
173
174 effected_pixels[idx] = static_cast<unsigned char>(
175 (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
176 effected_pixels[idx + 1] = static_cast<unsigned char>(
177 (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
178 effected_pixels[idx + 2] = static_cast<unsigned char>(
179 (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
180 effected_pixels[idx + 3] = original_pixels[idx + 3];
181 }
182 }
183}
184
185// Generate JSON string of this object
186std::string Brightness::Json() const {
187
188 // Return formatted string
189 return JsonValue().toStyledString();
190}
191
192// Generate Json::Value for this object
193Json::Value Brightness::JsonValue() const {
194
195 // Create root json object
196 Json::Value root = EffectBase::JsonValue(); // get parent properties
197 root["type"] = info.class_name;
198 root["brightness"] = brightness.JsonValue();
199 root["contrast"] = contrast.JsonValue();
200 root["mask_mode"] = mask_mode;
201
202 // return JsonValue
203 return root;
204}
205
206// Load JSON string into this object
207void Brightness::SetJson(const std::string value) {
208
209 // Parse JSON string into JSON objects
210 try
211 {
212 const Json::Value root = openshot::stringToJson(value);
213 // Set all values that match
214 SetJsonValue(root);
215 }
216 catch (const std::exception& e)
217 {
218 // Error parsing JSON (or missing keys)
219 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
220 }
221}
222
223// Load Json::Value into this object
224void Brightness::SetJsonValue(const Json::Value root) {
225
226 // Set parent data
228
229 // Set data from Json (if key is found)
230 if (!root["brightness"].isNull())
231 brightness.SetJsonValue(root["brightness"]);
232 if (!root["contrast"].isNull())
233 contrast.SetJsonValue(root["contrast"]);
234 if (!root["mask_mode"].isNull())
235 mask_mode = root["mask_mode"].asInt();
236}
237
238// Get all properties for a specific frame
239std::string Brightness::PropertiesJSON(int64_t requested_frame) const {
240
241 // Generate JSON properties list
242 Json::Value root = BasePropertiesJSON(requested_frame);
243
244 // Keyframes
245 root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
246 root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, -128, 128.0, false, requested_frame);
247 root["mask_mode"] = add_property_json("Mask Mode", mask_mode, "int", "", NULL, 0, 1, false, requested_frame);
248 root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", BRIGHTNESS_MASK_LIMIT_TO_AREA, mask_mode));
249 root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", BRIGHTNESS_MASK_VARY_STRENGTH, mask_mode));
250
251 // Return formatted string
252 return root.toStyledString();
253}
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:50
std::string Json() const override
Generate JSON string of this object.
void ApplyCustomMaskBlend(std::shared_ptr< QImage > original_image, std::shared_ptr< QImage > effected_image, std::shared_ptr< QImage > mask_image, int64_t frame_number) const override
Optional override for effects with custom mask implementation.
Keyframe contrast
Contrast keyframe.
Definition Brightness.h:51
int mask_mode
Mask behavior mode for this effect.
Definition Brightness.h:52
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:70
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string PropertiesJSON(int64_t requested_frame) const override
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
Brightness()
Blank constructor, useful when using Json to load the effect properties.
void SetJson(const std::string value) override
Load JSON string into this object.
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition ClipBase.cpp:132
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
bool mask_invert
Invert grayscale mask values before blending.
Definition EffectBase.h:111
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:110
Exception for invalid JSON.
Definition Exceptions.h:224
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition KeyFrame.h:53
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition KeyFrame.cpp:372
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:339
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
@ BRIGHTNESS_MASK_LIMIT_TO_AREA
Definition Brightness.h:28
@ BRIGHTNESS_MASK_VARY_STRENGTH
Definition Brightness.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:43
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:44
std::string class_name
The class name of the effect.
Definition EffectBase.h:39
std::string name
The name of the effect.
Definition EffectBase.h:40
std::string description
The description of this effect and what it does.
Definition EffectBase.h:41