OpenShot Library | libopenshot  0.1.2
QtImageReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for QtImageReader 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/QtImageReader.h"
29 
30 using namespace openshot;
31 
32 QtImageReader::QtImageReader(string path) throw(InvalidFile) : path(path), is_open(false)
33 {
34  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
35  Open();
36  Close();
37 }
38 
39 // Open image file
41 {
42  // Open reader if not already open
43  if (!is_open)
44  {
45  // Attempt to open file
46  image = tr1::shared_ptr<QImage>(new QImage());
47  bool success = image->load(QString::fromStdString(path));
48 
49  // Set pixel format
50  image = tr1::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
51 
52  if (!success)
53  // raise exception
54  throw InvalidFile("File could not be opened.", path);
55 
56  // Update image properties
57  info.has_audio = false;
58  info.has_video = true;
59  info.has_single_image = true;
60  info.file_size = image->byteCount();
61  info.vcodec = "QImage";
62  info.width = image->width();
63  info.height = image->height();
64  info.pixel_ratio.num = 1;
65  info.pixel_ratio.den = 1;
66  info.duration = 60 * 60 * 24; // 24 hour duration
67  info.fps.num = 30;
68  info.fps.den = 1;
70  info.video_timebase.den = 30;
72 
73  // Calculate the DAR (display aspect ratio)
75 
76  // Reduce size fraction
77  size.Reduce();
78 
79  // Set the ratio based on the reduced fraction
80  info.display_ratio.num = size.num;
81  info.display_ratio.den = size.den;
82 
83  // Mark as "open"
84  is_open = true;
85  }
86 }
87 
88 // Close image file
90 {
91  // Close all objects, if reader is 'open'
92  if (is_open)
93  {
94  // Mark as "closed"
95  is_open = false;
96 
97  // Delete the image
98  image.reset();
99 
100  info.vcodec = "";
101  info.acodec = "";
102  }
103 }
104 
105 // Get an openshot::Frame object for a specific frame number of this reader.
106 tr1::shared_ptr<Frame> QtImageReader::GetFrame(long int requested_frame) throw(ReaderClosed)
107 {
108  // Check for open reader (or throw exception)
109  if (!is_open)
110  throw ReaderClosed("The Image is closed. Call Open() before calling this method.", path);
111 
112  // Create or get frame object
113  tr1::shared_ptr<Frame> image_frame(new Frame(requested_frame, info.width, info.height, "#000000", Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), info.channels));
114 
115  // Add Image data to frame
116  image_frame->AddImage(image);
117 
118  // return frame object
119  return image_frame;
120 }
121 
122 // Generate JSON string of this object
124 
125  // Return formatted string
126  return JsonValue().toStyledString();
127 }
128 
129 // Generate Json::JsonValue for this object
131 
132  // Create root json object
133  Json::Value root = ReaderBase::JsonValue(); // get parent properties
134  root["type"] = "QtImageReader";
135  root["path"] = path;
136 
137  // return JsonValue
138  return root;
139 }
140 
141 // Load JSON string into this object
142 void QtImageReader::SetJson(string value) throw(InvalidJSON) {
143 
144  // Parse JSON string into JSON objects
145  Json::Value root;
146  Json::Reader reader;
147  bool success = reader.parse( value, root );
148  if (!success)
149  // Raise exception
150  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
151 
152  try
153  {
154  // Set all values that match
155  SetJsonValue(root);
156  }
157  catch (exception e)
158  {
159  // Error parsing JSON (or missing keys)
160  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
161  }
162 }
163 
164 // Load Json::JsonValue into this object
165 void QtImageReader::SetJsonValue(Json::Value root) throw(InvalidFile) {
166 
167  // Set parent data
169 
170  // Set data from Json (if key is found)
171  if (!root["path"].isNull())
172  path = root["path"].asString();
173 
174  // Re-Open path, and re-init everything (if needed)
175  if (is_open)
176  {
177  Close();
178  Open();
179  }
180 }
long long file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
Json::Value JsonValue()
Generate Json::JsonValue for this object.
int num
Numerator for the fraction.
Definition: Fraction.h:44
void Open()
Open File - which is called by the constructor automatically.
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:115
float duration
Length of time (in seconds)
Definition: ReaderBase.h:64
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:79
void SetJson(string value)
Load JSON string into this object.
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:234
tr1::shared_ptr< Frame > GetFrame(long int requested_frame)
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:61
Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3) ...
Definition: ReaderBase.h:72
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
This class represents a fraction.
Definition: Fraction.h:42
void Close()
Close File.
string Json()
Get and Set JSON methods.
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:63
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:104
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:153
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:108
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:76
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:73
int den
Denominator for the fraction.
Definition: Fraction.h:45
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:82
long int video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
int GetSamplesPerFrame(Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
Definition: Frame.cpp:497
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:81