OpenShot Library | libopenshot  0.1.9
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) : 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 QtImageReader::QtImageReader(string path, bool inspect_reader) : path(path), is_open(false)
40 {
41  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
42  if (inspect_reader) {
43  Open();
44  Close();
45  }
46 }
47 
48 // Open image file
50 {
51  // Open reader if not already open
52  if (!is_open)
53  {
54  // Attempt to open file
55  image = std::shared_ptr<QImage>(new QImage());
56  bool success = image->load(QString::fromStdString(path));
57 
58  // Set pixel format
59  image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
60 
61  if (!success)
62  // raise exception
63  throw InvalidFile("File could not be opened.", path);
64 
65  // Update image properties
66  info.has_audio = false;
67  info.has_video = true;
68  info.has_single_image = true;
69  info.file_size = image->byteCount();
70  info.vcodec = "QImage";
71  info.width = image->width();
72  info.height = image->height();
73  info.pixel_ratio.num = 1;
74  info.pixel_ratio.den = 1;
75  info.duration = 60 * 60 * 24; // 24 hour duration
76  info.fps.num = 30;
77  info.fps.den = 1;
79  info.video_timebase.den = 30;
81 
82  // Calculate the DAR (display aspect ratio)
84 
85  // Reduce size fraction
86  size.Reduce();
87 
88  // Set the ratio based on the reduced fraction
89  info.display_ratio.num = size.num;
90  info.display_ratio.den = size.den;
91 
92  // Mark as "open"
93  is_open = true;
94  }
95 }
96 
97 // Close image file
99 {
100  // Close all objects, if reader is 'open'
101  if (is_open)
102  {
103  // Mark as "closed"
104  is_open = false;
105 
106  // Delete the image
107  image.reset();
108 
109  info.vcodec = "";
110  info.acodec = "";
111  }
112 }
113 
114 void QtImageReader::SetMaxSize(int width, int height)
115 {
116  // Determine if we need to scale the image (for performance reasons)
117  // The timeline passes its size to the clips, which pass their size to the readers, and eventually here
118  // A max_width/max_height = 0 means do not scale (probably because we are scaling the image larger than 100%)
119 
120  // Remove cache that is no longer valid (if needed)
121  if (cached_image && (cached_image->width() != width && cached_image->height() != height))
122  // Expire this cache
123  cached_image.reset();
124 
125  max_width = width;
126  max_height = height;
127 }
128 
129 // Get an openshot::Frame object for a specific frame number of this reader.
130 std::shared_ptr<Frame> QtImageReader::GetFrame(int64_t requested_frame)
131 {
132  // Check for open reader (or throw exception)
133  if (!is_open)
134  throw ReaderClosed("The Image is closed. Call Open() before calling this method.", path);
135 
136  if (max_width != 0 && max_height != 0 && max_width < info.width && max_height < info.height)
137  {
138  // Scale image smaller (or use a previous scaled image)
139  if (!cached_image) {
140  // Create a scoped lock, allowing only a single thread to run the following code at one time
141  const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
142 
143  // We need to resize the original image to a smaller image (for performance reasons)
144  // Only do this once, to prevent tons of unneeded scaling operations
145  cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
146  cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
147  }
148 
149  // Create or get frame object
150  std::shared_ptr<Frame> image_frame(new Frame(requested_frame, cached_image->width(), cached_image->height(), "#000000", Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), info.channels));
151 
152  // Add Image data to frame
153  image_frame->AddImage(cached_image);
154 
155  // return frame object
156  return image_frame;
157 
158  } else {
159  // Use original image (higher quality but slower)
160  // Create or get frame object
161  std::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));
162 
163  // Add Image data to frame
164  image_frame->AddImage(image);
165 
166  // return frame object
167  return image_frame;
168  }
169 }
170 
171 // Generate JSON string of this object
173 
174  // Return formatted string
175  return JsonValue().toStyledString();
176 }
177 
178 // Generate Json::JsonValue for this object
180 
181  // Create root json object
182  Json::Value root = ReaderBase::JsonValue(); // get parent properties
183  root["type"] = "QtImageReader";
184  root["path"] = path;
185 
186  // return JsonValue
187  return root;
188 }
189 
190 // Load JSON string into this object
191 void QtImageReader::SetJson(string value) {
192 
193  // Parse JSON string into JSON objects
194  Json::Value root;
195  Json::Reader reader;
196  bool success = reader.parse( value, root );
197  if (!success)
198  // Raise exception
199  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
200 
201  try
202  {
203  // Set all values that match
204  SetJsonValue(root);
205  }
206  catch (exception e)
207  {
208  // Error parsing JSON (or missing keys)
209  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
210  }
211 }
212 
213 // Load Json::JsonValue into this object
214 void QtImageReader::SetJsonValue(Json::Value root) {
215 
216  // Set parent data
218 
219  // Set data from Json (if key is found)
220  if (!root["path"].isNull())
221  path = root["path"].asString();
222 
223  // Re-Open path, and re-init everything (if needed)
224  if (is_open)
225  {
226  Close();
227  Open();
228  }
229 }
int max_height
The maximium image height needed by this clip (used for optimizations)
Definition: ReaderBase.h:103
Json::Value JsonValue()
Generate Json::JsonValue for this object.
int num
Numerator for the fraction.
Definition: Fraction.h:44
CriticalSection getFrameCriticalSection
Section lock for multiple threads.
Definition: ReaderBase.h:99
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
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
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
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
void SetMaxSize(int width, int height)
Set Max Image Size (used for performance optimization)
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:106
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:155
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
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
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
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
int max_width
The maximum image width needed by this clip (used for optimizations)
Definition: ReaderBase.h:102
int GetSamplesPerFrame(Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
Definition: Frame.cpp:505
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