|
OpenShot Library | libopenshot
0.5.0
|
Go to the documentation of this file.
24 #ifdef USE_IMAGEMAGICK
31 Mask::Mask() : reader(NULL), replace_image(false), needs_refresh(true) {
33 init_effect_details();
38 reader(mask_reader), brightness(mask_brightness), contrast(mask_contrast), replace_image(false), needs_refresh(true)
41 init_effect_details();
45 void Mask::init_effect_details()
52 info.
name =
"Alpha Mask / Wipe Transition";
53 info.
description =
"Uses a grayscale mask image to gradually wipe / transition between 2 images.";
60 std::shared_ptr<openshot::Frame>
Mask::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) {
62 std::shared_ptr<QImage> frame_image = frame->GetImage();
63 bool mask_reader_failed =
false;
66 #pragma omp critical (open_mask_reader)
68 if (reader && !reader->
IsOpen()) {
71 }
catch (
const std::exception& e) {
74 std::string(
"Mask::GetFrame unable to open mask reader: ") + e.what());
78 mask_reader_failed =
true;
84 if (!reader || mask_reader_failed)
88 #pragma omp critical (open_mask_reader)
91 (original_mask && original_mask->size() != frame_image->size())) {
94 std::shared_ptr<QImage> mask_without_sizing;
96 mask_without_sizing = std::make_shared<QImage>(
97 *reader->
GetFrame(frame_number)->GetImage());
98 }
catch (
const std::exception& e) {
100 std::string(
"Mask::GetFrame unable to read mask frame: ") + e.what());
103 needs_refresh =
true;
104 mask_reader_failed =
true;
106 if (!mask_reader_failed && mask_without_sizing) {
108 original_mask = std::make_shared<QImage>(
109 mask_without_sizing->scaled(
110 frame_image->width(), frame_image->height(),
111 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
115 if (mask_reader_failed || !reader || !original_mask)
119 needs_refresh =
false;
122 unsigned char* pixels =
reinterpret_cast<unsigned char*
>(frame_image->bits());
123 unsigned char* mask_pixels =
reinterpret_cast<unsigned char*
>(original_mask->bits());
124 int width = original_mask->width();
125 int height = original_mask->height();
126 int num_pixels = width * height;
132 int brightness_adj =
static_cast<int>(255 * brightness_value);
133 float contrast_factor = 20.0f / std::max(0.00001f, 20.0f -
static_cast<float>(contrast_value));
136 #pragma omp parallel for schedule(static)
137 for (
int i = 0; i < num_pixels; ++i)
141 int R = mask_pixels[idx + 0];
142 int G = mask_pixels[idx + 1];
143 int B = mask_pixels[idx + 2];
144 int A = mask_pixels[idx + 3];
147 int gray = qGray(R, G, B);
148 gray += brightness_adj;
149 gray =
static_cast<int>(contrast_factor * (gray - 128) + 128);
153 if (diff < 0) diff = 0;
154 else if (diff > 255) diff = 255;
157 float alpha_percent =
static_cast<float>(diff) / 255.0f;
162 auto new_val =
static_cast<unsigned char>(diff);
163 pixels[idx + 0] = new_val;
164 pixels[idx + 1] = new_val;
165 pixels[idx + 2] = new_val;
166 pixels[idx + 3] = new_val;
169 pixels[idx + 0] =
static_cast<unsigned char>(pixels[idx + 0] * alpha_percent);
170 pixels[idx + 1] =
static_cast<unsigned char>(pixels[idx + 1] * alpha_percent);
171 pixels[idx + 2] =
static_cast<unsigned char>(pixels[idx + 2] * alpha_percent);
172 pixels[idx + 3] =
static_cast<unsigned char>(pixels[idx + 3] * alpha_percent);
199 root[
"reader"] = Json::objectValue;
216 catch (
const std::exception& e)
219 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
230 if (!root[
"replace_image"].isNull())
232 if (!root[
"brightness"].isNull())
234 if (!root[
"contrast"].isNull())
236 if (!root[
"reader"].isNull())
238 #pragma omp critical (open_mask_reader)
241 needs_refresh =
true;
243 if (!root[
"reader"][
"type"].isNull())
254 std::string type = root[
"reader"][
"type"].asString();
256 if (type ==
"FFmpegReader") {
259 reader =
new FFmpegReader(root[
"reader"][
"path"].asString(),
false);
262 #ifdef USE_IMAGEMAGICK
263 }
else if (type ==
"ImageReader") {
266 reader =
new ImageReader(root[
"reader"][
"path"].asString(),
false);
270 }
else if (type ==
"QtImageReader") {
273 reader =
new QtImageReader(root[
"reader"][
"path"].asString(),
false);
276 }
else if (type ==
"ChunkReader") {
279 reader =
new ChunkReader(root[
"reader"][
"path"].asString(), (
ChunkVersion) root[
"reader"][
"chunk_version"].asInt());
306 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader", reader->
Json(), NULL, 0, 1,
false, requested_frame);
308 root[
"reader"] =
add_property_json(
"Source", 0.0,
"reader",
"{}", NULL, 0, 1,
false, requested_frame);
311 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)
void Log(std::string message)
Log message to all subscribers of this logger (if any)
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)
Header file for ZeroMQ-based Logger class.
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.
static ZmqLogger * Instance()
Create or get an instance of this logger singleton (invoke the class with this method)
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.