22 #ifdef USE_IMAGEMAGICK 29 Mask::Mask() : reader(NULL), replace_image(false), needs_refresh(true) {
31 init_effect_details();
39 init_effect_details();
43 void Mask::init_effect_details()
50 info.
name =
"Alpha Mask / Wipe Transition";
51 info.
description =
"Uses a grayscale mask image to gradually wipe / transition between 2 images.";
58 std::shared_ptr<openshot::Frame>
Mask::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) {
60 std::shared_ptr<QImage> frame_image = frame->GetImage();
63 #pragma omp critical (open_mask_reader) 65 if (reader && !reader->
IsOpen())
74 #pragma omp critical (open_mask_reader) 77 (original_mask && original_mask->size() != frame_image->size())) {
80 auto mask_without_sizing = std::make_shared<QImage>(
81 *reader->
GetFrame(frame_number)->GetImage());
84 original_mask = std::make_shared<QImage>(
85 mask_without_sizing->scaled(
86 frame_image->width(), frame_image->height(),
87 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
92 needs_refresh =
false;
95 unsigned char *pixels = (
unsigned char *) frame_image->bits();
96 unsigned char *mask_pixels = (
unsigned char *) original_mask->bits();
102 for (
int pixel = 0, byte_index=0; pixel < original_mask->width() * original_mask->height(); pixel++, byte_index+=4)
105 int R = mask_pixels[byte_index];
106 int G = mask_pixels[byte_index + 1];
107 int B = mask_pixels[byte_index + 2];
108 int A = mask_pixels[byte_index + 3];
111 int gray_value = qGray(R, G, B);
114 gray_value += (255 * brightness_value);
117 float factor = (20 / std::fmax(0.00001, 20.0 - contrast_value));
118 gray_value = (factor * (gray_value - 128) + 128);
121 float alpha_percent = float(
constrain(A - gray_value)) / 255.0;
126 pixels[byte_index + 0] =
constrain(255 * alpha_percent);
127 pixels[byte_index + 1] =
constrain(255 * alpha_percent);
128 pixels[byte_index + 2] =
constrain(255 * alpha_percent);
129 pixels[byte_index + 3] =
constrain(255 * alpha_percent);
133 pixels[byte_index + 0] *= alpha_percent;
134 pixels[byte_index + 1] *= alpha_percent;
135 pixels[byte_index + 2] *= alpha_percent;
136 pixels[byte_index + 3] *= alpha_percent;
163 root[
"reader"] = Json::objectValue;
180 catch (
const std::exception& e)
183 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
194 if (!root[
"replace_image"].isNull())
196 if (!root[
"brightness"].isNull())
198 if (!root[
"contrast"].isNull())
200 if (!root[
"reader"].isNull())
202 #pragma omp critical (open_mask_reader) 205 needs_refresh =
true;
207 if (!root[
"reader"][
"type"].isNull())
218 std::string type = root[
"reader"][
"type"].asString();
220 if (type ==
"FFmpegReader") {
223 reader =
new FFmpegReader(root[
"reader"][
"path"].asString());
226 #ifdef USE_IMAGEMAGICK 227 }
else if (type ==
"ImageReader") {
230 reader =
new ImageReader(root[
"reader"][
"path"].asString());
234 }
else if (type ==
"QtImageReader") {
237 reader =
new QtImageReader(root[
"reader"][
"path"].asString());
240 }
else if (type ==
"ChunkReader") {
243 reader =
new ChunkReader(root[
"reader"][
"path"].asString(), (
ChunkVersion) root[
"reader"][
"chunk_version"].asInt());
259 root[
"id"] =
add_property_json(
"ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
260 root[
"position"] =
add_property_json(
"Position",
Position(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
false, requested_frame);
262 root[
"start"] =
add_property_json(
"Start",
Start(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
false, requested_frame);
263 root[
"end"] =
add_property_json(
"End",
End(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
false, requested_frame);
264 root[
"duration"] =
add_property_json(
"Duration",
Duration(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
true, requested_frame);
276 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader", reader->
Json(), NULL, 0, 1,
false, requested_frame);
278 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader",
"{}", NULL, 0, 1,
false, requested_frame);
284 return root.toStyledString();
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
Json::Value JsonValue() const override
Generate Json::Value for this object.
bool replace_image
Replace the frame image with a grayscale image representing the mask. Great for debugging a mask...
std::string Id() const
Get the Id of this clip object.
Header file for ReaderBase class.
float Start() const
Get start position (in seconds) of clip (trim start of video)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Header file for FFmpegReader class.
Header file for Mask class.
virtual void Close()=0
Close the reader (and any resources it was consuming)
const Json::Value stringToJson(const std::string value)
This abstract class is the base class, used by all readers in libopenshot.
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
bool has_audio
Determines if this effect manipulates the audio of a frame.
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
This class uses the ImageMagick++ libraries, to open image files, and return openshot::Frame objects ...
Header file for all Exception classes.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Keyframe contrast
Contrast keyframe to control the hardness of the wipe effect / mask.
virtual std::string Json() const =0
Generate JSON string of this object.
Header file for ChunkReader class.
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...
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
bool has_single_image
Determines if this file only contains a single image.
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low...
Mask()
Blank constructor, useful when using Json to load the effect properties.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
openshot::ReaderInfo info
Information about the current media file.
std::string PropertiesJSON(int64_t requested_frame) const override
float Duration() const
Get the length of this clip (in seconds)
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
This namespace is the default namespace for all code in the openshot library.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Json::Value JsonValue() const
Generate Json::Value for this object.
std::string description
The description of this effect and what it does.
Keyframe brightness
Brightness keyframe to control the wipe / mask effect. A constant value here will prevent animation...
bool has_video
Determines if this effect manipulates the image of a frame.
Exception for invalid JSON.
double GetValue(int64_t index) const
Get the value at a specific index.
Header file for QtImageReader class.
int constrain(int color_value)
Constrain a color value from 0 to 255.
std::string parent_effect_id
Id of the parent effect (if there is one)
std::string Json() const override
Generate JSON string of this object.
Header file for ImageReader class.
float Position() const
Get position on timeline (in seconds)
This class uses the Qt library, to open image files, and return openshot::Frame objects containing th...
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
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.
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
EffectInfoStruct info
Information about the current effect.
virtual bool IsOpen()=0
Determine if reader is open or closed.