OpenShot Library | libopenshot  0.2.5
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 "../../include/effects/Deinterlace.h"
32 
33 using namespace openshot;
34 
35 /// Blank constructor, useful when using Json to load the effect properties
36 Deinterlace::Deinterlace() : isOdd(true)
37 {
38  // Init effect properties
39  init_effect_details();
40 }
41 
42 // Default constructor
43 Deinterlace::Deinterlace(bool UseOddLines) : isOdd(UseOddLines)
44 {
45  // Init effect properties
46  init_effect_details();
47 }
48 
49 // Init effect settings
50 void Deinterlace::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Deinterlace";
57  info.name = "Deinterlace";
58  info.description = "Remove interlacing from a video (i.e. even or odd horizontal lines)";
59  info.has_audio = false;
60  info.has_video = true;
61 }
62 
63 // This method is required for all derived classes of EffectBase, and returns a
64 // modified openshot::Frame object
65 std::shared_ptr<Frame> Deinterlace::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
66 {
67  // Get original size of frame's image
68  int original_width = frame->GetImage()->width();
69  int original_height = frame->GetImage()->height();
70 
71  // Get the frame's image
72  std::shared_ptr<QImage> image = frame->GetImage();
73  const unsigned char* pixels = image->bits();
74 
75  // Create a smaller, new image
76  QImage deinterlaced_image(image->width(), image->height() / 2, QImage::Format_RGBA8888);
77  const unsigned char* deinterlaced_pixels = deinterlaced_image.bits();
78 
79  // Loop through the scanlines of the image (even or odd)
80  int start = 0;
81  if (isOdd)
82  start = 1;
83  for (int row = start; row < image->height(); row += 2) {
84  memcpy((unsigned char*)deinterlaced_pixels, pixels + (row * image->bytesPerLine()), image->bytesPerLine());
85  deinterlaced_pixels += image->bytesPerLine();
86  }
87 
88  // Resize deinterlaced image back to original size, and update frame's image
89  image = std::shared_ptr<QImage>(new QImage(deinterlaced_image.scaled(original_width, original_height, Qt::IgnoreAspectRatio, Qt::FastTransformation)));
90 
91  // Update image on frame
92  frame->AddImage(image);
93 
94  // return the modified frame
95  return frame;
96 }
97 
98 // Generate JSON string of this object
99 std::string Deinterlace::Json() const {
100 
101  // Return formatted string
102  return JsonValue().toStyledString();
103 }
104 
105 // Generate Json::Value for this object
106 Json::Value Deinterlace::JsonValue() const {
107 
108  // Create root json object
109  Json::Value root = EffectBase::JsonValue(); // get parent properties
110  root["type"] = info.class_name;
111  root["isOdd"] = isOdd;
112 
113  // return JsonValue
114  return root;
115 }
116 
117 // Load JSON string into this object
118 void Deinterlace::SetJson(const std::string value) {
119 
120  // Parse JSON string into JSON objects
121  try
122  {
123  const Json::Value root = openshot::stringToJson(value);
124  // Set all values that match
125  SetJsonValue(root);
126  }
127  catch (const std::exception& e)
128  {
129  // Error parsing JSON (or missing keys)
130  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
131  }
132 }
133 
134 // Load Json::Value into this object
135 void Deinterlace::SetJsonValue(const Json::Value root) {
136 
137  // Set parent data
139 
140  // Set data from Json (if key is found)
141  if (!root["isOdd"].isNull())
142  isOdd = root["isOdd"].asBool();
143 }
144 
145 // Get all properties for a specific frame
146 std::string Deinterlace::PropertiesJSON(int64_t requested_frame) const {
147 
148  // Generate JSON properties list
149  Json::Value root;
150  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
151  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
152  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
153  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
154  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
155  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
156  root["isOdd"] = add_property_json("Is Odd Frame", isOdd, "bool", "", NULL, 0, 1, true, requested_frame);
157 
158  // Add Is Odd Frame choices (dropdown style)
159  root["isOdd"]["choices"].append(add_property_choice_json("Yes", true, isOdd));
160  root["isOdd"]["choices"].append(add_property_choice_json("No", false, isOdd));
161 
162  // Return formatted string
163  return root.toStyledString();
164 }
std::string PropertiesJSON(int64_t requested_frame) const override
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Id() const
Get basic properties.
Definition: ClipBase.h:76
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:79
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:56
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: EffectBase.cpp:117
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
float start
The position in seconds to start playing (used to trim the beginning of a clip)
Definition: ClipBase.h:54
std::string class_name
The class name of the effect.
Definition: EffectBase.h:52
std::string name
The name of the effect.
Definition: EffectBase.h:53
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: EffectBase.cpp:84
std::string Json() const override
Get and Set JSON methods.
Definition: Deinterlace.cpp:99
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:81
This namespace is the default namespace for all code in the openshot library.
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:54
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:55
Exception for invalid JSON.
Definition: Exceptions.h:205
Deinterlace()
Blank constructor, useful when using Json to load the effect properties.
Definition: Deinterlace.cpp:36
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number)
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Definition: Deinterlace.cpp:65
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:77
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:80
void SetJson(const std::string value)
Load JSON string into this object.
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
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:73