OpenShot Library | libopenshot
0.3.3
|
Go to the documentation of this file.
22 #ifdef USE_IMAGEMAGICK
29 Mask::Mask() : reader(NULL), replace_image(false), needs_refresh(true) {
31 init_effect_details();
36 reader(mask_reader), brightness(mask_brightness), contrast(mask_contrast), replace_image(false), needs_refresh(true)
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());
270 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader", reader->
Json(), NULL, 0, 1,
false, requested_frame);
272 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader",
"{}", NULL, 0, 1,
false, requested_frame);
275 return root.toStyledString();
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.
const Json::Value stringToJson(const std::string value)
Json::Value JsonValue() const override
Generate Json::Value for this object.
EffectInfoStruct info
Information about the current effect.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual std::string Json() const =0
Generate JSON string of this object.
This namespace is the default namespace for all code in the openshot library.
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)
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Keyframe contrast
Contrast keyframe to control the hardness of the wipe effect / mask.
openshot::ReaderInfo info
Information about the current media file.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Mask()
Blank constructor, useful when using Json to load the effect properties.
Json::Value JsonValue() const
Generate Json::Value for this object.
std::string PropertiesJSON(int64_t requested_frame) const override
This class uses the Qt library, to open image files, and return openshot::Frame objects containing th...
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
virtual bool IsOpen()=0
Determine if reader is open or closed.
Exception for invalid JSON.
This class uses the ImageMagick++ libraries, to open image files, and return openshot::Frame objects ...
bool has_audio
Determines if this effect manipulates the audio of a frame.
bool has_single_image
Determines if this file only contains a single image.
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Header file for ChunkReader class.
Header file for Mask class.
bool replace_image
Replace the frame image with a grayscale image representing the mask. Great for debugging a mask.
std::string class_name
The class name of the effect.
Header file for ReaderBase class.
std::string description
The description of this effect and what it does.
bool has_video
Determines if this effect manipulates the image of a frame.
std::string Json() const override
Generate JSON string of this object.
int constrain(int color_value)
Constrain a color value from 0 to 255.
This abstract class is the base class, used by all readers in libopenshot.
std::string name
The name of the effect.
virtual void Close()=0
Close the reader (and any resources it was consuming)
void SetJson(const std::string value) override
Load JSON string into this object.
Header file for QtImageReader class.
Keyframe brightness
Brightness keyframe to control the wipe / mask effect. A constant value here will prevent animation.
Header file for ImageReader class.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low,...
Header file for all Exception classes.
Header file for FFmpegReader class.
virtual 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.