OpenShot Library | libopenshot 0.5.0
Loading...
Searching...
No Matches
Outline.cpp
Go to the documentation of this file.
1
8
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include "Outline.h"
14#include "Exceptions.h"
15
16using namespace openshot;
17
20 // Init effect properties
21 color = Color("#FFFFFF");
22 init_effect_details();
23}
24
25// Default constructor
28{
29 // Init effect properties
30 init_effect_details();
31}
32
33// Init effect settings
34void Outline::init_effect_details()
35{
38
40 info.class_name = "Outline";
41 info.name = "Outline";
42 info.description = "Add outline around any image or text.";
43 info.has_audio = false;
44 info.has_video = true;
45}
46
47// This method is required for all derived classes of EffectBase, and returns a
48// modified openshot::Frame object
49std::shared_ptr<openshot::Frame> Outline::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
50{
51 float widthValue = width.GetValue(frame_number);
52 int blueValue = color.blue.GetValue(frame_number);
53 int greenValue = color.green.GetValue(frame_number);
54 int redValue = color.red.GetValue(frame_number);
55 int alphaValue = color.alpha.GetValue(frame_number);
56
57 if (widthValue <= 0.0 || alphaValue <= 0) {
58 // If alpha or width is zero, return the original frame
59 return frame;
60 }
61
62 // Get the frame's image
63 std::shared_ptr<QImage> frame_image = frame->GetImage();
64
65 float sigmaValue = widthValue / 3.0;
66 if (sigmaValue <= 0.0)
67 sigmaValue = 0.01;
68 cv::Mat cv_image = QImageToBGRACvMat(frame_image);
69
70 // Extract alpha channel for the mask
71 std::vector<cv::Mat> channels(4);
72 cv::split(cv_image, channels);
73 cv::Mat alpha_mask = channels[3].clone();
74
75 // Create the outline mask
76 cv::Mat outline_mask;
77 cv::GaussianBlur(alpha_mask, outline_mask, cv::Size(0, 0), sigmaValue, sigmaValue, cv::BorderTypes::BORDER_DEFAULT);
78 cv::threshold(outline_mask, outline_mask, 0, 255, cv::ThresholdTypes::THRESH_BINARY);
79
80 // Antialias the outline edge & apply Canny edge detection
81 cv::Mat edge_mask;
82 cv::Canny(outline_mask, edge_mask, 250, 255);
83
84 // Apply Gaussian blur only to the edge mask
85 cv::Mat blurred_edge_mask;
86 cv::GaussianBlur(edge_mask, blurred_edge_mask, cv::Size(0, 0), 0.8, 0.8, cv::BorderTypes::BORDER_DEFAULT);
87 cv::bitwise_or(outline_mask, blurred_edge_mask, outline_mask);
88
89 cv::Mat final_image;
90
91 // Create solid color source mat (cv::Scalar: red, green, blue, alpha)
92 cv::Mat solid_color_mat(cv::Size(cv_image.cols, cv_image.rows), CV_8UC4, cv::Scalar(redValue, greenValue, blueValue, alphaValue));
93
94 // Place outline first, then the original image on top
95 solid_color_mat.copyTo(final_image, outline_mask);
96 cv_image.copyTo(final_image, alpha_mask);
97
98 std::shared_ptr<QImage> new_frame_image = BGRACvMatToQImage(final_image);
99
100 // FIXME: The shared_ptr::swap does not work somehow
101 *frame_image = *new_frame_image;
102 return frame;
103}
104
105cv::Mat Outline::QImageToBGRACvMat(std::shared_ptr<QImage>& qimage) {
106 cv::Mat cv_img(qimage->height(), qimage->width(), CV_8UC4, (uchar*)qimage->constBits(), qimage->bytesPerLine());
107 return cv_img;
108}
109
110std::shared_ptr<QImage> Outline::BGRACvMatToQImage(cv::Mat img) {
111 cv::Mat final_img;
112 cv::cvtColor(img, final_img, cv::COLOR_RGBA2BGRA);
113 QImage qimage(final_img.data, final_img.cols, final_img.rows, final_img.step, QImage::Format_ARGB32);
114 std::shared_ptr<QImage> imgIn = std::make_shared<QImage>(qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied));
115 return imgIn;
116}
117
118// Generate JSON string of this object
119std::string Outline::Json() const {
120
121 // Return formatted string
122 return JsonValue().toStyledString();
123}
124
125// Generate Json::Value for this object
126Json::Value Outline::JsonValue() const {
127
128 // Create root json object
129 Json::Value root = EffectBase::JsonValue(); // get parent properties
130 root["type"] = info.class_name;
131 root["width"] = width.JsonValue();
132 root["color"] = color.JsonValue();
133
134 // return JsonValue
135 return root;
136}
137
138// Load JSON string into this object
139void Outline::SetJson(const std::string value) {
140
141 // Parse JSON string into JSON objects
142 try
143 {
144 const Json::Value root = openshot::stringToJson(value);
145 // Set all values that match
146 SetJsonValue(root);
147 }
148 catch (const std::exception& e)
149 {
150 // Error parsing JSON (or missing keys)
151 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
152 }
153}
154
155// Load Json::Value into this object
156void Outline::SetJsonValue(const Json::Value root) {
157
158 // Set parent data
160
161 // Set data from Json (if key is found)
162 if (!root["width"].isNull())
163 width.SetJsonValue(root["width"]);
164 if (!root["color"].isNull())
165 color.SetJsonValue(root["color"]);
166}
167
168// Get all properties for a specific frame
169std::string Outline::PropertiesJSON(int64_t requested_frame) const {
170
171 // Generate JSON properties list
172 Json::Value root = BasePropertiesJSON(requested_frame);
173
174 // Keyframes
175 root["width"] = add_property_json("Width", width.GetValue(requested_frame), "float", "", &width, 0, 100, false, requested_frame);
176 root["color"] = add_property_json("Key Color", 0.0, "color", "", &color.red, 0, 255, false, requested_frame);
177 root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
178 root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
179 root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
180 root["color"]["alpha"] = add_property_json("Alpha", color.alpha.GetValue(requested_frame), "float", "", &color.alpha, 0, 255, false, requested_frame);
181
182 // Return formatted string
183 return root.toStyledString();
184}
Header file for all Exception classes.
Header file for Outline effect class.
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.
Definition ClipBase.cpp:96
This class represents a color (used on the timeline and clips).
Definition Color.h:27
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects).
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
Exception for invalid JSON.
Definition Exceptions.h:218
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition KeyFrame.h:53
std::string Json() const override
Generate JSON string of this object.
Definition Outline.cpp:119
Color color
Color of the outline.
Definition Outline.h:52
Keyframe width
Width of the outline.
Definition Outline.h:51
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...
Definition Outline.h:69
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Outline.cpp:156
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Outline.cpp:139
Outline()
Blank constructor, useful when using Json to load the effect properties.
Definition Outline.cpp:19
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Outline.cpp:126
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Outline.cpp:169
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
std::string name
The name of the effect.
Definition EffectBase.h:37
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38