30Mask::Mask() : reader(NULL), replace_image(false), needs_refresh(true) {
32 init_effect_details();
37 reader(mask_reader), brightness(mask_brightness), contrast(mask_contrast), replace_image(false), needs_refresh(true)
40 init_effect_details();
44void Mask::init_effect_details()
51 info.
name =
"Alpha Mask / Wipe Transition";
52 info.
description =
"Uses a grayscale mask image to gradually wipe / transition between 2 images.";
59std::shared_ptr<openshot::Frame>
Mask::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) {
61 std::shared_ptr<QImage> frame_image = frame->GetImage();
64 #pragma omp critical (open_mask_reader)
66 if (reader && !reader->
IsOpen())
75 #pragma omp critical (open_mask_reader)
78 (original_mask && original_mask->size() != frame_image->size())) {
81 auto mask_without_sizing = std::make_shared<QImage>(
82 *reader->
GetFrame(frame_number)->GetImage());
85 original_mask = std::make_shared<QImage>(
86 mask_without_sizing->scaled(
87 frame_image->width(), frame_image->height(),
88 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
93 needs_refresh =
false;
96 unsigned char* pixels =
reinterpret_cast<unsigned char*
>(frame_image->bits());
97 unsigned char* mask_pixels =
reinterpret_cast<unsigned char*
>(original_mask->bits());
98 int width = original_mask->width();
99 int height = original_mask->height();
100 int num_pixels = width * height;
106 int brightness_adj =
static_cast<int>(255 * brightness_value);
107 float contrast_factor = 20.0f / std::max(0.00001f, 20.0f -
static_cast<float>(contrast_value));
110#pragma omp parallel for schedule(static)
111 for (
int i = 0; i < num_pixels; ++i)
115 int R = mask_pixels[idx + 0];
116 int G = mask_pixels[idx + 1];
117 int B = mask_pixels[idx + 2];
118 int A = mask_pixels[idx + 3];
121 int gray = qGray(R, G, B);
122 gray += brightness_adj;
123 gray =
static_cast<int>(contrast_factor * (gray - 128) + 128);
127 if (diff < 0) diff = 0;
128 else if (diff > 255) diff = 255;
131 float alpha_percent =
static_cast<float>(diff) / 255.0f;
136 auto new_val =
static_cast<unsigned char>(diff);
137 pixels[idx + 0] = new_val;
138 pixels[idx + 1] = new_val;
139 pixels[idx + 2] = new_val;
140 pixels[idx + 3] = new_val;
143 pixels[idx + 0] =
static_cast<unsigned char>(pixels[idx + 0] * alpha_percent);
144 pixels[idx + 1] =
static_cast<unsigned char>(pixels[idx + 1] * alpha_percent);
145 pixels[idx + 2] =
static_cast<unsigned char>(pixels[idx + 2] * alpha_percent);
146 pixels[idx + 3] =
static_cast<unsigned char>(pixels[idx + 3] * alpha_percent);
173 root[
"reader"] = Json::objectValue;
190 catch (
const std::exception& e)
193 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
204 if (!root[
"replace_image"].isNull())
206 if (!root[
"brightness"].isNull())
208 if (!root[
"contrast"].isNull())
210 if (!root[
"reader"].isNull())
212 #pragma omp critical (open_mask_reader)
215 needs_refresh =
true;
217 if (!root[
"reader"][
"type"].isNull())
228 std::string type = root[
"reader"][
"type"].asString();
230 if (type ==
"FFmpegReader") {
233 reader =
new FFmpegReader(root[
"reader"][
"path"].asString());
236 #ifdef USE_IMAGEMAGICK
237 }
else if (type ==
"ImageReader") {
240 reader =
new ImageReader(root[
"reader"][
"path"].asString());
244 }
else if (type ==
"QtImageReader") {
247 reader =
new QtImageReader(root[
"reader"][
"path"].asString());
250 }
else if (type ==
"ChunkReader") {
253 reader =
new ChunkReader(root[
"reader"][
"path"].asString(), (
ChunkVersion) root[
"reader"][
"chunk_version"].asInt());
280 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader", reader->
Json(), NULL, 0, 1,
false, requested_frame);
282 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader",
"{}", NULL, 0, 1,
false, requested_frame);
285 return root.toStyledString();
Header file for ChunkReader class.
Header file for all Exception classes.
Header file for FFmpegReader class.
Header file for ImageReader class.
Header file for Mask class.
Header file for QtImageReader class.
Header file for ReaderBase class.
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
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.
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.
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
This class uses the ImageMagick++ libraries, to open image files, and return openshot::Frame objects ...
Exception for invalid JSON.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
double GetValue(int64_t index) const
Get the value at a specific index.
Json::Value JsonValue() const
Generate Json::Value for this object.
void SetJson(const std::string value) override
Load JSON string into this object.
Mask()
Blank constructor, useful when using Json to load the effect properties.
bool replace_image
Replace the frame image with a grayscale image representing the mask. Great for debugging a mask.
Keyframe brightness
Brightness keyframe to control the wipe / mask effect. A constant value here will prevent animation.
std::string PropertiesJSON(int64_t requested_frame) const override
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Json() const override
Generate JSON string of this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Keyframe contrast
Contrast keyframe to control the hardness of the wipe effect / mask.
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...
This class uses the Qt library, to open image files, and return openshot::Frame objects containing th...
This abstract class is the base class, used by all readers in libopenshot.
virtual bool IsOpen()=0
Determine if reader is open or closed.
openshot::ReaderInfo info
Information about the current media file.
virtual std::string Json() const =0
Generate JSON string of this object.
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
virtual void Close()=0
Close the reader (and any resources it was consuming)
This namespace is the default namespace for all code in the openshot library.
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low,...
const Json::Value stringToJson(const std::string value)
bool has_video
Determines if this effect manipulates the image of a frame.
bool has_audio
Determines if this effect manipulates the audio of a frame.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
std::string description
The description of this effect and what it does.
bool has_single_image
Determines if this file only contains a single image.