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