OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
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
23using namespace openshot;
24
25ImageReader::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;
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
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.
94std::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
115std::string ImageReader::Json() const {
116
117 // Return formatted string
118 return JsonValue().toStyledString();
119}
120
121// Generate Json::Value for this object
122Json::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
133void 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
150void 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
Header file for all Exception classes.
Header file for Frame class.
Header file for ImageReader class.
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
#define MAGICK_IMAGE_ALPHA(im, a)
Header file for QtUtilities (compatibiity overlay)
This class represents a fraction.
Definition Fraction.h:30
int num
Numerator for the fraction.
Definition Fraction.h:32
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition Fraction.cpp:40
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition Fraction.cpp:65
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition Fraction.cpp:78
int den
Denominator for the fraction.
Definition Fraction.h:33
void Open() override
Open File - which is called by the constructor automatically.
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
ImageReader(const std::string &path, bool inspect_reader=true)
Constructor for ImageReader.
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Json() const override
Generate JSON string of this object.
void Close() override
Close File.
Exception for files that can not be found or opened.
Definition Exceptions.h:188
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Exception when a reader is closed, and a frame is requested.
Definition Exceptions.h:364
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
bool has_single_image
Determines if this file only contains a single image.
Definition ReaderBase.h:42
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition ReaderBase.h:48
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
int height
The height of the video (in pixels)
Definition ReaderBase.h:45
int64_t video_length
The number of frames in the video stream.
Definition ReaderBase.h:53
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition ReaderBase.h:52
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
bool has_video
Determines if this file has a video stream.
Definition ReaderBase.h:40
bool has_audio
Determines if this file has an audio stream.
Definition ReaderBase.h:41
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition ReaderBase.h:55
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44