OpenShot Library | libopenshot  0.2.3
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 #include "../include/Settings.h"
30 #include "../include/Clip.h"
31 #include "../include/CacheMemory.h"
32 #include <QtCore/QString>
33 #include <QtGui/QImage>
34 #include <QtGui/QPainter>
35 
36 #if USE_RESVG == 1
37  // If defined and found in CMake, utilize the libresvg for parsing
38  // SVG files and rasterizing them to QImages.
39  #include "ResvgQt.h"
40 #endif
41 
42 using namespace openshot;
43 
44 QtImageReader::QtImageReader(string path) : path(path), is_open(false)
45 {
46  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
47  Open();
48  Close();
49 }
50 
51 QtImageReader::QtImageReader(string path, bool inspect_reader) : path(path), is_open(false)
52 {
53  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
54  if (inspect_reader) {
55  Open();
56  Close();
57  }
58 }
59 
60 // Open image file
62 {
63  // Open reader if not already open
64  if (!is_open)
65  {
66  bool success = true;
67  image = std::shared_ptr<QImage>(new QImage());
68 
69 #if USE_RESVG == 1
70  // If defined and found in CMake, utilize the libresvg for parsing
71  // SVG files and rasterizing them to QImages.
72  // Only use resvg for files ending in '.svg' or '.svgz'
73  if (path.find(".svg") != std::string::npos ||
74  path.find(".svgz") != std::string::npos) {
75 
76  ResvgRenderer renderer(QString::fromStdString(path));
77  if (!renderer.isValid()) {
78  success = false;
79  } else {
80 
81  image = std::shared_ptr<QImage>(new QImage(renderer.defaultSize(), QImage::Format_RGBA8888));
82  image->fill(Qt::transparent);
83 
84  QPainter p(image.get());
85  renderer.render(&p);
86  p.end();
87  }
88 
89  } else {
90  // Attempt to open file (old method)
91  success = image->load(QString::fromStdString(path));
92  if (success)
93  image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
94  }
95 #else
96  // Attempt to open file using Qt's build in image processing capabilities
97  success = image->load(QString::fromStdString(path));
98  if (success)
99  image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
100 #endif
101 
102  if (!success)
103  // raise exception
104  throw InvalidFile("File could not be opened.", path);
105 
106  // Update image properties
107  info.has_audio = false;
108  info.has_video = true;
109  info.has_single_image = true;
110  info.file_size = image->byteCount();
111  info.vcodec = "QImage";
112  info.width = image->width();
113  info.height = image->height();
114  info.pixel_ratio.num = 1;
115  info.pixel_ratio.den = 1;
116  info.duration = 60 * 60 * 24; // 24 hour duration
117  info.fps.num = 30;
118  info.fps.den = 1;
119  info.video_timebase.num = 1;
120  info.video_timebase.den = 30;
122 
123  // Calculate the DAR (display aspect ratio)
125 
126  // Reduce size fraction
127  size.Reduce();
128 
129  // Set the ratio based on the reduced fraction
130  info.display_ratio.num = size.num;
131  info.display_ratio.den = size.den;
132 
133  // Mark as "open"
134  is_open = true;
135  }
136 }
137 
138 // Close image file
140 {
141  // Close all objects, if reader is 'open'
142  if (is_open)
143  {
144  // Mark as "closed"
145  is_open = false;
146 
147  // Delete the image
148  image.reset();
149 
150  info.vcodec = "";
151  info.acodec = "";
152  }
153 }
154 
155 // Get an openshot::Frame object for a specific frame number of this reader.
156 std::shared_ptr<Frame> QtImageReader::GetFrame(int64_t requested_frame)
157 {
158  // Check for open reader (or throw exception)
159  if (!is_open)
160  throw ReaderClosed("The Image is closed. Call Open() before calling this method.", path);
161 
162  // Create a scoped lock, allowing only a single thread to run the following code at one time
163  const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
164 
165  // Determine the max size of this source image (based on the timeline's size, the scaling mode,
166  // and the scaling keyframes). This is a performance improvement, to keep the images as small as possible,
167  // without losing quality. NOTE: We cannot go smaller than the timeline itself, or the add_layer timeline
168  // method will scale it back to timeline size before scaling it smaller again. This needs to be fixed in
169  // the future.
170  int max_width = Settings::Instance()->MAX_WIDTH;
171  if (max_width <= 0)
172  max_width = info.width;
173  int max_height = Settings::Instance()->MAX_HEIGHT;
174  if (max_height <= 0)
175  max_height = info.height;
176 
177  Clip* parent = (Clip*) GetClip();
178  if (parent) {
179  if (parent->scale == SCALE_FIT || parent->scale == SCALE_STRETCH) {
180  // Best fit or Stretch scaling (based on max timeline size * scaling keyframes)
181  float max_scale_x = parent->scale_x.GetMaxPoint().co.Y;
182  float max_scale_y = parent->scale_y.GetMaxPoint().co.Y;
183  max_width = max(float(max_width), max_width * max_scale_x);
184  max_height = max(float(max_height), max_height * max_scale_y);
185 
186  } else if (parent->scale == SCALE_CROP) {
187  // Cropping scale mode (based on max timeline size * cropped size * scaling keyframes)
188  float max_scale_x = parent->scale_x.GetMaxPoint().co.Y;
189  float max_scale_y = parent->scale_y.GetMaxPoint().co.Y;
190  QSize width_size(max_width * max_scale_x,
191  round(max_width / (float(info.width) / float(info.height))));
192  QSize height_size(round(max_height / (float(info.height) / float(info.width))),
193  max_height * max_scale_y);
194  // respect aspect ratio
195  if (width_size.width() >= max_width && width_size.height() >= max_height) {
196  max_width = max(max_width, width_size.width());
197  max_height = max(max_height, width_size.height());
198  }
199  else {
200  max_width = max(max_width, height_size.width());
201  max_height = max(max_height, height_size.height());
202  }
203 
204  } else {
205  // No scaling, use original image size (slower)
206  max_width = info.width;
207  max_height = info.height;
208  }
209  }
210 
211  // Scale image smaller (or use a previous scaled image)
212  if (!cached_image || (cached_image && cached_image->width() != max_width || cached_image->height() != max_height)) {
213 
214 #if USE_RESVG == 1
215  // If defined and found in CMake, utilize the libresvg for parsing
216  // SVG files and rasterizing them to QImages.
217  // Only use resvg for files ending in '.svg' or '.svgz'
218  if (path.find(".svg") != std::string::npos ||
219  path.find(".svgz") != std::string::npos) {
220  ResvgRenderer renderer(QString::fromStdString(path));
221  if (renderer.isValid()) {
222 
223  cached_image = std::shared_ptr<QImage>(new QImage(QSize(max_width, max_height), QImage::Format_RGBA8888));
224  cached_image->fill(Qt::transparent);
225 
226  QPainter p(cached_image.get());
227  renderer.render(&p);
228  p.end();
229  }
230  } else {
231  // We need to resize the original image to a smaller image (for performance reasons)
232  // Only do this once, to prevent tons of unneeded scaling operations
233  cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
234  cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
235  }
236 #else
237  // We need to resize the original image to a smaller image (for performance reasons)
238  // Only do this once, to prevent tons of unneeded scaling operations
239  cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
240  cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
241 #endif
242  }
243 
244  // Create or get frame object
245  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));
246 
247  // Add Image data to frame
248  image_frame->AddImage(cached_image);
249 
250  // return frame object
251  return image_frame;
252 }
253 
254 // Generate JSON string of this object
256 
257  // Return formatted string
258  return JsonValue().toStyledString();
259 }
260 
261 // Generate Json::JsonValue for this object
263 
264  // Create root json object
265  Json::Value root = ReaderBase::JsonValue(); // get parent properties
266  root["type"] = "QtImageReader";
267  root["path"] = path;
268 
269  // return JsonValue
270  return root;
271 }
272 
273 // Load JSON string into this object
274 void QtImageReader::SetJson(string value) {
275 
276  // Parse JSON string into JSON objects
277  Json::Value root;
278  Json::Reader reader;
279  bool success = reader.parse( value, root );
280  if (!success)
281  // Raise exception
282  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
283 
284  try
285  {
286  // Set all values that match
287  SetJsonValue(root);
288  }
289  catch (exception e)
290  {
291  // Error parsing JSON (or missing keys)
292  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
293  }
294 }
295 
296 // Load Json::JsonValue into this object
297 void QtImageReader::SetJsonValue(Json::Value root) {
298 
299  // Set parent data
301 
302  // Set data from Json (if key is found)
303  if (!root["path"].isNull())
304  path = root["path"].asString();
305 
306  // Re-Open path, and re-init everything (if needed)
307  if (is_open)
308  {
309  Close();
310  Open();
311  }
312 }
Point GetMaxPoint()
Get max point (by Y coordinate)
Definition: KeyFrame.cpp:207
Json::Value JsonValue()
Generate Json::JsonValue for this object.
int MAX_HEIGHT
Maximum height for image data (useful for optimzing for a smaller preview or render) ...
Definition: Settings.h:92
int num
Numerator for the fraction.
Definition: Fraction.h:44
Keyframe scale_y
Curve representing the vertical scaling in percent (0 to 1)
Definition: Clip.h:215
CriticalSection getFrameCriticalSection
Section lock for multiple threads.
Definition: ReaderBase.h:101
void Open()
Open File - which is called by the constructor automatically.
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
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:65
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:80
Scale the clip until both height and width fill the canvas (cropping the overlap) ...
Definition: Enums.h:51
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:62
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:73
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:100
double Y
The Y value of the coordinate (usually representing the value of the property being animated) ...
Definition: Coordinate.h:57
int MAX_WIDTH
Maximum width for image data (useful for optimzing for a smaller preview or render) ...
Definition: Settings.h:89
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
ScaleType scale
The scale determines how a clip should be resized to fit it's parent.
Definition: Clip.h:146
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
ClipBase * GetClip()
Parent clip object of this reader (which can be unparented and NULL)
Definition: ReaderBase.cpp:252
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:64
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:114
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:169
Scale the clip until both height and width fill the canvas (distort to fit)
Definition: Enums.h:53
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:70
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:72
Coordinate co
This is the primary coordinate.
Definition: Point.h:83
Exception for invalid JSON.
Definition: Exceptions.h:152
Keyframe scale_x
Curve representing the horizontal scaling in percent (0 to 1)
Definition: Clip.h:214
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
static Settings * Instance()
Create or get an instance of this logger singleton (invoke the class with this method) ...
Definition: Settings.cpp:38
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
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:83
Scale the clip until either height or width fills the canvas (with no cropping)
Definition: Enums.h:52
int GetSamplesPerFrame(Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
Definition: Frame.cpp:521
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:82