OpenShot Library | libopenshot  0.5.0
ImageReader.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 // Require ImageMagick support
14 #ifdef USE_IMAGEMAGICK
15 
16 #include "MagickUtilities.h"
17 #include "QtUtilities.h"
18 
19 #include "ImageReader.h"
20 #include "Exceptions.h"
21 #include "Frame.h"
22 
23 using namespace openshot;
24 
25 ImageReader::ImageReader(const std::string& path, bool inspect_reader) : path(path), is_open(false)
26 {
27  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
28  if (inspect_reader) {
29  Open();
30  Close();
31  }
32 }
33 
34 // Open image file
36 {
37  // Open reader if not already open
38  if (!is_open)
39  {
40  // Attempt to open file
41  try
42  {
43  // load image
44  image = std::make_shared<Magick::Image>(path);
45 
46  // Give image a transparent background color
47  image->backgroundColor(Magick::Color("none"));
48  MAGICK_IMAGE_ALPHA(image, true);
49  }
50  catch (const Magick::Exception& e) {
51  // raise exception
52  throw InvalidFile("File could not be opened.", path);
53  }
54 
55  // Update image properties
56  info.has_audio = false;
57  info.has_video = true;
58  info.has_single_image = true;
59  info.file_size = image->fileSize();
60  info.vcodec = image->format();
61  info.width = image->size().width();
62  info.height = image->size().height();
64  info.fps = openshot::Fraction(30, 1);
66  // Default still-image duration: 1 hour, aligned to fps
67  info.video_length = 60 * 60 * info.fps.num; // 3600 seconds * 30 fps
68  info.duration = static_cast<float>(info.video_length / info.fps.ToDouble());
69 
70  // Calculate the DAR (display aspect ratio)
71  Fraction dar(
74 
75  // Reduce DAR fraction & set ratio
76  dar.Reduce();
77  info.display_ratio = dar;
78 
79  // Mark as "open"
80  is_open = true;
81  }
82 }
83 
85 {
86  if (is_open)
87  {
88  is_open = false;
89  // Delete the image
90  image.reset();
91  }
92 }
93 
94 // Get an openshot::Frame object for a specific frame number of this reader.
95 std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
96 {
97  if (!is_open) {
98  throw ReaderClosed(
99  "The ImageReader is closed. "
100  "Call Open() before calling this method.", path);
101  }
102 
103  // Create or get frame object
104  auto image_frame = std::make_shared<Frame>(
105  requested_frame,
106  image->size().width(), image->size().height(),
107  "#000000", 0, 2);
108 
109  // Add Image data to frame
110  auto qimage = openshot::Magick2QImage(image);
111  image_frame->AddImage(qimage);
112  return image_frame;
113 }
114 
115 // Generate JSON string of this object
116 std::string ImageReader::Json() const {
117 
118  // Return formatted string
119  return JsonValue().toStyledString();
120 }
121 
122 // Generate Json::Value for this object
123 Json::Value ImageReader::JsonValue() const {
124 
125  // get parent properties
126  Json::Value root = ReaderBase::JsonValue();
127 
128  root["type"] = "ImageReader";
129  root["path"] = path;
130  return root;
131 }
132 
133 // Load JSON string into this object
134 void ImageReader::SetJson(const std::string value) {
135 
136  // Parse JSON string into JSON objects
137  try
138  {
139  const Json::Value root = openshot::stringToJson(value);
140  // Set all values that match
141  SetJsonValue(root);
142  }
143  catch (const std::exception& e)
144  {
145  throw InvalidJSON(
146  "JSON is invalid (missing keys or invalid data types)");
147  }
148 }
149 
150 // Load Json::Value into this object
151 void ImageReader::SetJsonValue(const Json::Value root) {
152 
153  // Set parent data
155 
156  // Set data from Json (if key is found)
157  if (!root["path"].isNull())
158  path = root["path"].asString();
159 
160  if (is_open) {
161  Close();
162  Open();
163  }
164 }
165 
166 #endif //USE_IMAGEMAGICK
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:106
openshot::ImageReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: ImageReader.cpp:151
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:157
openshot::ImageReader::Open
void Open() override
Open File - which is called by the constructor automatically.
Definition: ImageReader.cpp:35
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::ImageReader::Close
void Close() override
Close File.
Definition: ImageReader.cpp:84
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:43
openshot::ImageReader::ImageReader
ImageReader(const std::string &path, bool inspect_reader=true)
Constructor for ImageReader.
Definition: ImageReader.cpp:25
QtUtilities.h
Header file for QtUtilities (compatibiity overlay)
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:40
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:46
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:40
openshot::ImageReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: ImageReader.cpp:123
openshot::ImageReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: ImageReader.cpp:116
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:53
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:65
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:78
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:44
openshot::ImageReader::GetFrame
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
Definition: ImageReader.cpp:95
openshot::Magick2QImage
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
Definition: MagickUtilities.cpp:46
openshot::ReaderInfo::has_single_image
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:42
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:55
path
path
Definition: FFmpegWriter.cpp:1469
Frame.h
Header file for Frame class.
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition: Exceptions.h:187
openshot::ImageReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: ImageReader.cpp:134
MAGICK_IMAGE_ALPHA
#define MAGICK_IMAGE_ALPHA(im, a)
Definition: MagickUtilities.h:44
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:52
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:363
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
openshot::ReaderInfo::pixel_ratio
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: ReaderBase.h:50
ImageReader.h
Header file for ImageReader class.
openshot::ReaderInfo::display_ratio
openshot::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:51
Exceptions.h
Header file for all Exception classes.