34 #ifdef USE_IMAGEMAGICK 42 Mask::Mask() : reader(NULL), replace_image(false), needs_refresh(true) {
44 init_effect_details();
52 init_effect_details();
56 void Mask::init_effect_details()
63 info.
name =
"Alpha Mask / Wipe Transition";
64 info.
description =
"Uses a grayscale mask image to gradually wipe / transition between 2 images.";
71 std::shared_ptr<openshot::Frame>
Mask::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) {
73 std::shared_ptr<QImage> frame_image = frame->GetImage();
76 #pragma omp critical (open_mask_reader) 78 if (reader && !reader->
IsOpen())
87 #pragma omp critical (open_mask_reader) 90 (original_mask && original_mask->size() != frame_image->size())) {
93 auto mask_without_sizing = std::make_shared<QImage>(
94 *reader->
GetFrame(frame_number)->GetImage());
97 original_mask = std::make_shared<QImage>(
98 mask_without_sizing->scaled(
99 frame_image->width(), frame_image->height(),
100 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
105 needs_refresh =
false;
108 unsigned char *pixels = (
unsigned char *) frame_image->bits();
109 unsigned char *mask_pixels = (
unsigned char *) original_mask->bits();
115 for (
int pixel = 0, byte_index=0; pixel < original_mask->width() * original_mask->height(); pixel++, byte_index+=4)
118 int R = mask_pixels[byte_index];
119 int G = mask_pixels[byte_index + 1];
120 int B = mask_pixels[byte_index + 2];
121 int A = mask_pixels[byte_index + 3];
124 int gray_value = qGray(R, G, B);
127 float factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
128 gray_value =
constrain((factor * (gray_value - 128)) + 128);
131 gray_value += (255 * brightness_value);
137 float alpha_percent = float(
constrain(A - gray_value)) / 255.0;
142 pixels[byte_index + 0] = gray_value;
143 pixels[byte_index + 1] = gray_value;
144 pixels[byte_index + 2] = gray_value;
145 pixels[byte_index + 3] = gray_value;
149 pixels[byte_index + 0] *= alpha_percent;
150 pixels[byte_index + 1] *= alpha_percent;
151 pixels[byte_index + 2] *= alpha_percent;
152 pixels[byte_index + 3] *= alpha_percent;
179 root[
"reader"] = Json::objectValue;
196 catch (
const std::exception& e)
199 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
210 if (!root[
"replace_image"].isNull())
212 if (!root[
"brightness"].isNull())
214 if (!root[
"contrast"].isNull())
216 if (!root[
"reader"].isNull())
218 #pragma omp critical (open_mask_reader) 221 needs_refresh =
true;
223 if (!root[
"reader"][
"type"].isNull())
234 std::string type = root[
"reader"][
"type"].asString();
236 if (type ==
"FFmpegReader") {
239 reader =
new FFmpegReader(root[
"reader"][
"path"].asString());
242 #ifdef USE_IMAGEMAGICK 243 }
else if (type ==
"ImageReader") {
246 reader =
new ImageReader(root[
"reader"][
"path"].asString());
250 }
else if (type ==
"QtImageReader") {
253 reader =
new QtImageReader(root[
"reader"][
"path"].asString());
256 }
else if (type ==
"ChunkReader") {
259 reader =
new ChunkReader(root[
"reader"][
"path"].asString(), (
ChunkVersion) root[
"reader"][
"chunk_version"].asInt());
275 root[
"id"] =
add_property_json(
"ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
276 root[
"position"] =
add_property_json(
"Position",
Position(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
false, requested_frame);
278 root[
"start"] =
add_property_json(
"Start",
Start(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
false, requested_frame);
279 root[
"end"] =
add_property_json(
"End",
End(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
false, requested_frame);
280 root[
"duration"] =
add_property_json(
"Duration",
Duration(),
"float",
"", NULL, 0, 30 * 60 * 60 * 48,
true, requested_frame);
292 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader", reader->
Json(), NULL, 0, 1,
false, requested_frame);
294 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader",
"{}", NULL, 0, 1,
false, requested_frame);
300 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.
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.
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.
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)
float End() const
Get end position (in seconds) of clip (trim end of video)
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.