OpenShot Library | libopenshot  0.3.2
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.duration = 60 * 60 * 1; // 1 hour duration
65  info.fps = openshot::Fraction(30, 1);
67  info.video_length = std::round(info.duration * info.fps.ToDouble());
68 
69  // Calculate the DAR (display aspect ratio)
70  Fraction dar(
73 
74  // Reduce DAR fraction & set ratio
75  dar.Reduce();
76  info.display_ratio = dar;
77 
78  // Mark as "open"
79  is_open = true;
80  }
81 }
82 
84 {
85  if (is_open)
86  {
87  is_open = false;
88  // Delete the image
89  image.reset();
90  }
91 }
92 
93 // Get an openshot::Frame object for a specific frame number of this reader.
94 std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
95 {
96  if (!is_open) {
97  throw ReaderClosed(
98  "The ImageReader is closed. "
99  "Call Open() before calling this method.", path);
100  }
101 
102  // Create or get frame object
103  auto image_frame = std::make_shared<Frame>(
104  requested_frame,
105  image->size().width(), image->size().height(),
106  "#000000", 0, 2);
107 
108  // Add Image data to frame
109  auto qimage = openshot::Magick2QImage(image);
110  image_frame->AddImage(qimage);
111  return image_frame;
112 }
113 
114 // Generate JSON string of this object
115 std::string ImageReader::Json() const {
116 
117  // Return formatted string
118  return JsonValue().toStyledString();
119 }
120 
121 // Generate Json::Value for this object
122 Json::Value ImageReader::JsonValue() const {
123 
124  // get parent properties
125  Json::Value root = ReaderBase::JsonValue();
126 
127  root["type"] = "ImageReader";
128  root["path"] = path;
129  return root;
130 }
131 
132 // Load JSON string into this object
133 void ImageReader::SetJson(const std::string value) {
134 
135  // Parse JSON string into JSON objects
136  try
137  {
138  const Json::Value root = openshot::stringToJson(value);
139  // Set all values that match
140  SetJsonValue(root);
141  }
142  catch (const std::exception& e)
143  {
144  throw InvalidJSON(
145  "JSON is invalid (missing keys or invalid data types)");
146  }
147 }
148 
149 // Load Json::Value into this object
150 void ImageReader::SetJsonValue(const Json::Value root) {
151 
152  // Set parent data
154 
155  // Set data from Json (if key is found)
156  if (!root["path"].isNull())
157  path = root["path"].asString();
158 
159  if (is_open) {
160  Close();
161  Open();
162  }
163 }
164 
165 #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:107
openshot::ImageReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: ImageReader.cpp:150
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:162
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:83
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:122
openshot::ImageReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: ImageReader.cpp:115
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:94
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:1444
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:133
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.