OpenShot Library | libopenshot
0.4.0
|
Go to the documentation of this file.
23 #ifdef USE_IMAGEMAGICK
30 Mask::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();
44 void 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.";
59 std::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();
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.
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.