OpenShot Library | libopenshot  0.3.3
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 = BasePropertiesJSON(requested_frame);
135 
136  // Add Is Odd Frame choices (dropdown style)
137  root["isOdd"] = add_property_json("Is Odd Frame", isOdd, "bool", "", NULL, 0, 1, true, requested_frame);
138  root["isOdd"]["choices"].append(add_property_choice_json("Yes", true, isOdd));
139  root["isOdd"]["choices"].append(add_property_choice_json("No", false, isOdd));
140 
141  // Return formatted string
142  return root.toStyledString();
143 }
openshot::ClipBase::add_property_json
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
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Deinterlace::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Deinterlace.cpp:91
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Deinterlace::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Deinterlace.cpp:84
openshot::ClipBase::add_property_choice_json
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
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Deinterlace::Deinterlace
Deinterlace()
Default constructor, useful when using Json to load the effect properties.
Definition: Deinterlace.cpp:19
openshot::Deinterlace::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Deinterlace.cpp:131
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
Definition: EffectBase.cpp:179
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
openshot::ClipBase::start
float start
The position in seconds to start playing (used to trim the beginning of a clip)
Definition: ClipBase.h:38
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::Deinterlace::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Deinterlace.cpp:103
openshot::Deinterlace::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Deinterlace.cpp:120
openshot::Deinterlace::GetFrame
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
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
Deinterlace.h
Header file for De-interlace class.
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115