OpenShot Library | libopenshot 0.2.7
Deinterlace.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for De-interlace class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 *
6 * @ref License
7 */
8
9/* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#include "Deinterlace.h"
32#include "Exceptions.h"
33
34using namespace openshot;
35
36/// Blank constructor, useful when using Json to load the effect properties
37Deinterlace::Deinterlace() : isOdd(true)
38{
39 // Init effect properties
40 init_effect_details();
41}
42
43// Default constructor
44Deinterlace::Deinterlace(bool UseOddLines) : isOdd(UseOddLines)
45{
46 // Init effect properties
47 init_effect_details();
48}
49
50// Init effect settings
51void Deinterlace::init_effect_details()
52{
53 /// Initialize the values of the EffectInfo struct.
55
56 /// Set the effect info
57 info.class_name = "Deinterlace";
58 info.name = "Deinterlace";
59 info.description = "Remove interlacing from a video (i.e. even or odd horizontal lines)";
60 info.has_audio = false;
61 info.has_video = true;
62}
63
64// This method is required for all derived classes of EffectBase, and returns a
65// modified openshot::Frame object
66std::shared_ptr<openshot::Frame> Deinterlace::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
67{
68 // Get original size of frame's image
69 int original_width = frame->GetImage()->width();
70 int original_height = frame->GetImage()->height();
71
72 // Get the frame's image
73 std::shared_ptr<QImage> image = frame->GetImage();
74 const unsigned char* pixels = image->bits();
75
76 // Create a smaller, new image
77 QImage deinterlaced_image(image->width(), image->height() / 2, QImage::Format_RGBA8888_Premultiplied);
78 const unsigned char* deinterlaced_pixels = deinterlaced_image.bits();
79
80 // Loop through the scanlines of the image (even or odd)
81 int start = 0;
82 if (isOdd)
83 start = 1;
84 for (int row = start; row < image->height(); row += 2) {
85 memcpy((unsigned char*)deinterlaced_pixels, pixels + (row * image->bytesPerLine()), image->bytesPerLine());
86 deinterlaced_pixels += image->bytesPerLine();
87 }
88
89 // Resize deinterlaced image back to original size, and update frame's image
90 image = std::make_shared<QImage>(deinterlaced_image.scaled(
91 original_width, original_height,
92 Qt::IgnoreAspectRatio, Qt::FastTransformation));
93
94 // Update image on frame
95 frame->AddImage(image);
96
97 // return the modified frame
98 return frame;
99}
100
101// Generate JSON string of this object
102std::string Deinterlace::Json() const {
103
104 // Return formatted string
105 return JsonValue().toStyledString();
106}
107
108// Generate Json::Value for this object
109Json::Value Deinterlace::JsonValue() const {
110
111 // Create root json object
112 Json::Value root = EffectBase::JsonValue(); // get parent properties
113 root["type"] = info.class_name;
114 root["isOdd"] = isOdd;
115
116 // return JsonValue
117 return root;
118}
119
120// Load JSON string into this object
121void Deinterlace::SetJson(const std::string value) {
122
123 // Parse JSON string into JSON objects
124 try
125 {
126 const Json::Value root = openshot::stringToJson(value);
127 // Set all values that match
128 SetJsonValue(root);
129 }
130 catch (const std::exception& e)
131 {
132 // Error parsing JSON (or missing keys)
133 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
134 }
135}
136
137// Load Json::Value into this object
138void Deinterlace::SetJsonValue(const Json::Value root) {
139
140 // Set parent data
142
143 // Set data from Json (if key is found)
144 if (!root["isOdd"].isNull())
145 isOdd = root["isOdd"].asBool();
146}
147
148// Get all properties for a specific frame
149std::string Deinterlace::PropertiesJSON(int64_t requested_frame) const {
150
151 // Generate JSON properties list
152 Json::Value root;
153 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
154 root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
155 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
156 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
157 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
158 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
159 root["isOdd"] = add_property_json("Is Odd Frame", isOdd, "bool", "", NULL, 0, 1, true, requested_frame);
160
161 // Add Is Odd Frame choices (dropdown style)
162 root["isOdd"]["choices"].append(add_property_choice_json("Yes", true, isOdd));
163 root["isOdd"]["choices"].append(add_property_choice_json("No", false, isOdd));
164
165 // Set the parent effect which properties this effect will inherit
166 root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
167
168 // Return formatted string
169 return root.toStyledString();
170}
Header file for De-interlace class.
Header file for all Exception classes.
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
float start
The position in seconds to start playing (used to trim the beginning of a clip)
Definition: ClipBase.h:56
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition: ClipBase.cpp:104
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
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:68
std::string Json() const override
Generate JSON string of this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Deinterlace()
Default constructor, useful when using Json to load the effect properties.
Definition: Deinterlace.cpp:37
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: Deinterlace.h:74
void SetJson(const std::string value) override
Load JSON string into this object.
std::string PropertiesJSON(int64_t requested_frame) const override
Json::Value JsonValue() const override
Generate Json::Value for this object.
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87
Exception for invalid JSON.
Definition: Exceptions.h:206
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56