OpenShot Library | libopenshot 0.6.0
Loading...
Searching...
No Matches
Mask.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 "Mask.h"
14
15#include "Exceptions.h"
16
17#include "ReaderBase.h"
18#include <array>
19#include <omp.h>
20
21using namespace openshot;
22
24Mask::Mask() : replace_image(false), fade_audio_hint(false) {
25 // Init effect properties
26 init_effect_details();
27}
28
29// Default constructor
30Mask::Mask(ReaderBase *mask_reader, Keyframe mask_brightness, Keyframe mask_contrast) :
31 brightness(mask_brightness), contrast(mask_contrast), replace_image(false), fade_audio_hint(false)
32{
33 // Init effect properties
34 init_effect_details();
35
36 // Keep ownership local by cloning externally-provided readers.
37 if (mask_reader)
39}
40
41// Init effect settings
42void Mask::init_effect_details()
43{
46
48 info.class_name = "Mask";
49 info.name = "Alpha Mask / Wipe Transition";
50 info.description = "Uses a grayscale mask image to gradually wipe / transition between 2 images.";
51 info.has_audio = false;
52 info.has_video = true;
53}
54
55// This method is required for all derived classes of EffectBase, and returns a
56// modified openshot::Frame object
57std::shared_ptr<openshot::Frame> Mask::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) {
58 // Get frame image first
59 std::shared_ptr<QImage> frame_image = frame->GetImage();
60 if (!frame_image || frame_image->isNull())
61 return frame;
62
63 // No reader (bail on applying the mask)
64 auto original_mask = ResolveMaskImage(frame_image, frame_number);
65 if (!original_mask || original_mask->isNull())
66 return frame;
67
68 // Grab raw pointers and dimensions one time
69 unsigned char* pixels = reinterpret_cast<unsigned char*>(frame_image->bits());
70 unsigned char* mask_pixels = reinterpret_cast<unsigned char*>(original_mask->bits());
71 const int num_pixels = original_mask->width() * original_mask->height();
72
73 // Evaluate brightness and contrast keyframes just once
74 double contrast_value = contrast.GetValue(frame_number);
75 double brightness_value = brightness.GetValue(frame_number);
76
77 int brightness_adj = static_cast<int>(255 * brightness_value);
78 float contrast_factor = 20.0f / std::max(0.00001f, 20.0f - static_cast<float>(contrast_value));
79 const bool output_mask = replace_image;
80 const auto clamp_u8 = [](int value) -> unsigned char {
81 if (value < 0) return 0;
82 if (value > 255) return 255;
83 return static_cast<unsigned char>(value);
84 };
85 // Precompute gray->adjusted-gray mapping for this frame's brightness/contrast.
86 std::array<unsigned char, 256> adjusted_gray{};
87 for (int gray = 0; gray < 256; ++gray) {
88 const int adjusted = static_cast<int>(contrast_factor * ((gray + brightness_adj) - 128) + 128);
89 adjusted_gray[gray] = clamp_u8(adjusted);
90 }
91 // 8-bit multiply lookup for premultiplied alpha channel scaling.
92 static const std::array<std::array<unsigned char, 256>, 256> mul_lut = [] {
93 std::array<std::array<unsigned char, 256>, 256> lut{};
94 for (int alpha = 0; alpha < 256; ++alpha) {
95 for (int value = 0; value < 256; ++value) {
96 lut[alpha][value] = static_cast<unsigned char>((value * alpha) / 255);
97 }
98 }
99 return lut;
100 }();
101
102 // Separate loops keep the hot path branch-free per pixel.
103 if (output_mask) {
104 #pragma omp parallel for if(num_pixels >= 16384) schedule(static)
105 for (int i = 0; i < num_pixels; ++i) {
106 const int idx = i * 4;
107 const int R = mask_pixels[idx + 0];
108 const int G = mask_pixels[idx + 1];
109 const int B = mask_pixels[idx + 2];
110 const int A = mask_pixels[idx + 3];
111
112 const int gray = ((R * 11) + (G * 16) + (B * 5)) >> 5;
113 const int diff = A - adjusted_gray[gray];
114 const unsigned char new_val = clamp_u8(diff);
115 pixels[idx + 0] = new_val;
116 pixels[idx + 1] = new_val;
117 pixels[idx + 2] = new_val;
118 pixels[idx + 3] = new_val;
119 }
120 } else if (mask_invert) {
121 #pragma omp parallel for if(num_pixels >= 16384) schedule(static)
122 for (int i = 0; i < num_pixels; ++i) {
123 const int idx = i * 4;
124 const int R = mask_pixels[idx + 0];
125 const int G = mask_pixels[idx + 1];
126 const int B = mask_pixels[idx + 2];
127 const int A = mask_pixels[idx + 3];
128
129 const int gray = ((R * 11) + (G * 16) + (B * 5)) >> 5;
130 int alpha = A - adjusted_gray[gray];
131 if (alpha < 0) alpha = 0;
132 else if (alpha > 255) alpha = 255;
133 alpha = 255 - alpha;
134
135 // Premultiplied RGBA → multiply each channel by alpha
136 pixels[idx + 0] = mul_lut[alpha][pixels[idx + 0]];
137 pixels[idx + 1] = mul_lut[alpha][pixels[idx + 1]];
138 pixels[idx + 2] = mul_lut[alpha][pixels[idx + 2]];
139 pixels[idx + 3] = mul_lut[alpha][pixels[idx + 3]];
140 }
141 } else {
142 #pragma omp parallel for if(num_pixels >= 16384) schedule(static)
143 for (int i = 0; i < num_pixels; ++i) {
144 const int idx = i * 4;
145 const int R = mask_pixels[idx + 0];
146 const int G = mask_pixels[idx + 1];
147 const int B = mask_pixels[idx + 2];
148 const int A = mask_pixels[idx + 3];
149
150 const int gray = ((R * 11) + (G * 16) + (B * 5)) >> 5;
151 int alpha = A - adjusted_gray[gray];
152 if (alpha < 0) alpha = 0;
153 else if (alpha > 255) alpha = 255;
154
155 // Premultiplied RGBA → multiply each channel by alpha
156 pixels[idx + 0] = mul_lut[alpha][pixels[idx + 0]];
157 pixels[idx + 1] = mul_lut[alpha][pixels[idx + 1]];
158 pixels[idx + 2] = mul_lut[alpha][pixels[idx + 2]];
159 pixels[idx + 3] = mul_lut[alpha][pixels[idx + 3]];
160 }
161 }
162
163 // return the modified frame
164 return frame;
165}
166
167// Generate JSON string of this object
168std::string Mask::Json() const {
169
170 // Return formatted string
171 return JsonValue().toStyledString();
172}
173
174// Generate Json::Value for this object
175Json::Value Mask::JsonValue() const {
176
177 // Create root json object
178 Json::Value root = EffectBase::JsonValue(); // get parent properties
179 root["type"] = info.class_name;
180 root["brightness"] = brightness.JsonValue();
181 root["contrast"] = contrast.JsonValue();
182 root["replace_image"] = replace_image;
183 root["fade_audio_hint"] = fade_audio_hint;
184
185 // return JsonValue
186 return root;
187}
188
189// Load JSON string into this object
190void Mask::SetJson(const std::string value) {
191
192 // Parse JSON string into JSON objects
193 try
194 {
195 const Json::Value root = openshot::stringToJson(value);
196 // Set all values that match
197 SetJsonValue(root);
198 }
199 catch (const std::exception& e)
200 {
201 // Error parsing JSON (or missing keys)
202 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
203 }
204}
205
206// Load Json::Value into this object
207void Mask::SetJsonValue(const Json::Value root) {
208 Json::Value normalized_root = root;
209 // Legacy compatibility: keep accepting "reader" on Mask effects.
210 if (!normalized_root["reader"].isNull() && normalized_root["mask_reader"].isNull())
211 normalized_root["mask_reader"] = normalized_root["reader"];
212
213 // Set parent data
214 EffectBase::SetJsonValue(normalized_root);
215
216 // Set data from Json (if key is found)
217 if (!normalized_root["replace_image"].isNull())
218 replace_image = normalized_root["replace_image"].asBool();
219 if (!normalized_root["fade_audio_hint"].isNull())
220 fade_audio_hint = normalized_root["fade_audio_hint"].asBool();
221 if (!normalized_root["brightness"].isNull())
222 brightness.SetJsonValue(normalized_root["brightness"]);
223 if (!normalized_root["contrast"].isNull())
224 contrast.SetJsonValue(normalized_root["contrast"]);
225
226}
227
228// Get all properties for a specific frame
229std::string Mask::PropertiesJSON(int64_t requested_frame) const {
230
231 // Generate JSON properties list
232 Json::Value root = BasePropertiesJSON(requested_frame);
233
234 // Add replace_image choices (dropdown style)
235 root["replace_image"] = add_property_json("Replace Image", replace_image, "int", "", NULL, 0, 1, false, requested_frame);
236 root["replace_image"]["choices"].append(add_property_choice_json("Yes", true, replace_image));
237 root["replace_image"]["choices"].append(add_property_choice_json("No", false, replace_image));
238
239 root["fade_audio_hint"] = add_property_json("Fade Audio", fade_audio_hint, "int", "", NULL, 0, 1, false, requested_frame);
240 root["fade_audio_hint"]["choices"].append(add_property_choice_json("Yes", true, fade_audio_hint));
241 root["fade_audio_hint"]["choices"].append(add_property_choice_json("No", false, fade_audio_hint));
242
243 // Keyframes
244 root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
245 root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, 0, 20, false, requested_frame);
246
247 // Return formatted string
248 return root.toStyledString();
249}
250
251void Mask::Reader(ReaderBase *new_reader) {
252 if (!new_reader) {
253 MaskReader(NULL);
254 return;
255 }
256
257 // Keep ownership local by cloning externally-provided readers.
259}
Header file for all Exception classes.
Header file for Mask class.
Header file for ReaderBase class.
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
ReaderBase * CreateReaderFromJson(const Json::Value &reader_json) const
Create a reader instance from reader JSON.
std::shared_ptr< QImage > ResolveMaskImage(std::shared_ptr< QImage > target_image, int64_t frame_number)
Resolve a cached/scaled mask image for the target frame dimensions.
Definition EffectBase.h:88
ReaderBase * MaskReader()
Get the common mask reader.
Definition EffectBase.h:175
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
ReaderBase * Reader()
Get the reader object of the mask grayscale image.
Definition Mask.h:93
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Mask.cpp:190
Mask()
Blank constructor, useful when using Json to load the effect properties.
Definition Mask.cpp:24
bool replace_image
Replace the frame image with a grayscale image representing the mask. Great for debugging a mask.
Definition Mask.h:46
Keyframe brightness
Brightness keyframe to control the wipe / mask effect. A constant value here will prevent animation.
Definition Mask.h:48
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Mask.cpp:229
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Mask.cpp:175
std::string Json() const override
Generate JSON string of this object.
Definition Mask.cpp:168
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Mask.cpp:207
Keyframe contrast
Contrast keyframe to control the hardness of the wipe effect / mask.
Definition Mask.h:49
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 Mask.h:69
bool fade_audio_hint
Timeline-only hint requesting equal-power audio crossfade for overlapping clips when this Mask is use...
Definition Mask.h:47
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
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: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