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