OpenShot Library | libopenshot  0.1.2
Profiles.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Profile 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/Profiles.h"
29 
30 using namespace openshot;
31 
32 
33 // @brief Constructor for Profile.
34 // @param path The folder path / location of a profile file
36 
37  bool read_file = false;
38 
39  try
40  {
41  // Initialize info values
42  info.description = "";
43  info.height = 0;
44  info.width = 0;
45  info.pixel_format = 0;
46  info.fps.num = 0;
47  info.fps.den = 0;
48  info.pixel_ratio.num = 0;
49  info.pixel_ratio.den = 0;
50  info.display_ratio.num = 0;
51  info.display_ratio.den = 0;
52  info.interlaced_frame = false;
53 
54  // Read the profile file
55  ifstream myfile (path.c_str());
56  if (myfile.is_open())
57  {
58  // Loop through each line
59  while (myfile.good())
60  {
61  // read current line of file
62  read_file = true;
63  string line = "";
64  getline (myfile, line);
65 
66  if (line.length() <= 0)
67  continue;
68 
69  // Split current line
70  QString qline(line.c_str());
71  QStringList parts = qline.split( "=" );
72  string setting = parts[0].toStdString();
73  string value = parts[1].toStdString();
74  int value_int = 0;
75 
76  // update struct (based on line number)
77  if (setting == "description")
78  info.description = value;
79  else if (setting == "frame_rate_num") {
80  stringstream(value) >> value_int;
81  info.fps.num = value_int;
82  }
83  else if (setting == "frame_rate_den") {
84  stringstream(value) >> value_int;
85  info.fps.den = value_int;
86  }
87  else if (setting == "width") {
88  stringstream(value) >> value_int;
89  info.width = value_int;
90  }
91  else if (setting == "height") {
92  stringstream(value) >> value_int;
93  info.height = value_int;
94  }
95  else if (setting == "progressive") {
96  stringstream(value) >> value_int;
97  info.interlaced_frame = !(bool)value_int;
98  }
99  else if (setting == "sample_aspect_num") {
100  stringstream(value) >> value_int;
101  info.pixel_ratio.num = value_int;
102  }
103  else if (setting == "sample_aspect_den") {
104  stringstream(value) >> value_int;
105  info.pixel_ratio.den = value_int;
106  }
107  else if (setting == "display_aspect_num") {
108  stringstream(value) >> value_int;
109  info.display_ratio.num = value_int;
110  }
111  else if (setting == "display_aspect_den") {
112  stringstream(value) >> value_int;
113  info.display_ratio.den = value_int;
114  }
115  else if (setting == "colorspace") {
116  stringstream(value) >> value_int;
117  info.pixel_format = value_int;
118  }
119  }
120  myfile.close();
121  }
122 
123  }
124  catch (exception e)
125  {
126  // Error parsing profile file
127  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
128  }
129 
130  // Throw error if file was not read
131  if (!read_file)
132  // Error parsing profile file
133  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
134 }
135 
136 // Generate JSON string of this object
137 string Profile::Json() {
138 
139  // Return formatted string
140  return JsonValue().toStyledString();
141 }
142 
143 // Generate Json::JsonValue for this object
144 Json::Value Profile::JsonValue() {
145 
146  // Create root json object
147  Json::Value root;
148  root["height"] = info.height;
149  root["width"] = info.width;
150  root["pixel_format"] = info.pixel_format;
151  root["fps"] = Json::Value(Json::objectValue);
152  root["fps"]["num"] = info.fps.num;
153  root["fps"]["den"] = info.fps.den;
154  root["pixel_ratio"] = Json::Value(Json::objectValue);
155  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
156  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
157  root["display_ratio"] = Json::Value(Json::objectValue);
158  root["display_ratio"]["num"] = info.display_ratio.num;
159  root["display_ratio"]["den"] = info.display_ratio.den;
160  root["interlaced_frame"] = info.interlaced_frame;
161 
162  // return JsonValue
163  return root;
164 }
165 
166 // Load JSON string into this object
167 void Profile::SetJson(string value) throw(InvalidJSON) {
168 
169  // Parse JSON string into JSON objects
170  Json::Value root;
171  Json::Reader reader;
172  bool success = reader.parse( value, root );
173  if (!success)
174  // Raise exception
175  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
176 
177  try
178  {
179  // Set all values that match
180  SetJsonValue(root);
181  }
182  catch (exception e)
183  {
184  // Error parsing JSON (or missing keys)
185  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
186  }
187 }
188 
189 // Load Json::JsonValue into this object
190 void Profile::SetJsonValue(Json::Value root) {
191 
192  if (!root["height"].isNull())
193  info.height = root["height"].asInt();
194  if (!root["width"].isNull())
195  info.width = root["width"].asInt();
196  if (!root["pixel_format"].isNull())
197  info.pixel_format = root["pixel_format"].asInt();
198  if (!root["fps"].isNull()) {
199  info.fps.num = root["fps"]["num"].asInt();
200  info.fps.den = root["fps"]["den"].asInt();
201  }
202  if (!root["pixel_ratio"].isNull()) {
203  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
204  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
205  }
206  if (!root["display_ratio"].isNull()) {
207  info.display_ratio.num = root["display_ratio"]["num"].asInt();
208  info.display_ratio.den = root["display_ratio"]["den"].asInt();
209  }
210  if (!root["interlaced_frame"].isNull())
211  info.interlaced_frame = root["interlaced_frame"].asBool();
212 
213 }
Profile(string path)
Constructor for Profile.
Definition: Profiles.cpp:35
int num
Numerator for the fraction.
Definition: Fraction.h:44
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:83
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Profiles.cpp:144
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Profiles.cpp:190
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: Profiles.h:61
int width
The width of the video (in pixels)
Definition: Profiles.h:58
void SetJson(string value)
Load JSON string into this object.
Definition: Profiles.cpp:167
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: Profiles.h:60
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: Profiles.h:59
int height
The height of the video (in pixels)
Definition: Profiles.h:57
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3) ...
Definition: Profiles.h:62
int den
Denominator for the fraction.
Definition: Fraction.h:45
string Json()
Get and Set JSON methods.
Definition: Profiles.cpp:137